- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-26-2020 06:55 AM
Hi I am new to servicenow and wanted to create an array of sys_id with gliderecord
This is my current code using a single sys_id but what if I needed to do say 10 sys_id. How would i put that into an array? What is the best way to do this?
Solved! Go to Solution.
- Labels:
-
Scripting and Coding
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-29-2020 09:28 AM
Try below:
var arr = [];//pass your sys_id's comma separated
for(var i=0; i<arr.length; i++){
var gr = new GlideRecord('sys_data_source');
gr.addQuery('sys_id',arr[i]);
gr.query();
if(gr.next()){
gr.last_run_datetime = '';
gr.update();
}
}
Please mark my response as correct and helpful if it helped solved your question.
-Thanks
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-02-2020 11:50 AM
Aman I actually tried your way first but couldn't get it to work.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-29-2020 09:30 AM
Hi,
you can use below query for array
var array = ['sys_id1','sys_id2'];
grScheduledImport.addQuery('sys_id', 'IN', array);
Mark ✅ Correct if this solves your issue and also mark 👍 Helpful if you find my response worthy based on the impact.
Thanks
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-29-2020 11:02 AM
Efficient and Optimized
Pro Tip: Use updateMultiple() for Maximum Efficiency!
var arr = ['sys_id1','sys_id2','sys_id3'];//pass your sys_id's comma separated
var gr = new GlideRecord('sys_data_source');
gr.addQuery('sys_id','IN',arr);
gr.setValue('last_run_datetime','');
gr.updateMultiple();
Mark Correct if this solves your issue and also mark Helpful if you find my response worthy based on the impact.
Aman Gurram
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-29-2020 01:23 PM
Hi,
I let you an example about incident query and how to create an array made from GrideRecord sys_id's or GlideRecord fields:
var array = [];
var gr = new GlideRecord('incident');
gr.addQuery('caller_id',sys_id_value);
gr.query();
while(gr.next()) {
array.push(gr.sys_id);
}
//If you want to use for another query you can use it as follow
var gr1 new GlideRecord('incident');
gr1.addEncodedQuery('sys_idIN' + array.join());
gr1.query();
In this case, you only need to iterate over GlideRecord and store with push all the attributes that you need.
If it was helpful, please give positive feedback.
Thanks,
☆ Community Rising Star 22, 23 & 24 ☆