Need help on date calculations

Kenny_vl
Tera Contributor

I have a requirement to trigger a notification on a contract's end date minus the notice period minus 30 days.
I am free to implement this however, so I figured to create a flow for this which runs on a daily schedule.
However, I am having difficulties to calculate the date.
This calculation needs to be 30 days before a contract's notice period.
The contract has the field ast_contract.ends set and a custom field (type duration) ast_contract.u_notice_period (for example 90 days), in this scenario the notification would trigger 120 days before the contract's end date.

 

I created a flow which runs on a daily schedule, I used a Lookup records flow action to retrieve all active contracts, followed by a for each action to loop through the records.

This is where I'm stuck. I can't figure out how to calculate if today is the date where the notification should be triggered. So I am looking for help in scripting ast_contract.ends-ast_contract-u_notice_period-30 days

 

Any input/advice/suggestions would be appreciated. I've looked at the GlideDateTime API's and the Durationcalculator script include but I just don't understand the concept with the duration field.

4 REPLIES 4

Ankur Bawiskar
Tera Patron
Tera Patron

@Kenny_vl 

You will require date/time or date field to compare and it won't work with Duration field.

You can't compare date/time or date with Duration field.

you can use a custom flow action which adds 30 days to a given date and it will return that new date.

then in your flow you can iterate over records and then see the date comparison

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

Hi Ankur, 

When I print out the duration field, it is returning as a Glidedatetime. Wouldn't I be able to work with that ? 

var grAstContract = new GlideRecord('ast_contract');
if (grAstContract.get('437526aa87f3e210dd0fcbb80cbb3587')) {

gs.print(grAstContract.ends.getValue());
gs.print(grAstContract.u_notice_period);

}
*** Script: 2025-09-09
*** Script: 1970-01-02 00:00:00

 

@Kenny_vl 

duration field holds the time interval from 1st Jan 1970 and it doesn't hold date/time

So you can directly compare ends field with your newer date/time value and then handle the logic.

AnkurBawiskar_0-1759237283842.png

 

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

@Kenny_vl 

I shared approach here recently, check that and enhance it as per your requirement

Send a reminder email before End date is due 

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader