Using Variable to Add Days with Script

melinda_owen
Giga Contributor

Hi, I'm not sure I'm posting this in the correct forum since I felt it could go in a number of places. My scenario is:

I have a Date variable (u_term_date) that gets filled in on the first task of my workflow, after my RITM has been created, it's for a contract termination date. I need my second task to wait to generate until 90 days after this term date variable. 

I have added a duration field to be hidden on my RITM so it can hold the value of the date for the 90 days added and the workflow Timer can wait until that duration field. I am having trouble with my script and would love some assistance and feedback, please.

Here is my script currently inside my Timer activity.

if(current.variables.u_term_date.getDisplayValue() < gs.nowDate()) {
 answer = true;
}
else{
answer = false;
}

I had included in my script the addDays() which it doesn't seem to be adding, since I included a gsLog after it to see if the addition happened, and it did not. It was still logging the original u_term_date. I am new to scripting inside ServiceNow, and more importantly, a complete novice to Date scripting. I have scoured the Community for related topics, and most are incorporating adding days/time to current date, which is not my goal. I've tried piecing together scripts and I've tried a couple different approaches, all to no avail. Any and all help is appreciated at this point.

If I need to include any further information, please let me know. Thank you

1 ACCEPTED SOLUTION

you need below script 

var now= new GlideDateTime();
var gdt = new GlideDateTime(current.variables.u_term_date); 
gdt.addDaysLocalTime(90); //adds 90 days to term date variable
var dur =  GlideDateTime.subtract(now, gdt);
answer = dur.getNumericValue();

View solution in original post

13 REPLIES 13

VigneshMC
Mega Sage

gs.nowDate() is a server side function cannot be used in client script

Refer below thread for your requirement

https://community.servicenow.com/community?id=community_question&sys_id=76b58321db1cdbc01dcaf3231f9619d3&view_source=searchResult

Alberto Consonn
ServiceNow Employee
ServiceNow Employee

Hi,

if you want to do this though a client script, you have to follow there article:

Client Side Dates in ServiceNow

If I have answered your question, please mark my response as correct so that others with the same question in the future can find it quickly and that it gets removed from the Unanswered list.

Thanks you

Cheers
Alberto

I appreciate the response. Reviewing that article, I can see the comparison being helpful in my situation, but it's comparing only the one variable/field to current time, and I am trying to find how to add days onto my current variable before any comparison to now is done.

Mike Patel
Tera Sage

If it's timer activity in workflow than you need something like below

var gdt = new GlideDateTime(current.variables.u_term_date); 
gdt.addDays(90); //adds 90 days to term date variable
answer = gs.dateDiff(gs.nowDateTime(),gdt + ' 07:00:00' , true); // I want it to run at 7am so you can change it as you need it.