- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-20-2022 05:53 AM
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
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-20-2022 06:27 AM
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));
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-20-2022 06:05 AM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-20-2022 06:14 AM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-20-2022 06:24 AM
@Surendra Deshmu You can create the custom object using GlideRecord object and use that wherever you need. like below
ServiceNow Community Rising Star, Class of 2023
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-20-2022 06:27 AM
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));