Business rule to calculate a Date from another Date minus XX days

christopher_wat
Kilo Contributor

Hi,

 

I am trying to create a business rule which will take one Date value from a field, then based on selection of choice another field return a new date in other field, its to work out a review date on a contract based on end date, initially one choiuce only, but there would be two choices

ends - the date the contract due to end

u_ck_renewal_or_termination_period  - the selection used to determine the new date

renewal_end_date - where I want the date calculated entered

 

I have tried the following script, which doesn't seem to work, using Istanbul, no conditions, before, on uodate, insert, any suggetsions on my  error, ommsision or alternative methods would be apprecaited

 

Many thanks

 

Table: ast_contract

(function executeRule(current, previous /*null when async*/) {

 

var gdt = current.ends.getGlideObject();

if (current.u_ck_renewal_or_termination_period =='3 Months'){   

current.renewal_end_date=gdt.subtractDays(90); 

 

}

 

})(current, previous);

 

15 REPLIES 15

Archana Reddy2
Tera Guru

Hi Christopher,

Give it a try. This should work.

var gdt = current.ends.getGlideObject();
if (current.u_ck_renewal_or_termination_period =='3 Months'){   
gdt.subtractDays(90); 
current.renewal_end_date=gdt;

Mark the answer as Correct/Helpful based on its impact.

Thanks

Chris Waters
Kilo Contributor

Thanks Archanareddy, that fires and puts a value in renewal_end_date, but it matches "ends"  date, i.e it doesn't subtract the 90 days it just copies the dates. Any ideas?

 

Thanks

 

Chris

 

Hi,

Try modifying the below.

gdt.subtractDays(90);//existing one
gdt.addDays(-90)//modified one

Hope this helps!

Thanks

Mike Patel
Tera Sage

try below

var gdt = current.ends.getGlideObject();

if (current.u_ck_renewal_or_termination_period =='3 Months'){   

current.renewal_end_date=gdt.addDays(-90); 

 }

 

Venkat799
Kilo Expert

Hello Christopher,

Please go through this. it's for adding days, if you want to subtract just add - sign in front of it.

The following code declares a GlideDateTime object, initializes it with the current date and time, then adds 3 months and 2 days by first adding 24 hours in milliseconds, 1 day in days, and then 3 months. Finally, it sets the new date/time to a date/time field on the current record:

var gdt = new GlideDateTime(gs.nowDateTime()); //current date/time 
gdt.add(24*60*60*1000); //Add 24 hours
gdt.addDaysLocalTime(1); //Add another day
gdt.addMonthsLocalTime(3); //Add 3 months
current.setValue('start_time', gdt.getValue());

Regards
Sree