want to set percentage based on two integer field values using BR

siva14
Tera Contributor

Hi everyone,

we have a requirement that, we want to update percentage based on two integer field value ,

so i have created After insert BR, but it's working , any suggestion that would be great help for me.

 

(function executeRule(current, previous /*null when async*/) {
 
var mcatdemand =parseFloat(current.u_mcat_current_year_demand);
var mcatreturn =parseFloat(current.u_current_year_returns);
 
if(current.u_current_year_returns != 0){
var percentage = (mcatdemand/mcatreturn)*100;
gs.log('siva:' + percentage);
gs.addInfoMessage('siva:' + percentage);
current.u_currentyearpercentagereturns = percentage.toFixed(2);
}else{
current.u_currentyearpercentagereturns = 0;
}
 
// Add your code here
 
})(current, previous);
 
 
#business rule #BR
1 ACCEPTED SOLUTION

Juhi Poddar
Kilo Patron

Hello @siva14 

To calculate and update the percentage based on two integer fields, make sure to handle possible null values, division by zero, and ensure proper data types.

Here's an optimized version of your Business Rule:

(function executeRule(current, previous /*null when async*/) {
    var mcatdemand = parseFloat(current.u_mcat_current_year_demand);
    var mcatreturn = parseFloat(current.u_current_year_returns);
    
    if (!isNaN(mcatdemand) && !isNaN(mcatreturn)) {
        current.u_currentyearpercentagereturns = mcatreturn !== 0 
            ? (mcatdemand / mcatreturn * 100).toFixed(2) 
            : 0;
    } else {
        current.u_currentyearpercentagereturns = 0; // Handle invalid data
    }
})(current, previous);

Make sure this Business Rule runs After Insert or After Update based on your requirement.

Hope this helps!

 

"If you found my answer helpful, please like and mark it as an "accepted solution". It helps others find the solution more easily and supports the community!"

 

Thank You
Juhi Poddar

View solution in original post

2 REPLIES 2

Juhi Poddar
Kilo Patron

Hello @siva14 

To calculate and update the percentage based on two integer fields, make sure to handle possible null values, division by zero, and ensure proper data types.

Here's an optimized version of your Business Rule:

(function executeRule(current, previous /*null when async*/) {
    var mcatdemand = parseFloat(current.u_mcat_current_year_demand);
    var mcatreturn = parseFloat(current.u_current_year_returns);
    
    if (!isNaN(mcatdemand) && !isNaN(mcatreturn)) {
        current.u_currentyearpercentagereturns = mcatreturn !== 0 
            ? (mcatdemand / mcatreturn * 100).toFixed(2) 
            : 0;
    } else {
        current.u_currentyearpercentagereturns = 0; // Handle invalid data
    }
})(current, previous);

Make sure this Business Rule runs After Insert or After Update based on your requirement.

Hope this helps!

 

"If you found my answer helpful, please like and mark it as an "accepted solution". It helps others find the solution more easily and supports the community!"

 

Thank You
Juhi Poddar

Hi @Juhi Poddar ,

 

thanks for the response, i have tried above script, still not updating percentage, and i have checked the field types, i am using percent complete as field type

 

siva14_1-1735068773512.png