Mvrs variable Total sum populating out side of Mvrs variable

bhanukota09
Tera Contributor

I have Multi Row Variable Set (MRVS) variables.

If I provide values in the variables like:

Test 1 = 6

Test 2 = 8

Test 3 = 18

My requirement is to calculate the total sum before submitting the request, like:

The calculated total value (32) should be populated into an outside variable named Total Shipment Value.

Please refer to the screenshot. In the screenshot, the “Total Value” field is showing 8. Similarly, I need the “Total Shipment Value” field to be automatically populated with the calculated sum before submitting the request.

1 ACCEPTED SOLUTION

Tanushree Maiti
Tera Patron

Hi @bhanukota09 

 

1. Create a Catalog Client Script 

  • Type: onChange
  • Variable Name: [Internal Name of your MRVS]
  • UI Type: All 
  • Applies on: Catalog Item view 

Script: (sample code/not tested)

 

function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
g_form.setValue('total_shipment_value', '0');
return;
}

try {
var mrvsData = JSON.parse(newValue);
var totalSum = 0;

for (var i = 0; i < mrvsData.length; i++) {
var val = parseFloat(mrvsData[i].test_variable_name);
if (!isNaN(val)) {
totalSum += val;
}
}
g_form.setValue('total_shipment_value', totalSum.toString());

} catch (e) {
console.error("Error calculating MRVS sum: " + e.message);
}
}

Refer: https://www.servicenow.com/community/developer-forum/how-to-auto-calculate-total-from-mrvs/m-p/33149...

https://www.servicenow.com/community/itsm-forum/how-to-calculate-the-sum-of-the-value-in-mrvs-multi-...

 

Please Accept the solution if it assisted you with your question & Mark this response as Helpful.
Regards
Tanushree Maiti
ServiceNow Technical Architect
LinkedIn: https://www.linkedin.com/in/tanushreemaiti

View solution in original post

4 REPLIES 4

Bhavya11
Kilo Patron

Hi @bhanukota09 ,

 

try this script in catalog client script

var prevValue = '';

    function calculateTotalIfChanged() {

        var currentValue = g_form.getValue('total_count'); // name of the MVRS

        if (!currentValue || currentValue === prevValue)
            return;

        prevValue = currentValue;

        var total = 0;

        try {
            var mrvsData = JSON.parse(currentValue);
        } catch (e) {
            return;
        }

        for (var i = 0; i < mrvsData.length; i++) {
            var row = mrvsData[i];

            if (row.total_amount) {
                total += parseFloat(row.total_amount) || 0; //total_amount variable inside the MVRS
            }
        }

        // total_amounts variable outside the MVRS
        g_form.setValue('total_amounts', total);
    }

    setInterval(calculateTotalIfChanged, 1000);
If this information proves useful, kindly mark it as helpful or accepted solution.

Thanks,
BK

Ankur Bawiskar
Tera Patron

@bhanukota09 

yes this is feasible

you can populate outside variable dynamically when a new row is added/removed

check below links

How to calculate the sum of the value in MRVS (multi row variable set) 

Populate field outside MRVS with a total sum of fields inside MRVS 

💡 If my response helped, please mark it as correct and close the thread 🔒— this helps future readers find the solution faster! 🙏

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

@bhanukota09 

Just a quick follow-up—were you able to review the links above and try the suggested approach?
Please let me know if you need any assistance.

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

Tanushree Maiti
Tera Patron

Hi @bhanukota09 

 

1. Create a Catalog Client Script 

  • Type: onChange
  • Variable Name: [Internal Name of your MRVS]
  • UI Type: All 
  • Applies on: Catalog Item view 

Script: (sample code/not tested)

 

function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
g_form.setValue('total_shipment_value', '0');
return;
}

try {
var mrvsData = JSON.parse(newValue);
var totalSum = 0;

for (var i = 0; i < mrvsData.length; i++) {
var val = parseFloat(mrvsData[i].test_variable_name);
if (!isNaN(val)) {
totalSum += val;
}
}
g_form.setValue('total_shipment_value', totalSum.toString());

} catch (e) {
console.error("Error calculating MRVS sum: " + e.message);
}
}

Refer: https://www.servicenow.com/community/developer-forum/how-to-auto-calculate-total-from-mrvs/m-p/33149...

https://www.servicenow.com/community/itsm-forum/how-to-calculate-the-sum-of-the-value-in-mrvs-multi-...

 

Please Accept the solution if it assisted you with your question & Mark this response as Helpful.
Regards
Tanushree Maiti
ServiceNow Technical Architect
LinkedIn: https://www.linkedin.com/in/tanushreemaiti