Mr world wide
Mega Guru

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 the calling script included from the client script will be a little bit tricky, we need to use the GlideAjax approach to call script include.

 

Let's take a practical example::

Please refer to the full video tutorial from here: 

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 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 select 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);
	}
	
}

find_real_file.png

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'
});

find_real_file.png

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

find_real_file.png
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.

Comments
Ankit Jambhale
Tera Contributor

how to use Global application scope with another scope 

ersureshbe
Giga Sage
Giga Sage

Hi, you can use EfficientGlideRecord function instead of GlideAjax and Script include. 

Ankit Jambhale
Tera Contributor

Not working

 

Ankit Jambhale
Tera Contributor

can we connect 

Carlo Jimenez
Tera Contributor

What should be the type of the record producer variable?

Farhana1
Tera Explorer

not working

Ghazali
Tera Contributor

When trying to save the Script Include, a popup comes asking to select a user role, what role to select?

CP-UW
Tera Contributor

The music in the video is awful. Why is there music in a coding how to video?

A_Stembreit
Tera Contributor

Would this work for a record producer on the service portal? 

BillMartin
Mega Sage

Sharing this live example demo where I added a resuable class and made the AbastractAjaxProcessor become a base class to dynamically call parameters that can revceive any type of class, function and payload from the client towards the server side using GlideAjax.

 

Dynamic Script Include and GlideAjax in ServiceNow: Scalable & Reusable Code for Architects

 

BillMartin
Mega Sage

i created this full course about script include and glide ajax. how to call script include from client script.

 

Version history
Last update:
‎06-05-2021 03:14 PM
Updated by: