Return another value from Script Include
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-10-2024 07:04 AM
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'
});
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-10-2024 07:14 AM
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?

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-10-2024 07:49 AM
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);
},
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-10-2024 07:52 AM
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.)