Best practice with GlideDate and GlideDateTime

dmitritheengine
Kilo Contributor

Hey folk,

I have a requirement where I need to notify users about an upcoming renewal date.

So, I have a GlideRecord that has a renewal date field that is a GlideDate.

I need to run a daily scheduled job that will check each record in the table, to see if any of the renewal fields are less than 30 days away.

I presume I can use 1000*60*60*24*30 to get the 30 day duration in milliseconds.

Also, I presume I can use gs.now() to get the current date.

1) How can I subtract 30 days from this GlideDate renewal date and properly compare it to today's date?

2) Or how can I add 30 days to today's date and compare it to this GlideDate renewal date?

3) Or perhaps I should do the comparison, then compare it with 30 days?

I could use the community's help in figuring out what the best practice is for doing the above in Fuji... bit rusty on the date comparison.

Thanks,

D

1 ACCEPTED SOLUTION

Abhinay Erra
Giga Sage

It is very easy by setting the filter on your list first and then copy the query and use an addEncodedQuery()


Go thru section 3 in this link


Encoded Query Strings - ServiceNow Wiki


Here I am setting a filter for all the records that have Cab date 30 from now.  


find_real_file.png


Once you set the filter, copy the query as shown


find_real_file.png



Now use it in the Encoded query as shown below


gr.addEncodedQuery('cab_dateRELATIVEEE@dayofweek@ahead@30');


View solution in original post

4 REPLIES 4

Abhinay Erra
Giga Sage

It is very easy by setting the filter on your list first and then copy the query and use an addEncodedQuery()


Go thru section 3 in this link


Encoded Query Strings - ServiceNow Wiki


Here I am setting a filter for all the records that have Cab date 30 from now.  


find_real_file.png


Once you set the filter, copy the query as shown


find_real_file.png



Now use it in the Encoded query as shown below


gr.addEncodedQuery('cab_dateRELATIVEEE@dayofweek@ahead@30');


Thanks Abhinay! That's definitely the most straightforward and clean approach!! (I should have thought of it haha)


Geoffrey2
ServiceNow Employee
ServiceNow Employee

You can do this Date comparison with gs.dateDiff(): GlideSystem Date and Time Functions - ServiceNow Wiki



var diff = gs.dateDiff(gr.u_some_date_field.getDisplayValue(), gs.nowDateTime(), true); // returns seconds


var thirtyDays = 2592000; // Thirty days in seconds


if (diff < thirtyDays) {


      // send notification


}


Abhinay Erra
Giga Sage

It is very useful. I use this almost all the time. You do not need to worry about how complex your conditions could be, because ServiceNow will take care of everything for you by converting it to a query.