Date diff in seconds

servicenow14710
Tera Expert

Hello developers, I need to implements a scenario where i have to send reminder email week before a date field on UI. 

  • i'm getting UI date field 
  • var effectiveDate = new GlideDateTime(current.variables.getValue('effective_date'));
  • effectiveDate .addWeeksLocalTime(-1);
  • i have added addWeeksLocalTime so a week before effectiveDate.
  • Now i need sugggestions as how can i get this time difference(from effectiveDate a week before) in seconds so i can give this to timer and craete an event or if there is any other way i can implement this scenario. PLEASE provide suggestions, Any idea is appreciated, Thanks!
 
 
 
4 REPLIES 4

Juhi Poddar
Kilo Patron

Hello @servicenow14710 

 

The effectiveDate variable is being modified to represent a date one week before the original effective date, which means the original value from the catalog variable is lost. To avoid this mistake, we can store the effective date in two separate variables: one for the original date and another for the reminder date.

var effectiveDate = new GlideDateTime(current.variables.getValue('effective_date')); // Original effective date
var reminderDate = new GlideDateTime(current.variables.getValue('effective_date')); // Copy of the effective date
reminderDate.addWeeksLocalTime(-1); 

// Calculate the time difference in seconds
var timeDifferenceInSeconds = gs.dateDiff(effectiveDate, reminderDate, true);
gs.log(timeDifferenceInSeconds);

Now you can use this timeDifferenceInSeconds value to set your timer or schedule a reminder event accordingly.

 

Alternatively, you could use a scheduled job to check for records that need reminders sent within the upcoming week. This way, you can avoid setting a timer directly and instead rely on a daily or hourly scheduled script. This is usually more efficient and reliable when dealing with multiple records.

 

"If you found my answer helpful, please give it a like and mark it as the accepted solution. It helps others find the solution more easily and supports the community!"

 

Thank You

Juhi Poddar

 

Hello @Juhi Poddar : Thanks for the response, as replied instead of giving it to timer we can use scheduled job so how can i implement this here as giving the timer doesnt work as suppose the effective date is on 10 oct 2024

as per our logic it should be 1 week before 10th oct say, on 3rd oct 2024 the email should trigger . After usimg this code i got timer in sec as -60,4800 . Can i do this via scheduled job, Any suggestions, Thanks!

 

Hello @servicenow14710 

 

To implement this using a scheduled job, create a job that runs daily. This job will check for records where the reminder date matches the current date. If matching records are found, it will trigger an event to send a notification.

You can refer to the post "Schedule Job Script to Call Event" for a detailed understanding of the complete flow. Additionally, the record details can also be passed through the event and can be used to customize the notification.

 

"If you found my answer helpful, please give it a like and mark it as the accepted solution. It helps others find the solution more easily and supports the community!"

 

Thank You

Juhi Poddar

Rohini S Sane
Tera Guru

@servicenow14710 , Could you please try below code.

 

var effectiveDate = new GlideDateTime(current.variables.getValue('effective_date'));

var rdate = new GlideDateTime(current.variables.getValue('effective_date'));

rdate.addWeeksLocalTime(-1);

var dur = GlideDateTime.subtract(rdate, effectiveDate);
dur.getNumericValue();
gs.log(dur);
gs.eventQueueScheduled("<event_name>", object, parm1, parm2, dur);//to generate event on specified time
 
 
Please Mark Helpful.
 
Regards,
Rohini S Sane