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

DrewW
Mega Sage
Mega Sage

Whats not working about it?  Are you getting an odd value?  Is the math wrong?  Are you getting no value?

 

So use either gs.addInfoMessage or gs.log to write out the total so you can see what the value is.

If your log statement does not run then there is probably and error happening, and you need to look in the system log to see if you can find it or your BR did not run and you need to check your condition and see if its set to run on update/insert and active.

Also using the above two options dump out the 5 values separately to see if they are what you think they are.

I suspect one of them is NULL and your code does not like it.

 

It's not getting any value, stays at 0.  I'll do those things you suggested and report back, thanks for the advice!

Uday_Kumar
Giga Guru

Hi @Staxed Your code is correct but your side is not working right.

I am pasting my code and it's working fine for me.

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

	// Add your code here
	current.u_answer=parseInt(parseInt(current.u_choice1)+parseInt(current.u_choice2));


})(current, previous);

My Output :

Uday_Kumar_0-1680366498154.png

You can connect with me in linkedin Uday Kumar Valapudasu so that we can connect through zoom and i will debug your code.

If my solution helps you please mark my Solution Accepted and Helpful.

Thanks and regards

Uday Kumar Valapudasu

 

I got it to work (I had the wrong field displaying, so it was actually updating).  However, now I have a new issue.  I edited by BR to update the variables being pulled from the record producer that is adding these fields.  However, the BR is working to update the field, but I'm getting "NaN" instead of an actual number.  Any idea what could be causing that?  Here's my new BR Script

 

(function executeRule(current, previous /*null when async*/) {
	var compliance_score = parseInt(current.variables.getValue('compliance_score'));
	var org_impact_score = parseInt(current.variables.getValue('org_impact_score'));
	var patient_safety_score = parseInt(current.variables.getValue('patient_safety_score'));
	var productivity_score = parseInt(current.variables.getValue('productivity_score'));
	var revenue_impact_score = parseInt(current.variables.getValue('revenue_impact_score'));
	var result = (compliance_score * 6) + (org_impact_score * 3) + (patient_safety_score * 7) + (productivity_score * 5) + (revenue_impact_score * 4);
	result.toString();
	current.variables.clinical_optimization_score = result;

})(current, previous);

I tried to add the toString() because the clinical_optimization_score is a single line text type on the record producer, still gives me NaN with or without that line.