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

Tai Vu
Kilo Patron
Kilo Patron

Hi @DPrasna 

You can clone your function to a Script Include that has the Client Callable Unchecked.

Sample.

var CLCatalogItemUtils = Class.create();
CLCatalogItemUtils.prototype = {
    initialize: function() {},

    getCI: function(app_name) {
		var CIlist = [];
        var grgetCI = new GlideRecord('cmdb_ci_service');
        grgetCI.addEncodedQuery('u_application_name!=NULL');
        grgetCI.addQuery('u_application_name', app_name);
        grgetCI.query();
        while (grgetCI.next()) {
			CIlist.push(grgetCI.getValue('used_for'));
        }
        return CIlist;
    },

    type: 'CLCatalogItemUtils'
};

 

Then call the function in the workflow.

var CIlist = new CLCatalogItemUtils().getCI(current.variable.<your_variable_app_name>.toString()); //replace your variable name

 

Cheers,

Tai Vu

DPrasna
Tera Contributor

Hi @Tai Vu Thanks for the reply,

I tried to create a New Script Include with Client callable unchecked.
But on the other client callable script include I had passed the getappname parameter from client side as follows

SI:var getAppname = this.getParameter('sysparm_app_name');
var getsysid = this.getParameter('sysparm_sysid');

 

Client: var prod = g_form.getReference('product', myFunc);
function myFunc(prod){
var ga = new GlideAjax('CheckDevProdCIenv');
ga.addParam("sysparm_name", "getCI");
ga.addParam("sysparm_app_name", prod.u_application_name);

 

Now on the new script include(Client callable unchecked) I am trying to pass the parameter as follows:

var CIlist = new CheckDevProdCIenv_WF().getCI(current.variables.product.u_application_name.toString());

 

This returns empty value on the log when I checked

As mentioned above I am trying to pass the application name to the query and return the used for environments like Dev/ Prod / Test. I am not sure which parameter to pass there and How?

AnveshKumar M
Tera Sage
Tera Sage

Hi @DPrasna 

You can not directly use this function as is, but the same logic can be used. You can create another wrapper method which can be called by passing the argument directly. Like the one below.

 

getCI: function() {
		var getAppname = this.getParameter('sysparm_app_name');
		var getsysid = this.getParameter('sysparm_sysid');
		var answer = this.getCIWF(getAppname, getsysid);
		return JSON.stringify(answer);
	},

	getCIWF: function(getAppname, getsysid) {
		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;	 // Returns ths CI List as comma seperated string
	},

 

So you can use getCI method from your onChange catalog client script. And you can also use getCIWF from script in  workflow activity like below.

 

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

 

And you can split the ci_list variable into an array or whatever the you need.

 

Please mark my answer helpful and accept as a solution if it helped 👍✔️

Thanks,
Anvesh