The CreatorCon Call for Content is officially open! Get started here.

How to wait until specific time of day?

josh_tessaro
Giga Expert

We have a scenario where we need a workflow to pause until at or after the next occurrence of 4:00 AM. When a network account request is completed, we want to wait until after the new user record is inserted into service-now (4:00 AM) before kicking off the other items on the request.

Any suggestions on how to set up an activity that will always wait until the next occurrence of <time>?

EDIT: The end goal here was to wait until a new sys_user was inserted from AD, which I have done using my below solution. It turns out using a wait for or timer is not the best way to accomplish this but a timer solution that works has been marked as the correct answer.

I am checking if there is more than one sys_user matching last name and created in the last day. If yes I continue, if no I wait an hour and repeat.

SS052.bmp

1 ACCEPTED SOLUTION

Jim Coyne
Kilo Patron

You can use a "Timer" activity with the "Timer based on" field set to "Script":


ServiceNow.png



And this would be your script:



// Set 'answer' to the number of seconds this timer should wait


answer = (function(){


  var gdtToday = new GlideDateTime();


  var gdtTomorrow = new GlideDateTime();


  gdtTomorrow.addDaysLocalTime(1);


  gdtTomorrow.setDisplayValue(gdtTomorrow.getLocalDate() + " 04:00:00");   //careful, there's a leading space before 04:00:00


  //calculate wait time


  var wait = gs.dateDiff(gdtToday.getDisplayValue(), gdtTomorrow.getDisplayValue(), true);


  return wait;


})();



The script calculates the number of seconds before 4:00 am the next day.


View solution in original post

10 REPLIES 10

You can do it in the opposite direction, but its dangerous since Last_Name is not unique.


What I was thinking is on a User insert you scan through your open task records looking for the shared unique value (in whatever field you're storing it), then you pump an update to work notes and make your Wait For Condition look for that.



Its clunkier than I like, but it might work.


I got it working. I am checking if there is more than one sys_user matching last name and created in the last day. If yes I continue, if no I wait an hour and repeat.



SS052.bmp


Wait For Conditions only evaluate on updates to the record the workflow is in context of.   So I don't think that script will work as it would require a check every second until the appropriate time.


Jim Coyne
Kilo Patron

You can use a "Timer" activity with the "Timer based on" field set to "Script":


ServiceNow.png



And this would be your script:



// Set 'answer' to the number of seconds this timer should wait


answer = (function(){


  var gdtToday = new GlideDateTime();


  var gdtTomorrow = new GlideDateTime();


  gdtTomorrow.addDaysLocalTime(1);


  gdtTomorrow.setDisplayValue(gdtTomorrow.getLocalDate() + " 04:00:00");   //careful, there's a leading space before 04:00:00


  //calculate wait time


  var wait = gs.dateDiff(gdtToday.getDisplayValue(), gdtTomorrow.getDisplayValue(), true);


  return wait;


})();



The script calculates the number of seconds before 4:00 am the next day.


Cookie.png