Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

how to call script include in server side and Client side? i need example?

ram2497
Tera Contributor

how to call script include in  server side and Client side? i need example?

1 ACCEPTED SOLUTION

ScienceSoft
Tera Guru

Hi, ram2497

here are 3 EXAMPLES that you may learn from: 

EXAMPLE 1: Single function (server)

find_real_file.png

function exampleOne() {
	gs.log('This is the exampleOne');
}

EXAMPLE 2: Multiple functions (server)

find_real_file.png

 

var ScriptIncludeExampleTwo = Class.create();
ScriptIncludeExampleTwo.prototype = {
	initialize: function() {
	},
	functionOne: function(){
		gs.log('This is functionOne');
	},
	functionTwo: function(){
		gs.log('This is functionTwo');
	},
	type: 'ScriptIncludeExampleTwo'
};

 Then call it (from server-side):

var foo =new ScriptIncludeExampleTwo();
foo.functionOne();
foo.functionTwo();

EXAMPLE 3: Ajax (Client-callable)

find_real_file.png

var ScriptIncludeExampleThree = Class.create();
ScriptIncludeExampleThree.prototype = Object.extendsObject(AbstractAjaxProcessor, {
	helloWorld:function() {
		return "Hello " + this.getParameter('sysparm_user_name') + "!";
	} ,
	type: 'ScriptIncludeExampleThree'
});

 Then call it from client:

var ga = new GlideAjax('ScriptIncludeExampleThree');
ga.addParam('sysparm_user_name', "Bob");
ga.getXML(HelloWorldParse);
 
function HelloWorldParse(response) {
  var answer = response.responseXML.documentElement.getAttribute("answer");
  alert(answer); 
}

Please refer to ServiceNow documentation for more information:

  • https://docs.servicenow.com/bundle/kingston-application-development/page/script/server-scripting/concept/c_ScriptIncludes.html
  • https://docs.servicenow.com/bundle/kingston-application-development/page/script/ajax/topic/p_AJAX.html

View solution in original post

3 REPLIES 3

Omkar Mone
Mega Sage

Hi 

In client side you can use GlideAjax in order to call Script Include.

like this,

var ga = new GlideAjax('name of Script include');

ga.addparam('sysparm_name','name of fucntion in script include');

ga.getXML(ResponseFunction);

 

function ResponseFunction(response)

{

here you get your response.

}

 

in Server side you can achieve this by

 var a = new ScriptIncludeName.functionName();

ScienceSoft
Tera Guru

Hi, ram2497

here are 3 EXAMPLES that you may learn from: 

EXAMPLE 1: Single function (server)

find_real_file.png

function exampleOne() {
	gs.log('This is the exampleOne');
}

EXAMPLE 2: Multiple functions (server)

find_real_file.png

 

var ScriptIncludeExampleTwo = Class.create();
ScriptIncludeExampleTwo.prototype = {
	initialize: function() {
	},
	functionOne: function(){
		gs.log('This is functionOne');
	},
	functionTwo: function(){
		gs.log('This is functionTwo');
	},
	type: 'ScriptIncludeExampleTwo'
};

 Then call it (from server-side):

var foo =new ScriptIncludeExampleTwo();
foo.functionOne();
foo.functionTwo();

EXAMPLE 3: Ajax (Client-callable)

find_real_file.png

var ScriptIncludeExampleThree = Class.create();
ScriptIncludeExampleThree.prototype = Object.extendsObject(AbstractAjaxProcessor, {
	helloWorld:function() {
		return "Hello " + this.getParameter('sysparm_user_name') + "!";
	} ,
	type: 'ScriptIncludeExampleThree'
});

 Then call it from client:

var ga = new GlideAjax('ScriptIncludeExampleThree');
ga.addParam('sysparm_user_name', "Bob");
ga.getXML(HelloWorldParse);
 
function HelloWorldParse(response) {
  var answer = response.responseXML.documentElement.getAttribute("answer");
  alert(answer); 
}

Please refer to ServiceNow documentation for more information:

  • https://docs.servicenow.com/bundle/kingston-application-development/page/script/server-scripting/concept/c_ScriptIncludes.html
  • https://docs.servicenow.com/bundle/kingston-application-development/page/script/ajax/topic/p_AJAX.html

Hi ScienceSoft team,

How will you call a client callable script include from GlideAjax if the name of script include is different from the object name?

Name of script include : ScriptIncludeExampleThree

Name of object : scriptIncludeExampleThree

We know that to invoke the script include from server side, method "gs.include" is utilized. How will be invoke such script include using GlideAjax?

Regards,

Kshitij