How to calculate Total in MRVS when users enter values in any order?

Ankit Balapure
Tera Contributor

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.

AnkitBalapure_0-1750156063093.png

 

 

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.

AnkitBalapure_2-1750156224794.png

 

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?

7 REPLIES 7

@Ankit Balapure 

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:

AnkurBawiskar_0-1750238978119.png

 

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

Ankit Balapure
Tera Contributor

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

@Ankit Balapure 

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.

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