gs.days ago method is not working properly
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-14-2023 05:40 AM
As per the below code i am trying to reject approval records if those are old exactly 13 days and still not approved.
But the code is basically rejecting the approvals those are old 13 days or more then 13 days as i am using the operation '>=' but i need ti reject then which is exactly 13 days old not more then that or less then that.
I tried using sign '==' but that is not working , please suggest.
var gr = new GlideRecord("sysapproval_approver");
gr.addQuery('sys_id', '674edc491b79611031f9eca2604bcbd0');
gr.addQuery('sys_created_on','>=',gs.daysAgo(13));
gr.query();
while (gr.next())
{
gr.comments = 'testing by vikas';
gr.state = 'rejected';
gr.update();
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-14-2023 05:58 AM
Hi @VIKAS MISHRA ,
I trust you are doing great.
To reject the approval records that are exactly 13 days old and still not approved, you can modify your code to use the exact 13th-day timestamp instead of the greater than or equal to operator.
var gr = new GlideRecord("sysapproval_approver");
gr.addQuery('sys_id', '674edc491b79611031f9eca2604bcbd0');
gr.addQuery('sys_created_on', '=', gs.daysAgoStart(13));
gr.addQuery('state', 'requested');
gr.query();
while (gr.next()) {
gr.comments = 'testing by vikas';
gr.state = 'rejected';
gr.update();
}
Was this answer helpful?
Please consider marking it correct or helpful.
Your feedback helps us improve!
Thank you!
Regards,
Amit Gujrathi

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-14-2023 08:13 AM - edited ‎04-14-2023 08:13 AM
For queries like this you need to bracket the date. Using an "=" just causes the system to look for records that were created on that exact date/time. If you use the platform UI to do a query for created today you get this
var gr = new GlideRecord("sysapproval_approver");
gr.addQuery('sys_id', '674edc491b79611031f9eca2604bcbd0');
gr.addQuery('sys_created_on', '<=', gs.daysAgo(12));
gr.addQuery('sys_created_on', '>=', gs.daysAgo(13));
gr.query();
while (gr.next()){
gr.comments = 'testing by vikas';
gr.state = 'rejected';
gr.update();
}
So that will get you basically what you want but it will have an issue. That issue is that jobs do not run exactly at the time you set. So if there is an approval created right at 1am in the example above it is likely that the query will not return it because the job will run a few seconds late. Its not an issue if you do not have approvals being created at night. If you would like the code to deal with that do something like this
var start = new GlideDateTime(gs.beginningOfToday());
start.addDaysUTC(-13);
var end = new GlideDateTime(gs.endOfToday());
end.addDaysUTC(-13);
var gr = new GlideRecord("sysapproval_approver");
gr.addQuery('sys_id', '674edc491b79611031f9eca2604bcbd0');
gr.addQuery('sys_created_on', '<=', end);
gr.addQuery('sys_created_on', '>=', start);
gr.query();
while (gr.next()){
gr.comments = 'testing by vikas';
gr.state = 'rejected';
gr.update();
}
Always keep in mind that when you run things like this the system will always use the time zone of the user running the code for finding the date/times. So if you have people in multiple time zones creating approvals you code will reject the ones that are 13 days ago in the time zone of the user who the code is run as. That may or may not be a concern for you.
My last thought to share would be why would you not want to reject approvals that are over the 13 day mark? If they were supposed to be rejected and are still there on day 14 then why wouldn't you reject them? You can just drop the line
gr.addQuery('sys_created_on', '>=', start);
to get anything 13 days or more in age.