How to call script include from client script #ServiceNow #GlideAjax



How to call script include from client script #ServiceNow #GlideAjax

How to call script include from client script #ServiceNow #GlideAjax

Please find the article here explaining the code clearly:
https://community.servicenow.com/community?id=community_article&sys_id=25975379dbe0f8d0190dfb2439961937

We can call script include from client script using Glideajax:
Calling a script include from the business rule or from any server-side scripting it is very easy and straightforward syntax is there for that.

Whereas calling script include from the client script will be a little bit tricky, we need to use the GlideAjax approach to call script include.

Use case:
On selecting a user field we are trying to populate the user information on other fields.

To get the requirement done,
The first thing we need is a client script to trigger on change functionality and then we required a script include to pull the data related to that user record.

Step 1:
Create a client script by selecting type as onChange and selecting the field as the user

Add the following script to your client script:

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

var user = g_form.getValue(‘u_user’);

//Call script include
var ga = new GlideAjax(‘global.sampleUtils’); //Scriptinclude
ga.addParam(‘sysparm_name’, ‘getUserDetails’); //Method
ga.addParam(‘userId’,user); //Parameters
ga.getXMLAnswer(getResponse);

function getResponse(response){
console.log(response);
var res = JSON.parse(response);
console.log(res);
g_form.setValue(‘u_phone’,res.mobile_phone);
g_form.setValue(‘u_email’,res.email);
}

}

Step 2:
Create a script include and make it client callable (select the client callable checkbox)
Add the following script to your script include:

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

getUserDetails: function(){
gs.addInfoMessage(‘script include triggered’);

var userId = this.getParameter(‘userId’);
gs.addInfoMessage(‘user scr–‘+userId);
obj = {};

var grSysUser = new GlideRecord(‘sys_user’);
if (grSysUser.get(userId)) {
obj.mobile_phone = grSysUser.getValue(‘mobile_phone’);
obj.email = grSysUser.getValue(’email’);
}
gs.addInfoMessage(obj+JSON.stringify(obj));
return JSON.stringify(obj);
},

type: ‘sampleUtils’
});

That’s it, now as soon as you select a user your fields will auto-populate based on that.

Thank you so much for watching the tutorial please hit the like button if you like the video and don’t forget to subscribe for more interesting videos.

If you have any questions please drop them in the comments below.

Access the script include from client script
Accessing script includes are from on load client script
Read script include from client script
Can we call script include from a business rule
How to call script include from client script
Service now glideajax tutorial
Service now script include call from client script tutorial
A step-by-step guide to calling script include from the client script service now

Comments are closed.