Using Script Include in Workflow

DPrasna
Tera Contributor

Hello All,

I have the below script include which I use on On change catalog client script

SI::

 

getCI: function() {

		var getAppname = this.getParameter('sysparm_app_name');
		var getsysid = this.getParameter('sysparm_sysid');
		var grgetCI = new GlideRecord('cmdb_ci_service');
		grgetCI.addEncodedQuery('u_application_name!=NULL');
		grgetCI.addQuery('u_application_name',getAppname);			
		grgetCI.query();
		var CIlist = "";
		while(grgetCI.next()){				
			if(CIlist == ''){
				CIlist = grgetCI.used_for.toString();
			}
			else
			{
				CIlist = CIlist + ", "+ grgetCI.used_for.toString();
			}
		}
		var answer = CIlist;	
		//JSON.stringify(answer);
		return JSON.stringify(answer);
		//return answer;

	},

 

 

And on the Client script I pass the parameter getAppname - with g_form.product variable

The above script include returns the value of Used for - Prod/Dev/test from Business Service

 

Now, I have the same logic to be applied from workflow and I want the same return values from the script include that would return the Used for of a Business Service when we pass the current.variables.product field from workflow

How to achieve the same?

 

TIA

@Ankur Bawiskar @AnveshKumar M @Tai Vu @Danish Bhairag2 

7 REPLIES 7

Danish Bhairag2
Tera Sage
Tera Sage

Hi @DPrasna ,

 

U can create a new Script include with client callable as false (Deactivate the existing one or delete it)& then paste the same code which u have shared above. The only difference would be this

 

getCI: function(appName,sysID) {

		var getAppname = appName;
		var getsysid = sysID;
		var grgetCI = new GlideRecord('cmdb_ci_service');
		grgetCI.addEncodedQuery('u_application_name!=NULL');
		grgetCI.addQuery('u_application_name',getAppname);			
		grgetCI.query();
		var CIlist = "";
		while(grgetCI.next()){				
			if(CIlist == ''){
				CIlist = grgetCI.used_for.toString();
			}
			else
			{
				CIlist = CIlist + ", "+ grgetCI.used_for.toString();
			}
		}
		var answer = CIlist;	
		//JSON.stringify(answer);
		return JSON.stringify(answer);
		//return answer;

	},

 

& from ur workflow u can call the script include using this command.

 

var response = new ScriptIncludeName().getCI(current.variable.appName,current.sysID);//please use proper script & variable names as per your requirement

 

Thanks,

Danish

 

 

Mayur2109
Kilo Sage
Kilo Sage

Hi @DPrasna ,

 

Refer below community post.

https://www.servicenow.com/community/developer-forum/how-do-you-include-quot-script-includes-quot-in... 

 

Please check and Mark Helpful and Correct if it really helps you.

Regards,
Mayur Shardul

Ankur Bawiskar
Tera Patron
Tera Patron

@DPrasna 

you need to update the function as this so that it works from both client and server

getCI: function(getAppname1,getsysid1) {

var getAppname = this.getParameter('sysparm_app_name') || getAppname1;
var getsysid = this.getParameter('sysparm_sysid') || getsysid1;

var grgetCI = new GlideRecord('cmdb_ci_service');
grgetCI.addEncodedQuery('u_application_name!=NULL');
grgetCI.addQuery('u_application_name',getAppname);
grgetCI.query();
var CIlist = "";
while(grgetCI.next()){
if(CIlist == ''){
CIlist = grgetCI.used_for.toString();
}
else
{
CIlist = CIlist + ", "+ grgetCI.used_for.toString();
}
}
var answer = CIlist;
//JSON.stringify(answer);
return JSON.stringify(answer);
//return answer;

},

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Thanks for the reply. How to call the SI from WF?

var cc = new YourScriptIncludeName();
var ci_list = cc.getCI(current.variables.product);

Is this the right way?