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.

Split array and use elements in GlideRecord

Puru2
Tera Contributor

Dear All,

I want to split the array and use its stored elements in GlideRecord query.

Below mentioned is the code snippet :

// str contains 2 sys_ids  as  -> [*sys_id_1*, *sys_id_2*] 
for(var i=0;i<str.length;i++)
{

var rec = new GlideRecord('core_country');
rec.addQuery('sys_id',str[i]);  // trying to query a record via first sys_id
rec.query();
while(rec.next())
{

// perform action

}

 

Please  suggest me any possible corrections in this code snippet.

 

Best Regards,

Puru

 

 

8 REPLIES 8

Dubz
Mega Sage

This will return all records in the str array, you can then iterate through them in a while loop.

var str = [];
var rec = new GlideRecord('core_country');
rec.addEncodedQuery('sys_idIN' + str);  
rec.query();
while(rec.next())
{

// perform action

}

Puru2
Tera Contributor

Hello David ,

 

str  is an array which contains 5 or 6 sys_id  of Countries (records of core_country table). We want to query these sys_id one by one using a for loop :

for(var i=0;i<str.length;i++)
{

var rec = new GlideRecord('core_country');
rec.addQuery('sys_id',str[i]);  

rec.query();
while(rec.next())
{

  // perform action
}

 

Issue with above code snippet : even if we put str[i] as a query condition, it doesn't query single index , but it takes whole array.

For e.g.  str (as an array) has following data --> [zxc234xxxxxxxxx, hfl34xxxxxxxxxxxx, 790uhuhxxxxxxxxxxx]
Instead of querying   str[0], it queries whole array.

Please let me know any possible suggestions/corrections.

Best Regards,

Puru

I'm not sure why querying them individually is so important.  By using the while(rec.next()) you still iterate over them one at a time, the only thing you lose by doing this is possibly the return order.

You can still perform whatever action you intend.

Here's a working example;

var str = ['1ed87fdedbd2c748f826fa38bf961963',1ed87fdedbd2c748f826fa38bf961911','1ed87fdedbd2c748f826fa38bf961922','1ed87fdedbd2c748f826fa38bf961933']; // really the var name should be something like countryArr;
var core_country = new GlideRecord('core_country');
core_country.addEncodedQuery('sys_idIN' + str.toString());
core_country.query();
while(core_country.next()){
// perform your action
}

ChrisBurks
Giga Sage

A few of things. 

1) You mentioned a string and also use in your example str. Is the variable really a string or a real array? If it's a string that looks like an array ( "[sys_id1,sys_id2]" ) the results to str[0] will be completely different than a real array that contains sys_ids as strings or variables  (["sys_id1","sys_id2"] or [sys_id1, sys_id2]). 

Remember in Javascript a string can be mis-interpreted as an array because bracket notation can be used on a string. For example

var str = "[zxc234xxxxxxxxx,hfl34xxxxxxxxxxxx]";

str[0]; 

//output will be "["

as opposed to 

var str = [zxc234xxxxxxxxx,hfl34xxxxxxxxxxxx];

str[0];

//output will be "zxc234xxxxxxxxx"

Notice the quotes in the first example surrounding and including the brackets when str is defined.

If I wanted to use the first example as an array I would need to do a couple of things to make it act as an array. One, is to remove the brackets and then split the string to make it an array.

var str = "[zxc234xxxxxxxxx,hfl34xxxxxxxxxxxx]"

str = str.replace(/\[|\]\s/g, "").split(",");

str[0];

//output will be "zxc234xxxxxxxxx"

 

2) if you only need to get one record at a time and you can isolate the sys_id then just use the .get() method on the GlideRecord:

   i.e. 

var record = new GlideRecord('core_country');
record.get(<sys_id>); // or in your case record.get(str[0]);

 

3) Instead of a for loop why not use built-in array methods like .forEach(). 

i.e.

var str = "[zxc234xxxxxxxxx, hfl34xxxxxxxxxxxx, 790uhuhxxxxxxxxxxx]";
//regex to find "[","]",or " " and replace with blank and then split string by comma to //an array
str = str.replace(/\[|\|\]\s/g,"").split(","); 

str.forEach(function(id){
    var record = new GlideRecord('core_country');
    record.get(id);

    if(record.getUniqueValue()){
       //perform actions
    }
});