How to Call Script Include from Client Script in ServiceNow


Step 1: 
ClientScript
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') return;
var user = g_form.getValue('u_user');
var ga = new GlideAjax('global.sampleUtils');
ga.addParam('sysparm_name', 'getUserDetails');
ga.addParam('userId', user);
ga.getXMLAnswer(function(response) {
var res = JSON.parse(response);
g_form.setValue('u_phone', res.mobile_phone);
g_form.setValue('u_email', res.email);
});
}
Step 2: ClientCallable ScriptInclude  
var sampleUtils = Class.create();
sampleUtils.prototype = Object.extendsObject(AbstractAjaxProcessor, {

getUserDetails: function() {
var userId = this.getParameter('userId');
var obj = {};
var grSysUser = new GlideRecord('sys_user');
if (grSysUser.get(userId)) {
obj.mobile_phone = grSysUser.getValue('mobile_phone');
obj.email = grSysUser.getValue('email');
}
return JSON.stringify(obj);
},
type: 'sampleUtils'
});

Calling ScriptInclude from the ClientScript will be tricky, so we need to use the GlideAjax approach.

Note:
We can write the GlideRecord as well, but it's not recommended.

Let's take one example:
When selecting a User field, we populate the UserInformation in other fields like Email and Phone.

 









To get the requirement done,
The first thing we need is a ClientScript to trigger the change functionality, and then we require a ScriptInclude to pull the data related to that user record. 


Refer to the implementation video: (Apologies for the background music)


Refer to the servicenow community article with detailed screenshots: