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.

Return sys id in array script include

DB1
Tera Contributor

Hello All,

 

I have the following Script Include where I need to return multiple sys ids in array.

Could you please help with how to achieve the same?

SI:

 

getCI: function(getAppname) {

		var grgetCI = new GlideRecord('cmdb_ci_service');
		grgetCI.addEncodedQuery('u_client_solution!=NULL');
		grgetCI.addQuery('u_client_solution',getAppname);			
		grgetCI.query();
		var CIlist = "";
		var myStr = "";
		var SYSIDlist = [];
		while(grgetCI.next()){	

			if(CIlist == ''){
				CIlist = grgetCI.used_for.toString();
				//myStr = grgetCI.name;
				SYSIDlist.push(grgetCI.sys_id.toString());
			}
			else
			{
				CIlist = CIlist + ", "+ grgetCI.used_for.toString();
				//myStr = myStr + ", " + grgetCI.name;
				SYSIDlist = SYSIDlist + ", " + grgetCI.sys_id.toString();
			}
		}
		var answer = CIlist + "\n" + SYSIDlist;		
		gs.log('SI answer = ' +  + "Sys ID 1 " +SYSIDlist[0] + "Sys ID 2 " +SYSIDlist[1]);
		//JSON.stringify(answer);
		return JSON.stringify(answer);	
		//return answer;

	},

TIA

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

10 REPLIES 10

Hi @DB1 

It is working fine in my PDI.

SaurabhGupta_0-1705854111301.png

Can you please check if log prints within while meaning if condition is returning some objects.

getCI: function(getAppname) {
    var grgetCI = new GlideRecord('cmdb_ci_service');
    grgetCI.addNotNullQuery('u_client_solution');
    grgetCI.addQuery('u_client_solution', getAppname);
    grgetCI.query();
    var CIlist = [];
    var SYSIDlist = [];
    while (grgetCI.next()) {
gs.info("I am inside while");
        CIlist.push(grgetCI.used_for + "");
        SYSIDlist.push(grgetCI.getUniqueValue());
    }
if(SYSIDlist.length>0)
    gs.info('SI answer = ' +"Sys ID 1 " + SYSIDlist[0] + "Sys ID 2 " + SYSIDlist[1]+" length of the array: "+SYSIDlist.length);
    var answer = CIlist.join(",") + "\n" + SYSIDlist.join(",");
    return answer;
},
C

If the provided solution meets your needs, kindly consider marking it as helpful and accepting it as the solution. This helps others who may have similar questions.

 


Thanks and Regards,

Saurabh Gupta

Hi @DB1 

 

C

If the provided solution meets your needs, kindly consider marking it as helpful and accepting it as the solution. This helps others who may have similar questions.


Thanks and Regards,

Saurabh Gupta

Bert_c1
Kilo Patron

 

//				SYSIDlist = SYSIDlist + ", " + grgetCI.sys_id.toString();
				SYSIDlist.push(grgetCI.sys_id.toString());

 

you need the above in the "else" part. If you don't want to use Saurabh's version.

DB1
Tera Contributor

Still undefined while I try to print the log

	getCI: function(getAppname) {

		var grgetCI = new GlideRecord('cmdb_ci_service');
		grgetCI.addEncodedQuery('u_client_solution!=NULL');
		grgetCI.addQuery('u_client_solution',getAppname);			
		grgetCI.query();
		var CIlist = "";
		var myStr = "";
		var SYSIDlist = [];
		while(grgetCI.next()){	

			if(CIlist == ''){
				CIlist = grgetCI.used_for.toString();
				//myStr = grgetCI.name;
				SYSIDlist.push(grgetCI.getUniqueValue());
			}
			else
			{
				CIlist = CIlist + ", "+ grgetCI.used_for.toString();
				//myStr = myStr + ", " + grgetCI.name;
				//SYSIDlist = SYSIDlist + ", " + grgetCI.sys_id.toString();
				SYSIDlist.push(grgetCI.sys_id.toString());
			}
		}
		var answer = CIlist + "\n" + SYSIDlist.join(",");		
		gs.log('SI answer ='+ CIlist +SYSIDlist  + "Sys ID 1 " +SYSIDlist[0] + "Sys ID 2 " +SYSIDlist[1]);
		//JSON.stringify(answer);
		return JSON.stringify(answer);	
		//return answer;

	},

sysIDlist[0] or [1] returns undefined

Hi @DB1,

Use below script by using json stringify 

gs.log('SI answer = ' + CIlist + JSON.stringify(SYSIDlist) + " Sys ID 1 " + SYSIDlist[0] + " Sys ID 2 " + SYSIDlist[1]);

 

Mark it as helpful and solution proposed if it serves your purpose.
Thanks,
Anand