Return another value from Script Include

DB1
Tera Contributor

Hello All,

 

I have the following script include which returns Output as "Development/Production". However I want to return the name/Sys ID of the CI. How do I get the same from the below script:

 

var CheckDevProdCIenv = Class.create();
CheckDevProdCIenv.prototype = Object.extendsObject(AbstractAjaxProcessor, {

	getCI: function() {

		var getAppname = this.getParameter('sysparm_app_name');
		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(); //returns the env of the CI. I also want to return the name and sys id of the CI
			}
			else
			{
				CIlist = CIlist + ", "+ grgetCI.used_for.toString();// returns the env of the CI. I also want to return the name and sys id of the CI
			}
		}
		var answer = CIlist;	
		//JSON.stringify(answer);
		return JSON.stringify(answer);
		//return answer;

	},

	type: 'CheckDevProdCIenv'
});

@Ankur Bawiskar @Dr Atul G- LNG @Maik Skoddow 

3 REPLIES 3

maroon_byte
Mega Sage

Depending upon how CIs are related to Service/Application, you will need to script accordingly. Do you have an application/service field on the CI record? or do you have CIs related to application/services using the CI relationship table?

OlaN
Giga Sage
Giga Sage

Hi,

It seems this method returns one or more results, depending on how many CIs match the query.

If you want to retrieve multiple data from the GlideRecord, I would recommend you return a array or Object, which can be parsed/stringified using the JSON method already in place.

You can add additional data in a similar way that's already in the script, but I always recommend using the getValue method.

Something like this might help you forward:

getCI: function() {

	var getAppname = this.getParameter('sysparm_app_name');
	var grgetCI = new GlideRecord('cmdb_ci_service');
	grgetCI.addEncodedQuery('u_application_name!=NULL');
	grgetCI.addQuery('u_application_name', getAppname);			
	grgetCI.query();
	
    var ciData = {
        sysID : [],
        name : [],
        environment : []
    };

	while(grgetCI.next()){				
		
		ciData.sysID.push(grgetCI.getUniqueValue());
        ciData.name.push(grgetCI.getValue('name'));
        ciData.environment.push(grgetCI.getValue('used_for'));
	}
    return JSON.stringify(ciData);
},

Bert_c1
Kilo Patron

Hi DB1,

 

You can add lines to declare two new strings, and include logic to build those like your "CIlist". Then append the 3 strings to be returned

 

var getAppname = this.getParameter('sysparm_app_name');
var grgetCI = new GlideRecord('cmdb_ci_service');
//		grgetCI.addEncodedQuery('u_application_name!=NULL');
//		grgetCI.addQuery('u_application_name',getAppname);
grgetCI.setLimit(3);		
grgetCI.query();
var CIlist = "";
var myStr = "";
var SYSIDlist = "";
while(grgetCI.next()){				
	if(CIlist == ''){
		CIlist = grgetCI.used_for.toString(); //returns the env of the CI. I also want to return the name and sys id of the CI
		myStr = grgetCI.name;
		SYSIDlist = grgetCI.sys_id.toString();
	}
	else
	{
		CIlist = CIlist + ", "+ grgetCI.used_for.toString();// returns the env of the CI. I also want to return the name and sys id of the CI
		myStr = myStr + ", " + grgetCI.name;
		SYSIDlist = SYSIDlist + ", " + grgetCI.sys_id.toString();
	}
}
var answer = CIlist + "\n" + myStr + "\n" + SYSIDlist;	
//JSON.stringify(answer);
//	return JSON.stringify(answer);
gs.info(answer);

(Modified version of your script logic for testing in scripts background.)