We're reclaiming inactive PDIs to keep them available for active builders. Learn what's changing, who's affected, and how to protect your work. Read More

How to build a delay in a workflow for task creation

MBMRED
Tera Expert

I'm looking for advice on how to build a delay into a workflow (using Workflow Studio) to create tasks relative to a specific date.  

Scenario: 

Create tasks 60-days prior to indicated start date (captured in the requested item).

Caveat:  Start date may change while we're waiting on tasks to kick off.

 

Requirements:

  1. Periodically check the "start date" on the RITM to determine if the start date has changed.
    • If changed, the delay loop should auto-adjust to the new date (forward or backward)
  2. Best way to delay task creation until today's date is at/within 60-days of start date

Note: We are on Zurich.

 

Thank you in advance,

Mathew

1 ACCEPTED SOLUTION

Tanushree Maiti
Tera Patron

Hi @MBMRED 

 

The best way to delay task creation until 60 days before a start date in a workflow is to use a Timer activity configured with a custom script. This script dynamically calculates the exact difference in seconds between today's date and the target start date minus 60 days, pausing the workflow until then. 

 

In Timer activity ->Script

(sample script /not tested/ modify it as per your requirement) 

 

var startDate = new GlideDateTime(current.variables.u_start_date);
startDate.addDaysUTC(-60);
var now = new GlideDateTime();
var diffSeconds = (startDate.getNumericValue() - now.getNumericValue()) / 1000;
if (diffSeconds < 0) {
answer = 0;
} else {
answer = diffSeconds;
}

Refer:

https://www.servicenow.com/community/developer-forum/i-need-to-schedule-an-email-notifications-for-c...

 https://www.servicenow.com/community/developer-forum/workflow-timer-activity-script/td-p/3463501

 

 

Please Accept the solution if it assisted you with your question & Mark this response as Helpful.
Regards
Tanushree Maiti
ServiceNow Technical Architect
LinkedIn: https://www.linkedin.com/in/tanushreemaiti

View solution in original post

6 REPLIES 6

Tanushree Maiti
Tera Patron

Hi @MBMRED 

 

The best way to delay task creation until 60 days before a start date in a workflow is to use a Timer activity configured with a custom script. This script dynamically calculates the exact difference in seconds between today's date and the target start date minus 60 days, pausing the workflow until then. 

 

In Timer activity ->Script

(sample script /not tested/ modify it as per your requirement) 

 

var startDate = new GlideDateTime(current.variables.u_start_date);
startDate.addDaysUTC(-60);
var now = new GlideDateTime();
var diffSeconds = (startDate.getNumericValue() - now.getNumericValue()) / 1000;
if (diffSeconds < 0) {
answer = 0;
} else {
answer = diffSeconds;
}

Refer:

https://www.servicenow.com/community/developer-forum/i-need-to-schedule-an-email-notifications-for-c...

 https://www.servicenow.com/community/developer-forum/workflow-timer-activity-script/td-p/3463501

 

 

Please Accept the solution if it assisted you with your question & Mark this response as Helpful.
Regards
Tanushree Maiti
ServiceNow Technical Architect
LinkedIn: https://www.linkedin.com/in/tanushreemaiti

Ankur Bawiskar
Tera Patron

@MBMRED 

see this link

Create Catalog task after 30 days from the selected date in variable 

💡 If my response helped, please mark it as correct and close the thread 🔒— this helps future readers find the solution faster! 🙏

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

Ankur,

Will this periodically check the RITM to verify that the "Start Date" (that is the reference date) has not changed?

 

Thank you,

Mathew

 

Luca Michalczyk
Tera Contributor

Setting flow designer flows and workflow execution to wait for longer times might break over a long run.

This seems to be problem that should be solved process wise instead of technical.

 

Eitherway, I had similiar topics in the past and we went with creating the tasks/records without waiting but putting it's due date to the desired date. This works for employee task's, because the 'My Task' widget in the portal is sorting by due date by default. The task is created, sorted to the bottom and you do not have to think about making the workflow wait for time x. 

 

If this is about sc_tasks, please reconsinder creating them outside of the flow attached to the sc_cat_item because you'd have to make the sc_cat_item's flow aware, that a catalog task was created outside of the flow in case that the flow should wait for the task to be closed.