Need to push object in an array from loop

Surendra Deshmu
Tera Contributor

Hello Team,

 

Is there any way I can push Glide object as a value in an array-

 

Eg.

 

var users = [];
var a = new GlideRecord('sys_user');
a.addQuery('active',true);
a.setLimit(5);
a.query();
while(a.next()){

//*******************HERE I HAVE DOUBT

users.push(a);

//*******************

}
for(var index in users){
var inc = new GlideRecord('incident');
inc.initialize();
inc.caller_id = users.data[index].sys_id;
if(inc.insert()){
gs.info('INCIDENT is created '+inc.number);
}
}

 

For now there is only 1 value storing in an array, as it is object and not string I can't even use toString(), is there any way I can store all object records from loop in an array

1 ACCEPTED SOLUTION

Brad Bowman
Kilo Patron
Kilo Patron

Here's an example of how you can push an entire GlideRecord object into an array:

var users = [];
var obj = {};
var a = new GlideRecord('sys_user');
a.addQuery('active',true);
a.setLimit(5);
a.query();
while (a.next()) {
	for (var key in a) {
		obj[key] = a[key].toString();
	}
	users.push(obj);
}
gs.print(JSON.stringify(users));

View solution in original post

7 REPLIES 7

mdash
Giga Guru

Why not shift the loop inside the while() body?

//var users = [];
var a = new GlideRecord('sys_user');
a.addQuery('active',true);
a.setLimit(5);
a.query();
while(a.next()){

var inc = new GlideRecord('incident');
inc.initialize();
inc.caller_id = a.sys_id; //you have access to the whole Glide Object here
if(inc.insert()){
gs.info('INCIDENT is created '+inc.number);
}
}



Please mark my answer as helpful/correct depending on its impact.

Thanks

Hello mdash, 

 

Thanks for replying.

Posted script is just an example script.

Basically I'm getting comma separated user IDs as parameter in my util function and I'm dividing it into 2 different arrays of active and inactive user objects, and based on some logic I want to update these user records, so thats why I want array with user object stored-

 

inactiveUsers[index]).setValue('locked_out', false);
inactiveUsers[index]).setValue('active', true); 

 

So, thats why I wanted these objects to be stored in an array, but only one value is storing.

jaheerhattiwale
Mega Sage
Mega Sage

@Surendra Deshmu You can create the custom object using GlideRecord object and use that wherever you need. like below

 

var users = [];
var a = new GlideRecord('sys_user');
a.addQuery('active',true);
a.setLimit(5);
a.query();
while(a.next()){

//*******************HERE I HAVE DOUBT

var object = {
    "sys_id" : a.sys_id.toString(),
    "user_name": a.user_name.toString()
}
users.push(object);

//*******************

}


for(var index in users){
var inc = new GlideRecord('incident');
inc.initialize();
inc.caller_id = users[index].sys_id.toString();
if(inc.insert()){
gs.info('INCIDENT is created '+inc.number);
}
}
 
Please mark as correct answer if this solves your issue.
Please mark the answer as correct or helpful based on impact
ServiceNow Community Rising Star, Class of 2023

Brad Bowman
Kilo Patron
Kilo Patron

Here's an example of how you can push an entire GlideRecord object into an array:

var users = [];
var obj = {};
var a = new GlideRecord('sys_user');
a.addQuery('active',true);
a.setLimit(5);
a.query();
while (a.next()) {
	for (var key in a) {
		obj[key] = a[key].toString();
	}
	users.push(obj);
}
gs.print(JSON.stringify(users));