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.

Adding days to date field based on another date field (Default value)

Tomer
Kilo Expert

Hello experts,

I'm writing a short script for adding days to date field value based on another field (Record producer):

javascript:var d = new GlideDate(current.u_start_day.getDisplayValue()); d.addDays(182); d.getDisplayValue();

need to add half an year for starting date.

this piece of code worked without my field:

javascript:var d = new GlideDate(current.u_start_day.getDisplayValue()); d.addDays(182); d.getDisplayValue();

but then it's adding days from today (I need to take it from the date on : 'u_start_day' field).

any suggestions?

Thanks,

Tomer.

6 REPLIES 6

Jorn van Beek
Tera Guru

try 

var d = new GlideDate();d.setValue(current.u_start_day.getDisplayValue()); d.addDays(182); d.getDisplayValue()

 

I had more issues with GlideDate and GlideDateTime when I wanted to set the date/time component on calling the class.

Thank you Jorn, it's still taking half an year from today

can I do client side?

 

thanks again.

 

find_real_file.png

 
 

So, you are wanting to add 182 days to Start Date to create Expiration Date?

If so, then this should do the trick:

var d = new GlideDate(current.u_start_day);
d.addDaysLocalTime(182);
current.u_expiration_date = d.getValue();

Sumanth16
Kilo Patron

Hi Tomer , 

 

Try below script in business rule 

function onBefore(current, previous) {

    //This function will be automatically called when this rule is processed.

var gdt = gs.now();  

if(your condition){  

   

var gdt = new GlideDateTime();

 

gdt.addDaysLocalTime(number of days you want to add);

 

current.your second field name= gdt;

}    

}

 

Mark as correct if it is helpful 

 

 

Thanks ,

Sumanth