- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-04-2021 10:02 PM
Im trying to loop the sys_user record but it runs only once.
if the end date is nearing im creating a request for the user .
the problem which im facing is the while loop runs only once.
if i remove the function which is being called, it runs for all the records.
offboard: function() {
var user= new GlideRecord("sys_user");
user.addEncodedQuery("active=true^u_end_dateISNOTEMPTY");
user.query();
while (user.next()) {
var enddate;
enddate = user.u_end_date;
var currentdate = new GlideDate();
var diffSeconds = gs.dateDiff(currentdate, enddate, false);
var remainingdays = parseInt(diffSeconds.replace("00:00:00", ""));
if (remainingdays == 14) {
gs.eventQueue("send.offboarding.remainder", user, user.name, remainingdays);
this.generate_request(user);
}
}
},
generate_request: function(current) {
var cartId = GlideGuid.generate(null);
var cart = new Cart(cartId);
var item = cart.addItem('0acdec651315ea80db7b745fd144b0a1');
cart.setVariable(item, 'name', current.sys_id);
cart.setVariable(item, 'manager', current.manager);
cart.setVariable(item, 'Location', current.location);
cart.setVariable(item, 'Enddate', current.u_enddate);
var rc = cart.placeOrder();
this.REQupdate(rc.sys_id, current);
},
REQupdate: function(rq, current) {
var req = new GlideRecord('sc_request');
req.get(rq);
req.requested_for = current.manager;
req.opened_by = current.manager;
req.update();
var tsk = new GlideRecord('sc_task');
tsk.query('request', rq);
tsk.query();
while (tsk.next()) {
tsk.variables.LastWorkingDay = current.u_end_date;
tsk.short_description = desc;
tsk.update();
}
},
Solved! Go to Solution.
- Labels:
-
Scripting and Coding
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-04-2021 11:53 PM
Glad that it worked.
There must be some exception thrown when the script runs and that might be causing the loop to break.
Please mark my response as correct and helpful and close the question.
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
‎02-04-2021 10:09 PM
it is due to undefined gliderecord object
gs.eventQueue("send.offboarding.remainder", hr_profile, hr_profile.name, remainingdays);
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-04-2021 10:16 PM
OOPs sorry i forgot to update the script.
even though i mentioned the correct gliderecord object it doesnt loop.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-04-2021 10:36 PM
replace it with addquery
var tsk = new GlideRecord('sc_task');
tsk.query('request', rq);
tsk.query();
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-04-2021 10:42 PM
Still the same Pranesh.
Its not looping