restrict a catalog item to just one time request per user

levino
Giga Guru

Hi there

i have a requirement to restrict a catalog item to just one time request per user, that is once its requested and state pending or completed it cannot be requested again.

This is requested via the servicenow portal.

the attached script does not seem to work it always comes up with the message  when i change user  'duplicate request cannot be submitted'

Thanks

Levino

 

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
    if (isLoading || newValue === '') {
        return;
    }
  
    var ga = new GlideAjax('restrictduplicateitem');
    ga.addParam('sysparm_name', 'restrictduplication');
    ga.addParam('sysparm_reqfor', newValue);
    ga.getXML(alertUser);

    function alertUser(response) {
        var answer = response.responseXML.documentElement.getAttribute("answer");
        if (answer) {
            g_form.showFieldMsg('requested_for', 'Duplicate request can not be submitted', 'Error');
            g_form.setValue('hidden_variable', true);
        } else {
            g_form.hideFieldMsg('hidden_variable', true);
        }
    }

}


var restrictduplicateitem = Class.create();
restrictduplicateitem.prototype = Object.extendsObject(AbstractAjaxProcessor, {
restrictduplication: function() {
       var user = this.getParameter('sysparm_user');
		var cat_item_id = this.getParameter('sysparm_catitem');
        
        var gr = new GlideRecord('sc_req_item');
        gr.addEncodedQuery('cat_item=' + cat_item_id + '^stateNOT IN3,4,7^request.requested_for=' + req_for);
        gr.query();
        if (gr.next())
            return true;
        else
            return false;
    },
    type: 'restrictduplicateitem'
});


function onSubmit() {
    //Type appropriate comment here, and begin script below
    if (g_form.getValue('hidden_variable') == 'true') {
        return false;
    }
}

 

Thanks

Levino

1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

you don't require onSubmit

if the validation fails then clear the requested for and show error so that unless user selects correct user form cannot be submitted

try this

var restrictduplicateitem = Class.create();
restrictduplicateitem.prototype = Object.extendsObject(AbstractAjaxProcessor, {
	
	
	restrictduplication: function() {
		var user = this.getParameter('sysparm_user');
		var cat_item_id = this.getParameter('sysparm_catitem');

		var gr = new GlideRecord('sc_req_item');
		gr.addEncodedQuery('cat_item=' + cat_item_id + '^stateNOT IN3,4,7^request.requested_for=' + user);
		gr.query();
		return gr.hasNext();
	},
	type: 'restrictduplicateitem'
});
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
	if (isLoading || newValue === '') {
		return;
	}

	g_form.hideFieldMsg('requested_for', true);

	if(oldValue != newValue){
		var ga = new GlideAjax('restrictduplicateitem');
		ga.addParam('sysparm_name', 'restrictduplication');
		ga.addParam('sysparm_reqfor', newValue);
		ga.getXML(alertUser);
		function alertUser(response) {
			var answer = response.responseXML.documentElement.getAttribute("answer");
			if (answer.toString() == 'true') {
				g_form.clearValue('requested_for', true);
				g_form.showFieldMsg('requested_for', 'Duplicate request can not be submitted', 'Error');
			} 
		}
	}
}

Regards
Ankur

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

View solution in original post

20 REPLIES 20

i have tried this still no luck

var restrictduplicateitem = Class.create();
restrictduplicateitem.prototype = Object.extendsObject(AbstractAjaxProcessor, {
    restrictduplication: function() {
        var user = this.getParameter('sysparm_user');
        var cat_item_id = this.getParameter('sysparm_catitem');

        var gr = new GlideRecord('sc_req_item');
        gr.addEncodedQuery('cat_item=' + cat_item_id + '^stateNOT IN3,4,7^request.requested_for=' + user);
        gr.query();
        if (gr.next())
            return true;
        else
            return false;
    },
    type: 'restrictduplicateitem'
});

Hey Levino.

What I will suggest you to check, if your values that you are passing from the client script, if the values are getting passed to the script include are correct or not by putting gs.info statements in Script include for variables

Also, try to check if your addencodedquery whatever you have written try to put same values in the list layout to see if it is giving any result

 

Best Regards
Aman Kumar

Feel free to mark correct if I answered your query correctly. Cheers!
Best Regards
Aman Kumar

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

you don't require onSubmit

if the validation fails then clear the requested for and show error so that unless user selects correct user form cannot be submitted

try this

var restrictduplicateitem = Class.create();
restrictduplicateitem.prototype = Object.extendsObject(AbstractAjaxProcessor, {
	
	
	restrictduplication: function() {
		var user = this.getParameter('sysparm_user');
		var cat_item_id = this.getParameter('sysparm_catitem');

		var gr = new GlideRecord('sc_req_item');
		gr.addEncodedQuery('cat_item=' + cat_item_id + '^stateNOT IN3,4,7^request.requested_for=' + user);
		gr.query();
		return gr.hasNext();
	},
	type: 'restrictduplicateitem'
});
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
	if (isLoading || newValue === '') {
		return;
	}

	g_form.hideFieldMsg('requested_for', true);

	if(oldValue != newValue){
		var ga = new GlideAjax('restrictduplicateitem');
		ga.addParam('sysparm_name', 'restrictduplication');
		ga.addParam('sysparm_reqfor', newValue);
		ga.getXML(alertUser);
		function alertUser(response) {
			var answer = response.responseXML.documentElement.getAttribute("answer");
			if (answer.toString() == 'true') {
				g_form.clearValue('requested_for', true);
				g_form.showFieldMsg('requested_for', 'Duplicate request can not be submitted', 'Error');
			} 
		}
	}
}

Regards
Ankur

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

Thanks Ankur

seen Hitoshi comment, is BR the way to go?