- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
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.
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
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);
}
}
Regards
Tanushree Maiti
ServiceNow Technical Architect
LinkedIn: https://www.linkedin.com/in/tanushreemaiti
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
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);
Thanks,
BK
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
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! 🙏
Ankur
✨ Certified Technical Architect || ✨ 10x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
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.
Ankur
✨ Certified Technical Architect || ✨ 10x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
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);
}
}
Regards
Tanushree Maiti
ServiceNow Technical Architect
LinkedIn: https://www.linkedin.com/in/tanushreemaiti