Calculate the percentage of each field

heathers_
Kilo Sage

I need to calculate three fields and identify their respective percentage.

 

Use Case: Three fields will be populated with integers. I need to sum the values together and output the percentage

 

Input:

OpEx Amount = 5000

CapEx 1 Amount = 1000

CapEx 2 Amount = 400

Sum = 6400

 

Output (calculated value):

OpEx Percentage = 78.12

CapEx 1 Amount = 15.63

CapEx 2 Amount = 6.25

 

 

1 ACCEPTED SOLUTION

@heathers_ 

if you want in reverse then update your BR as this

(function executeRule(current, previous /*null on insert*/) {
    var total = current.u_total_amount || 0;

    var opexPerc = current.u_opex_percentage;
    var capex1Perc = current.u_capex1_percentage;
    var capex2Perc = current.u_capex2_percentage;

    // Only calculate if total > 0
    if (total > 0) {
        if (opexPerc != null && opexPerc !== '') {
            current.u_opex_amount = parseFloat(((total * opexPerc) / 100).toFixed(2));
        }

        if (capex1Perc != null && capex1Perc !== '') {
            current.u_capex1_amount = parseFloat(((total * capex1Perc) / 100).toFixed(2));
        }

        if (capex2Perc != null && capex2Perc !== '') {
            current.u_capex2_amount = parseFloat(((total * capex2Perc) / 100).toFixed(2));
        }
    } else {
        // If total is 0 or empty, clear amounts
        current.u_opex_amount = 0;
        current.u_capex1_amount = 0;
        current.u_capex2_amount = 0;
    }
})(current, previous);

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

View solution in original post

8 REPLIES 8

Dr Atul G- LNG
Tera Patron
Tera Patron

Hi @heathers_ 

Where do you want to display this?
If it's in a report, then it's out-of-the-box (OOTB) functionality.

*************************************************************************************************************
If my response proves useful, please indicate its helpfulness by selecting " Accept as Solution" and " Helpful." This action benefits both the community and me.

Regards
Dr. Atul G. - Learn N Grow Together
ServiceNow Techno - Functional Trainer
LinkedIn: https://www.linkedin.com/in/dratulgrover
YouTube: https://www.youtube.com/@LearnNGrowTogetherwithAtulG
Topmate: https://topmate.io/atul_grover_lng [ Connect for 1-1 Session]

****************************************************************************************************************

@Dr Atul G- LNG , this would either be in a Flow or client script.

Hi @heathers_ 

 

https://www.servicenow.com/community/developer-forum/percentage-calculation-through-script/m-p/24465...

https://www.servicenow.com/community/developer-forum/calculate-percentage/m-p/2027044

*************************************************************************************************************
If my response proves useful, please indicate its helpfulness by selecting " Accept as Solution" and " Helpful." This action benefits both the community and me.

Regards
Dr. Atul G. - Learn N Grow Together
ServiceNow Techno - Functional Trainer
LinkedIn: https://www.linkedin.com/in/dratulgrover
YouTube: https://www.youtube.com/@LearnNGrowTogetherwithAtulG
Topmate: https://topmate.io/atul_grover_lng [ Connect for 1-1 Session]

****************************************************************************************************************

@heathers_ 

If you are planning to store the percentages in fields then I shared approach below.

if not then you can use 3 flow variables of type Integer/Float and calculate the percentage and store in those flow variables using "Set Flow Variables" flow logic.

Flow variables  

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader