Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Workflow Timer Script Example

elemonnier
Tera Expert

You can never get enough examples, so I've included a small example of a workflow timer that pulls a Catalog Item form variable and calculates a time to wait from that date.



//get term_datetime variable
var termDate = new GlideDateTime();
termDate.setDisplayValue(current.variable_pool.term_datetime.getDisplayValue());

//convert to millisecond value and add specified time
var termDateValue = termDate.getNumericValue()
termDateValue += (1000*60*60*24*30); //Add 30 days

//write offset time to datetime variable
var offsetDate = new GlideDateTime();
offsetDate.setNumericValue(termDateValue);
//write comments to RITM
current.comments = '"Additional task on hold until: ' + offsetDate.getDisplayValue();
//return difference from current time to offset time in secs
answer = gs.dateDiff(gs.nowDateTime(),offsetDate.getDisplayValue(),true);
5 REPLIES 5

lawrence_eng
Administrator
Administrator

Hi Eric,

Thank you for taking the time to share!

-Lawrence


Mike Hill1
Giga Contributor

Eric,



Thanks for posting this information - I was able to use it as a starting guide for something similar that I am trying to do with a workflow timer being set from a variable that is set in a catalog task before the timer.   For this example I wanted the timer to wait until the day after the variable "install_date" was set to in the catalog task.



//get "install_date" variable from the request item  


var installDate = new GlideDateTime();  


installDate.setDisplayValue(current.variables.install_date.getDisplayValue());  


 


//convert install_date value to millisecond value    


var installDateValue = installDate.getNumericValue();



//get current date/time in MS


var dateobj = new Date();


var dateObject = new Date(dateobj.getTime() + (dateobj.getTimezoneOffset() * 60000));


var currentDateMS = (dateobj.getTime() + (dateobj.getTimezoneOffset() * 60000));



//calculate difference in seconds and add 86400 for the day after


answer = (((installDateValue - currentDateMS)/1000) + 86400);


Thanks

 

you help me a lot too!

 

🙂

maricielo
Tera Contributor

Thanks a lot!

 

It was very helpful 🙂