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

Raghu Ram Y
Kilo Sage

Hi @Jayshree 

In Onsubmit client script validation won't work(means...getting some data from other tables).. only the way is you need to use Synchronous GlideAjax but it won't work on Service Portal.

So I suggest you to set reference qualifier for the "Requested on" field for the catalog item..so that it will show users only who is having division or subdivision.

shloke04
Kilo Patron

Hi @Jayshree 

My suggestion here will be to use an On Change Catalog Client script and not an On Submit script as it will be difficult to validate as in Service portal during submission it won't wait for a response coming from server to validate the Division and Sub Division of the user.

Create a Client Callable Script Include and use the script as below:

var getCallerDetails = Class.create();
getCallerDetails.prototype = Object.extendsObject(AbstractAjaxProcessor, {
	
	getDetails : function(){
		var getRequester = this.getParameter('sysparm_requester');
		
		var gr = new GlideRecord('sys_user');
		gr.addQuery('sys_id',getRequester);
		gr.query();
		if(gr.next()){
			var obj = {};
			obj.DIVISION = gr.Field_Name_of_division;
			obj.SUB_DIV = gr.Field_Name_of_SUB_division;
			
		}
		return JSON.stringify(obj);
	},

    type: 'getCallerDetails'
});

find_real_file.png

Now create an On Change Catalog Client script in your Catalog item and use the script below. This script will clear out the Requester value if Division is Customer and Sub Division is Support and make it mandatory which will not allow them to Submit the form as mentioned below:

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }
    
    var gaPhone = new GlideAjax('getCallerDetails');
    gaPhone.addParam('sysparm_name', 'getDetails');
    gaPhone.addParam('sysparm_po', newValue);
    gaPhone.getXMLAnswer(_handleResponse);

    function _handleResponse(response) {
        var answer = response;
		var parseAnswer = JSON.parse(answer);
		if(parseAnswer.DIVISION == 'customer' && parseAnswer.SUB_DIV == 'support'){
			g_form.clearValue('Variable Name of Requester');
			g_form.setMandatory('Variable Name of requester',true);
		}

        g_form.setValue('Variable name', answer); //Repalce your Variable Name where you want to set the value in
    }

}

 

If you still need to do it via On Submit Catalog Client Script which will add a bit on extra complexity but is feasible and the solution has been mentioned in below blog on how you should do it:

https://snprotips.com/blog/2018/10/19/synchronous-lite-onsubmit-catalogclient-scripts

Hope this helps. Please mark the answer as correct/helpful based on impact.

Regards,
Shloke

Hope this helps. Please mark the answer as correct/helpful based on impact.

Regards,
Shloke

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

Hello Ankur,

 

I am using on submit client script with getReference method using Callback function.