Trigger Workflow Timer 5 days before a specific Date/Time given in RITM variable value

subhadeep1618
Tera Guru

Hello Everyone,

Need one help.

I have a requirement to trigger a timer if current date is 5 days or less than estimated_end_date (which is a RITM variable).

Now this date is editable, that is, users can edit the variable value anytime.

In the workflow timer script, I have written the below code, but sadly the timer is not firing.

Can you please tell me where I am going wrong here.

Thanks in advance.

var diff = gs.dateDiff(gs.nowDateTime(), current.variables.estimated_end_date, true);
var days = diff / (24 * 60 * 60);
if (days <= 5) //if no. of days is 5 or less
    answer = 0;
else
    answer = 1000000; //an arbitrary large value if no. of days is more than 5

 


Please mark this post as a solution and also as helpful, if this resolves your issue or query.

Thanks,
Subhadeep Ghosh.
11 REPLIES 11

Maddysunil
Kilo Sage

@subhadeep1618 

Script seems to be correct, Can you put some log and check :

 

 

var diff = gs.dateDiff(gs.nowDateTime(), current.variables.estimated_end_date, true);
var days = diff / (24 * 60 * 60);

gs.info("Days difference: " + days);

if (days <= 5) {
    answer = 0;
    gs.info("Timer should be set.");
} else {
    answer = 1000000;
    gs.info("Timer should not be set.");
}

 

 

Please Mark āœ…Correct if this solves your query and also mark šŸ‘Helpful if you find my response worthy based on the impact.

 

Thanks

Amit Gujarathi
Giga Sage
Giga Sage

HI @subhadeep1618 ,
I trust you are doig great.
Here's an updated version of your script with added debugging statements:

var estimatedEndDate = new Date(current.variables.estimated_end_date); // Parse estimated_end_date as Date object
gs.log("Estimated End Date: " + estimatedEndDate); // Log estimated end date for debugging

var diff = gs.dateDiff(gs.nowDateTime(), estimatedEndDate, true);
var days = diff / (24 * 60 * 60);
gs.log("Days difference: " + days); // Log days difference for debugging

if (days <= 5) {
    answer = 0;
    gs.log("Timer set to 0."); // Log timer set to 0 for debugging
} else {
    answer = 1000000;
    gs.log("Timer set to 1000000."); // Log timer set to 1000000 for debugging
}

Was this answer helpful?


Please consider marking it correct or helpful.


Your feedback helps us improve!


Thank you!


Regards,


Amit Gujrathi



Bidduam
Tera Guru

@subhadeep1618 I'd suggest moving it into a flow, if it is not already. 

Then all you need to do is add a wait for duration of time from the flow logic, similar to this:

Bidduam_1-1708401468240.png

 

Bidduam_0-1708401228858.png

 

Ankur Bawiskar
Tera Patron
Tera Patron

@subhadeep1618 

user can change the variable anytime? That's not good.

How would the workflow determine this?

Please discuss with your customer when should this variable be edited and once filled make it readonly so that it updates only once

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