How to calculate Total in MRVS when users enter values in any order?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-17-2025 03:36 AM
Hi everyone,
I'm working on a Multi-Row Variable Set (MRVS) in a catalog item and need help with calculating a Total field based on the values entered in multiple fee fields.
Use Case
I have the following fields in my MRVS:
transportation_fee
accomodation_fee
meal_fee
other_fee
total (read-only)
Users can enter values in any order in any of the fields above (some fields may even be left blank). Once all the fee fields have values (or at least the fields that are filled), the Total field should automatically update with the sum.
What I Tried
I created a Catalog Client Script of type onChange and set Variable name = None, but it did not get triggered.
I don’t want to create 50 onChange scripts if I have 50 fields.
I want a single script that can listen to changes in all fee fields and calculate the total.
What I Want
A single client script that can watch all specific fields in MRVS.
It should update the Total field automatically when any fee field is changed.
Can someone help me with:
The best approach to achieve this efficiently?
Any sample script or working example?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-18-2025 02:29 AM
Another easy way
1) let the user submit catalog item
2) use after insert business rule on RITM table
3) parse each row of MRVS, calculate the sum and set the MRVS again
(function executeRule(current, previous /*null when async*/ ) {
// Add your code here
var mrvsJsonString = current.variables.mrvsVariableSetName;
var arr = JSON.parse(mrvsJsonString);
arr.forEach(function(obj) {
// Parse string values to numbers, sum, and assign to totalValue
var total = Number(obj.variableName1) + Number(obj.variableName2);
obj.totalValue = total.toString(); // Store as string to match original format
});
// arr now contains the updated objects
gs.info(JSON.stringify(arr));
current.variables.mrvsVariableSetName = JSON.stringify(arr);
current.update();
})(current, previous);
Script I tried
var arr = [
{"variableName1":"100","variableName2":"200","totalValue":""},
{"variableName1":"300","variableName2":"400","totalValue":""}
];
arr.forEach(function(obj) {
// Parse string values to numbers, sum, and assign to totalValue
var total = Number(obj.variableName1) + Number(obj.variableName2);
obj.totalValue = total.toString(); // Store as string to match original format
});
// arr now contains the updated objects
gs.info(JSON.stringify(arr));
Output:
If my response helped please mark it correct and close the thread so that it benefits future readers.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-18-2025 02:34 AM
Hi @Ankur Bawiskar,
Sorry not sure where to use that solution, since I am working in the MRVS and it is about using the client script in the widget, so getting confused in that.
Please suggest if you are clear picture on this one.
Thanks,
Ankit
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-18-2025 02:43 AM
you need to create a widget and then add that in variable of type Custom on your catalog form
Then check the link I shared
Also I shared another workaround using after insert business rule, check that
If my response helped please mark it correct and close the thread so that it benefits future readers.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader