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 use query records with an array?

Sathwik1
Tera Expert

I am having an array with these values..

var arr = [10, 20, 30, 40];

var gr = new GlideRecord('incident');

gr.addQuery('number', 'INC0003440');

if(gr.next())

{

var getCode = gr.code;

if (arr.indexOf(getCode) != -1) {                     ----> Instead of written this indexOf .. is there any other alternate way?

arr2.push(gr.number)

}

1 REPLY 1

ryan_pope
Mega Guru

I think indexOf will be the easiest and quickest way to do what you're looking for.

Otherwise, you could loop through the array looking for a match. It also looks like you might not be executing the query.

var arr = [10, 20, 30, 40];

var incidentGR = new GlideRecord('incident');
incidentGR.addQuery('number', 'INC0003440');
incidentGR.query();
var arr2 = [];
if(incidentGR.next()){
   var getCode = gr.code;
   if (arr.indexOf(getCode) > -1) { 
      arr2.push(gr.number);
   }

OR

var arr = [10, 20, 30, 40];

var incidentGR = new GlideRecord('incident');
incidentGR.addQuery('number', 'INC0003440');
incidentGR.query();
var arr2 = [];
if(incidentGR.next()){
   var getCode = gr.code;
   for(code in arr) {
      if(getCode == arr[code]) {
         arr2.push(incidentGR.number);
         break;
      }
   }
}