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.

How to fetch array if it contains particular string?

Gayathri5
Tera Guru

Hi All,

 

I have array it contains

var allarr =[];

var incarr =[];

allarr=INCXXXX, INCXXXX, PRBXXXX, SCTXXXXX, PRBXXXX;

 

now i am checking if allarr contains INC separate it in another array which is not working

for(var x in allarr){

gs.log("Inside for loop:"+allarr);//going here

if(allarr[x].indexof('INC') > -1){

incarr.push(allarr[x]);

gs.log("Inside if:"+incarr); //not going inside if loop

}

}

 

after going inside if loop i want to separate all incidents in one array, can any one help me pls

 

Regards,

Gayathri

 

17 REPLIES 17

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

try this

var incarr = [];

var allarr = ['INCXXXX', 'INCXXXX', 'PRBXXXX', 'SCTXXXXX', 'PRBXXXX'];

for(var x in allarr){
	gs.info("Inside for loop:"+allarr);//going here
	if(allarr[x].indexof('INC') > -1){
		incarr.push(allarr[x].toString());
		gs.info("Inside if:"+incarr); //not going inside if loop
	}
}

Regards
Ankur

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

Hi Ankur,

 

It is not separating incidents, how to fetch only incidents from that allrr array?

 

Regards,

gayathri

Hi,

this worked for me

var incarr = [];

var allarr = ['INCXXXX', 'INCXXXX', 'PRBXXXX', 'SCTXXXXX', 'PRBXXXX'];

for(var x in allarr){
	if(allarr[x].toString().indexOf('INC') > -1){
		incarr.push(allarr[x].toString());
	}
}

gs.info(incarr);

Output:

find_real_file.png

Regards
Ankur

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

In my email script it is not working, let me try once in background script.