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

rammohanraomadd
Kilo Guru

Hi,

 

Please try the below script:

 

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

 

var gdt = new GlideDateTime(current.ends);//hope ends is the correct field name

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

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

 

}

 

})(current, previous);

 

Regards,

Ram M

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

christopher_wat
Kilo Contributor

Thanks Ram, I tried that but didn't seem to work either,

 

I looked at the dictionary entry for renewal_end_date, and there is only on OOB ACL on it for Write whilst DRAFT state, the contract I tried were in draft state anyway, but I tried deactivating it, but that didn't work either

 

 

Is there something basic I'm missing that might prevent the BR from firing, it didn't have a condition, but tried on change which didn't make difference

 

Thanks

 

 

Chris

purbalipc
Tera Expert

add addInfoMessage before creating glidedatetime object as suggested by Ram to see if business rule is getting triggered or not