Comparing difference between 'current' and 'previous' in business rule script on task table

Will Coquillet1
Mega Expert

I have a business rule that starts with this:

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

I'm familiar with grabbing values from 'current' but I'm hoping to find the most efficient way to compare all values of 'current' and 'previous' and if the fields sla_due and sys_updated_on are the ONLY differences, then stop the script immediately.

This is because the script runs anytime an update is made to the task table, and a different script runs that changes the SLA value. I don't want my script to run when that sla_due value is the only thing that changed. And I can't modify the other script that actually changes the SLA value.

As a summary, this is what I want to do: ****Compare current and previous and if the only differences are 'sla_due' and 'sys_updated_on' then stop running script****

Any ideas on how I can get this done efficiently?

1 ACCEPTED SOLUTION

athm
Tera Guru

Hi willmx,

I am not sure on which table you were planning to write this script.

But, in case if you are need in of such script for your use case, I think below script might be useful, which would run only when field other than 'sla_due', 'sys_updated_on' and 'sys_mod_count' are changed:

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

	for(var fieldName in current){
		if(fieldName != 'sla_due' && fieldName != 'sys_mod_count' && fieldName != 'sys_updated_on'){
			if(previous[fieldName] != current[fieldName]){
				//Write your logic here
				//gs.log("something other than sla_due,sys_mod_count,sys_udpated_on is changed, field name = " + fieldName + ", previous[fieldName] = " + previous[fieldName] + ", current[fieldName] = " + current[fieldName]);
			}
		}
	}

})(current, previous);

 

Hope this helps you willmx.

View solution in original post

3 REPLIES 3

SanjivMeher
Mega Patron

When sla_due is updated, I think business rules shouldn't be running. So I dont think, you should worry about that field.

And sys_updated_on, will only update when something changes, so you shouldn't worry about that as well.

athm
Tera Guru

Hi willmx,

I am not sure on which table you were planning to write this script.

But, in case if you are need in of such script for your use case, I think below script might be useful, which would run only when field other than 'sla_due', 'sys_updated_on' and 'sys_mod_count' are changed:

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

	for(var fieldName in current){
		if(fieldName != 'sla_due' && fieldName != 'sys_mod_count' && fieldName != 'sys_updated_on'){
			if(previous[fieldName] != current[fieldName]){
				//Write your logic here
				//gs.log("something other than sla_due,sys_mod_count,sys_udpated_on is changed, field name = " + fieldName + ", previous[fieldName] = " + previous[fieldName] + ", current[fieldName] = " + current[fieldName]);
			}
		}
	}

})(current, previous);

 

Hope this helps you willmx.

Thank you, athm! This is indeed the answer that worked for me. I asked another developer on my team how he thought we should do it, and he gave me this function:

if (compareGR(current, previous) != true) {
      return;
    }
function compareGR(new_gr, old_gr) {
    var haveUpdate = false;
    
    for (var prop in new_gr) {
      if (prop != 'sys_meta' && prop != 'sla_due' && prop != 'sys_updated_on' && prop != 'sys_mod_count')
      if (new_gr[prop])
        if (old_gr[prop])
          if (old_gr.getValue(prop) != new_gr.getValue(prop))
            haveUpdate = true;
    }
    return haveUpdate;
  }

I still don't know if he saw your post before he answered me, but either way, this is great!