confusion about script include , glide ajax

imran nannu
Tera Contributor

pls give me differences between glide ajax & script include with best examples 

 

Thanks in advance 

4 REPLIES 4

Prateek kumar
Mega Sage

Take a look at these.

https://www.youtube.com/watch?v=qPMeBYVuJAQ

https://www.youtube.com/watch?v=Yaes8WxQ-h8


Please mark my response as correct and helpful if it helped solved your question.
-Thanks

Varsha21
Giga Guru

Hi

Script include: is a server side script , we can use it like a reusable code.

Server side reusable code-> script include as like Client side Reusable code -> Ui script

E.g if we want to use a some logic again and again then instead of writing such logic again and again ,Write once and call many times.

then we should go for script include.It is not specific to any table,it can called on demand of user.

for e.g email can be needed many times,go through link to get email address many times from different client

script include can be call from server side and client side as well,

Example to call at client side

 https://developer.servicenow.com/app.do#!/training/article/app_store_learnv2_scripting_madrid_server...

var GetEmailAddress = Class.create();
// Extend the global.AbstractAjaxProcessor class
GetEmailAddress.prototype = Object.extendsObject(global.AbstractAjaxProcessor,{
	// Define the getEmail function.  
	// Create a GlideRecord for the User table.
	// Use the sysparm_userID passed from the client side to retrieve a record from the User table.
	// Return the email address for the requested record
	getEmail: function() {
		var userRecord = new GlideRecord("sys_user");
		userRecord.get(this.getParameter('sysparm_userID'));
		return userRecord.email + '';
	},
	type: 'GetEmailAddress'
});

GlideAjax() :  is API which is used to call script include from client side,it has some predefined methods,as like java class,

Above link is EX. Script include called from client side using gildeAjax()

just go through syntax,

we should create object of Sript incude.and call the script include.

like

	var getEmailAddr = new GlideAjax('GetEmailAddress');
	// Specify the getEmail method
	getEmailAddr.addParam('sysparm_name','getEmail');
	// Pass the Requested for sys_id
	getEmailAddr.addParam('sysparm_userID', g_form.getValue('u_requested_for'));
	// Send the request to the server
	getEmailAddr.getXML(populateEmailField);


	// When the response is back from the server
	function populateEmailField(response){
		// Extract the email address from the response, clear any value from the email field, 
		// set new value in the email field
		var emailFromScriptInclude = response.responseXML.documentElement.getAttribute("answer");
		g_form.clearValue('u_requested_for_email');
		g_form.setValue('u_requested_for_email',emailFromScriptInclude);

Above example is called Script include from client side,

just go through above link. 

If script include is Client callable then only we can call it through Client script and using GlideAjax.

Hope this will give you better idea.

 

 

adamn
Tera Contributor

Varsha's reply is very good and accurate.

Just a quick tip though

Script includes can be both Client callable (Ajax) and Server Side callable if you design them right

 

For the sake of example, say you want to pass a string which gets logged to the system log (pointless but its an example)

 

You could do this for an Ajax only approach

logThisString: function(){
	gs.log(this.getParamter('sysparm_string');
}

(This takes the GlideAjax addParam value of 'sysparm_string' and logs it using gs.log)

 

However if we also wanted to do this server side we'd need another script include or function.

Instead we can write the server side function, then a connector function to access it from Ajax

logThisStringServer: function(string){
	gs.log(string);
}

logThisStringAjax: function(){
	this.logThisStringServer(this.getParameter('sysparm_string'))
}

So in this the lgoThisStringServer function does the job based on an input string variable, and the logThisStringAjax function calls the logThisStringServer function by referencing itself using this (in the context of a script include this is the script include)

This means we can call this script include

From a business rule:

new ScriptIncludeName().logThisStringServer('my string');

From a client script:

var ga =  new GlideAjax('ScriptIncludeName');

ga.addParam('sysparm_name', 'logThisStringAjax');

ga.addParam('sysparm_string', 'my string');

ga.getXML(callback);

callback is a function which handles the response, docs has the full information on using GlideAjax

 

Hope this tip helps 🙂

 

 

Sanket Shinde1
Tera Contributor

Hello imran nannu,

 

A very easy explanation.

Script include -

Its a server side script which can be reused. 

Script includes can be called from any other server side script and if the client-callable checkbox is checked from client script too.

 

Glide Ajax -

If the script includes are client-callable, i.e. client-callable check box is checked, then the script include can be called from client side script using glide ajax.

Glide ajax can be synchronous as well as asynchronous.