Calculating a field via business rule

Staxed
Giga Guru

I am trying to populate an integer field on a table based on 5 other fields on that table, using a business rule; can't seem to get it to work so hoping someone can point out what I'm doing wrong.

 

The 5 other fields are of type "choice", but their values are just numbers.

 

The business rule I'm currently using is below, running on the specific table, and set to after (I've tried before as well).  The conditions are set to when any of the 5 fields changes (just 5 or statements "or if field1, field2, etc changes"

 

 

 

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

	var total = parseInt(current.u_compliance_score) + parseInt(current.u_org_impact_score) + parseInt(current.u_patient_safety_score) + parseInt(current.u_productivity_score) + parseInt(current.u_revenue_impact_score);
current.u_clinical_optimization_score = total;

})(current, previous);

 

also tried this:

(function executeRule(current, previous /*null when async*/) {
	var compliance_score = parseInt(current.getValue('u_compliance_score'));
	var org_impact_score = parseInt(current.getValue('u_org_impact_score'));
	var patient_safety_score = parseInt(current.getValue('u_patient_safety_score'));
	var productivity_score = parseInt(current.getValue('u_productivity_score'));
	var revenue_impact_score = parseInt(current.getValue('u_revenue_impact_score'));
	var result = compliance_score + org_impact_score + patient_safety_score + productivity_score + revenue_impact_score;
	current.u_clinical_optimization_score = result;

})(current, previous);
8 REPLIES 8

NaN = Not a Number

So one of your values is Null or a string that cannot be parsed into a number.

You may want to do something like this for all of your variables.  It will prevent a null and empty string values from being converted which can cause a NaN result.

var productivity_score = 0;
if(current.variables.productivity_score)
	productivity_score = parseInt(current.variables.getValue('productivity_score'));

 

Thanks, I'll give that a try now.  Though the value of all these options are all numbers, so I'm not sure where it could be pulling a null value from.

hi @Staxed add .toString() to last line instead of above and try

Kamil Smusz
Kilo Sage

Hi @Staxed ,

 

If your BR is run AFTER add current.update() at the end and check.