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

Ankur Bawiskar
Tera Patron
Tera Patron

@heathers_ 

this is for table?

If yes then you can use 3 fields to store the Amount and respective Percentage fields for each.

You can use before insert/update business rule to populate those

Sample BR Script

(function executeRule(current, previous /*null on insert*/ ) {
    // Retrieve amount values, default to 0 if empty or null
    var opex = current.u_opex_amount || 0;
    var capex1 = current.u_capex1_amount || 0;
    var capex2 = current.u_capex2_amount || 0;

    // Calculate sum
    var total = opex + capex1 + capex2;

    // Only calculate percentages if sum > 0
    if (total > 0) {
        // Calculate percentages, only if corresponding amount field is not empty
        if (current.u_opex_amount != null && current.u_opex_amount !== '') {
            current.u_opex_percentage = parseFloat(((opex / total) * 100).toFixed(2));
        }

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

        if (current.u_capex2_amount != null && current.u_capex2_amount !== '') {
            current.u_capex2_percentage = parseFloat(((capex2 / total) * 100).toFixed(2));
        }
    } else {
        // If total is 0, clear percentages
        current.u_opex_percentage = 0;
        current.u_capex1_percentage = 0;
        current.u_capex2_percentage = 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

@Ankur Bawiskar This was helpful.

 

What if I have the total value and need to determine the amount for each based on the percentage. Essentially, opposite of my original question.

 

SUM = 100000

 

OpEx Percentage = 25

CapEx 1 Percentage = 70

CapEx 2 Percentage = 5

 

Output (calculated value):

OpEx Amount = 25000

CapEx 1 Amount = 70000

CapEx 2 Amount = 5000

(Sum = 100000)

@heathers_ 

that's also feasible.

I believe I answered your original question.

Would you mind marking my response as correct if my response helped?

The discussion can continue on an answered thread as well.

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

@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