Business Rule- ChangesTo and Check for Empty

jlaps
Kilo Sage

Good afternoon community,

I am trying to implement a business rule that will check for a change on a boolean checkbox, where if it changes to true, a date/time field is populated with the current time.

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

if (current.u_rca_approved.changesTo('true')) {

if (!current.u_rca_date_approved_on){

current.u_rca_date_approved_on.setValue(gs.now.DateTime());

}

}

})(current, previous);

This is supposed to check for a checkbox being checked, and if so, it checks if the date field is empty or not, and if empty, sets the current time. I am making a mistake someplace though, and I was hoping someone couple help me spot it?

Currently set as an AFTER rule.

When using the fields to trigger things, I could get it to work, but it would update the date every time the record was updated, since I was only able to check if RCA APPROVED = TRUE, which of course it is every time after it changes to true.

Thanks in advance for scripting/syntax help!

Jeff

3 REPLIES 3

sachin_namjoshi
Kilo Patron
Kilo Patron

Hi Jeff,



You can change your business rule on before and use below script


Please check the



  1. (function executeRule(current, previous /*null when async*/) {  
  2. if (current.u_rca_approved.changesTo('True')) {  
  3. if (JSUtil.notNil(current.u_rca_date_approved_on)){  
  4. current.u_rca_date_approved = gs.nowDateTime();
  5. }  
  6. }  
  7.  
  8.  
  9. })(current, previous);  


OR



you can add u_rca_approved and u_rca_date_approved_on in business rule conditions.



Regards,


Sachin


Sachin,



I edited the script to include your changes-



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


        if (current.u_rca_approved.changesTo('True')) {


                  if (JSUtil.notNil(current.u_rca_date_approved_on)){


                  current.u_rca_date_approved_on = gs.nowDateTime();


}


}




})(current, previous);



It is still not working, and I am not getting any useful info from the business rule debugger. I can see the rule getting called, but no info-



find_real_file.png


I did not add any conditions as you note. Can I put in the conditions there, or should I, if they are listed explicitly in the script? I guess I am confused how to trigger using RCA APPROVED = TRUE, when it will be true every time after the first time. I only want to trigger the date set when it flips the first time.


find_real_file.png



is this what you think they should look like? I tried setting these and it made no change. It is not setting the date for sure, but I am not sure if it failing the conditions or what. I will add some log statements to track it down.


Jeff


In business rule conditions, you can use below filter to check RCA changesTo true and also check for date.



Regards,


Sachin