The CreatorCon Call for Content is officially open! Get started here.

gs.days ago method is not working properly

VIKAS MISHRA
Tera Contributor

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();
 }
2 REPLIES 2

Amit Gujarathi
Giga Sage
Giga Sage

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



DrewW
Mega Sage

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

 
sys_created_onONToday@javascript:gs.beginningOfToday()@javascript:gs.endOfToday()
 
So you can see its using the beginning of the day and the end of the day.  So you basically need to do the same thing.  But keep in mind that gs.daysAgo will return a date/time and the time it returns will be based on the moment it was run.  So if you do a gs.daysAgo(13) at 3am it will return a date that is 13 days in the past and the time will be 3am.  So the simple solution to your problem is to bracket the date.  So if the below is run at 1am it will return the records created between 2023-04-01 01:00:00 and 2023-04-02 01:00:00 so that will cover most of 2023-04-01.
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.