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

Aman Kumar S
Kilo Patron

Hey,

I don't see you passing catalog item from client script to script include.

Could you include that?

Also, can you confirm, on which field you have written this onChange script?

Best Regards
Aman Kumar

Hi Aman

 

i will add this line 

ga.addParam('sysparm_catitem', g_form.getUniqueValue());

 

onchange  'requested_for'  variable

 

Thanks

Levino

i think i need to do this as well

ga.addParam('sysparm_user', g_form.getValue('requested_for'));

 

 

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
    if (isLoading || newValue === '') {
        return;
    }
  
    var ga = new GlideAjax('restrictduplicateitem');
    ga.addParam('sysparm_name', 'restrictduplication');
    ga.addParam('sysparm_user', g_form.getValue('requested_for'));
	ga.addParam('sysparm_catitem', g_form.getUniqueValue());
    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);
        }
    }

}