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

joshua_bice
Giga Expert

You put in your workflow a wait for condition, and you set the script to something like:



var gdt = new GlideDateTime();


var time = gdt.getTime();



if(time.toString() == '1970-01-01 04:00:00')


          answer = true;



I believe the system is set to GMT, so you may need to off-set that value to fit your organization's timezone, but that should work out if you're using a workflow.


This does not seem to work, I tested as if the sytesm time were both GMT or my local time.


Josh,



Does the record holding the workflow share anything unique with the user getting inserted into the system?


Yes, last_name.



Right now I am trying to write a script against users checking for a user with last_name inserted in the last 5 minutes. If false I'll loop to a timer and if true I will continue.