- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-27-2022 10:36 PM
Hi,
I am using a scheduled job in which I am getting the records from a table and performing action on every record in while loop. Now I want to set some delay for every iteration in while loop, how to achieve it?
var csa = new GlideRecord('cmdb_ci_cloud_service_account');
csa.addEncodedQuery('datacenter_type!=null');
csa.query();
while(csa.next()){
var scheduleConfig = new global.CloudDiscoveryScheduleConfig();
var accountSysId = csa.sys_id + '';
var result = {};
try {
result = scheduleConfig.discoverDatacenters(accountSysId);
} catch(err) {
result.error = err;
}
}
Thanks
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-28-2022 12:19 AM
we do nothing in that while as it will just iterate for 10 seconds
That's how the delay would be generated
If my response helped you please mark it correct to close the question so that it benefits future readers as well.
Regards
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
‎01-27-2022 10:43 PM
The easiest solution is to try gs.sleep(milliseconds);
Put the below function in a script include if you want to reuse it too often and you can call it wherever required with the required delay in milliseconds.
sleep: function(duration) {
gs.sleep(duration);
},
Mark my answer as correct if this helps you in any way
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-27-2022 10:49 PM
Hi
I have gone through few community blogs before posting the question and I found that gs.sleep() is not good to use in few of them. Is it okay to use?
If it is okay to use, can I give that directly at the end of my script without defining that in any of the script includes?
var csa = new GlideRecord('cmdb_ci_cloud_service_account');
csa.addEncodedQuery('datacenter_type!=null');
csa.query();
while(csa.next()){
var scheduleConfig = new global.CloudDiscoveryScheduleConfig();
var accountSysId = csa.sys_id + '';
var result = {};
try {
result = scheduleConfig.discoverDatacenters(accountSysId);
} catch(err) {
result.error = err;
}
gs.sleep(5000);
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-27-2022 11:23 PM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-27-2022 11:40 PM
Hi
Thank you. I need to run this once in a day. As we don't know the number of records we get as per the query condition we have given at the initial part of the script, we can't set it to run periodically, because it may run again for the same record on the same day right?