Add Hours to a Date field on a WF Timer Script

jsummers
Kilo Contributor

Hello,

 

I'm using a Workflow Timer to wait for a date field but need to add hours to the Date, and can't seem to figure this one out. I'm referencing a variable (emp_start) that is a Date field on the RITM that the workflow is running on but it's not waiting for that date and proceeding through immediately. I'm wanting the workflow to continue at 9am on the emp_start date.

 

Here is what I've come up with so far, although fairly new to scripting, any help would be greatly appreciated:

 

var getDate = current.variables.emp_start;

var setDate = getDate.getGlideObject();

setDate.setHours(09);

setDate.setMinutes(00);

    var startDate = gs.dateDiff(gs.nowDateTime(), setDate, true);

        answer = startDate;

 

Thanks!

-Jon

1 ACCEPTED SOLUTION

Nope, the issue is the date format you're using, versus what my script is expecting.


Change this line:


var splitDate = getDate.split("-"); // Split Catalog Date into year, month, day  




To this line:


var splitDate = getDate.split("/"); // Split Catalog Date into year, month, day




Then change this line:


var startDate = new Date(splitDate[0],splitDate[1]-1,splitDate[2]);




To this line:


var startDate = new Date(splitDate[2],splitDate[0]-1,splitDate[1]);




Your date format on your instance is different than mine, hence the issue.


View solution in original post

16 REPLIES 16

I think it's because the glideobject returned for a variable is a Variable not a GlideDateTime. I didn't try this earlier on a variable just a standard date time field. I did try it this time on a variable like this:



// Variable GlideDateTime


var getDate = new GlideDateTime(gr.variables.emp_start.getDisplayValue());




// Convert to Javascript Date which has setHours and setMinutes functions


var d = new Date(getDate.getNumericValue());


d.setHours(9);


d.setMinutes(0);



// Convert Javascript Date to a GlideDateTime


var setDate = new GlideDateTime();


setDate.setValue(d.getTime());



// Set answer to seconds between now and our new date/time


answer = gs.dateDiff(gs.nowDateTime(), setDate, true);



Hopefully it will work better now.


jsummers
Kilo Contributor

While I'm not getting any errors, it doesn't seem to even acknowledge the date and goes right through the timer job onto the next stage.


You'll need to add in a gs.log statement to see what answer has. It should contain the # of seconds to wait and should be a positive value.


robpickering
ServiceNow Employee
ServiceNow Employee

I find it much easier, for stuff like this, to avoid the Glide environment all together and stick with Javascript.   Since you're starting with a string (catalog variable) and you need to calculate seconds for the timer you don't need Glide.   We have a similar problem with allowing a Manager to specify the date that the employee will be offboarded, we hard-code the time to 5:00 p.m. EST, here is how we did it (worked like a charm):


// get date entered in catalog


var offboardDate = current.variables.offboard_departure_date.getDisplayValue();


var timeNow = new Date(); // Current Date/Time


var splitDate = offboardDate.split("-"); // Split Catalog Date into year, month, day


var termDate = new Date(splitDate[0],splitDate[1]-1,splitDate[2]); // Avoid timezone issues, but month starts at 0


termDate.setHours(14); // 5:00 p.m. EST (2:00 p.m. PST by default)



var dif = termDate.getTime() - timeNow; // Difference in milliseconds


var Seconds_from_T1_to_T2 = dif / 1000; // Convert to seconds


answer = Math.abs(Seconds_from_T1_to_T2); // Make sure it's not negative



workflow.debug('Timer will end @: ' + termDate + '   ' +


  (answer/60) + ' minutes from now' );



This allows our Offboarding workflow to sit and wait until the specified date at 5:00 p.m. EST and then proceed with the automated Offboarding, notifications, and Task assignments.


Hi Rob,



I tried changing out just my variable for yours and this is all I'm getting in the log: "Timer will end @: Invalid Date   NaN minutes from now" and the workflow just proceeds without waiting.



The only other thing is on the timer, I've tried multiple configurations on the "Schedule based on" and "Time zone based on" and still no luck.



-Jon