How to prevent submission of catalog form according to requester details with the help of on submit client script

Jayshree2
Tera Contributor

I wanted to prevent form submission where requested on catalog item who is having the division and subdivision is customer and support in sys_user table then that user will  be able to submit the catalog item form or else not.

i tried to get this solve with the help of getReference method in onsubmit client script but it is not working.

1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

so what script did you use?

you can use onChange client script + GlideAjax

OR

you can use onSubmit with GlideAjax and custom solution for synchronous ajax call

Regards
Ankur

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

View solution in original post

6 REPLIES 6

Hi,

you can use onChange client script + GlideAjax

OR

you can use onSubmit with GlideAjax and custom solution for synchronous ajax call

But remember these points:

1) Asynchronous GlideAjax won't work as by the time ajax function returns the value your form will get submitted

2) Synchronous GlideAjax is not allowed in portal and scoped application

Refer this link

How to limit the total quantity of a shopping cart for a specific service catalog item in New York v...

Refer these links for workaround/solution on how to use Synchronous GlideAjax in onSubmit

How To: Async GlideAjax in an onSubmit script

Asynchronous onSubmit Catalog/Client Scripts in ServiceNow

Regards
Ankur

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

Service_RNow
Mega Sage

HI,

please try below code:-

Script Include: It should be client callable

var CheckRecords = Class.create();
CheckRecords.prototype = Object.extendsObject(AbstractAjaxProcessor, {

	checkRecordPresent: function(){
		var obj = {};
		var id = this.getParameter('sysparm_userID');            
		var gr = new GlideRecord('sys_user');
		gr.addQuery('sys_id', id); 
		gr.query();
		if(gr.next()){
			obj['email'] = gr.getValue('email');
			obj['manager'] = gr.getValue('manager');
			obj['location'] = gr.getValue('location');
			obj['region'] = gr.region.toString();
		}
		return JSON.stringify(obj);
	},
	type: 'CheckRecords'
});
onChange Client Script:

function onChange(control, oldValue, newValue, isLoading) {
	if (isLoading || newValue == '') {
		return;
	}

	var ga = new GlideAjax('CheckRecords');
	ga.addParam('sysparm_name', "checkRecordPresent");
	ga.addParam('sysparm_userID', g_form.getValue('requested_for_variable')); // give here your variable name
	ga.getXMLAnswer(function(answer){
		if(answer != 'not found'){
			var parser = JSON.parse(answer);
			g_form.setValue('manager_variable', parser.manager); // give here manager variable name
			g_form.setValue('email_variable', parser.email); // give here email variable name
			g_form.setValue('location_variable', parser.location); // give here location variable name
			g_form.setValue('region_variable', parser.region); // give here region variable name
		}
	});
	//Type appropriate comment here, and begin script below

}

Hope this helps.
please mark my answer as Correct & Helpful based on the validations.