Welcome to Community Week 2025! Join us to learn, connect, and be recognized as we celebrate the spirit of Community and the power of AI. Get the details  

How to calculate the total of variables into another variable from a multi-row variable set?

John Clyde Ap_a
Giga Expert

Is there a way to calculate the total for this? The quantity will still need to be multiplied based on the price per size of the drink and I wanted to store the total into another variable that is not inside the multi-row variable set.

find_real_file.png

Is there also a way so that every time they click add It'll update the total variable outside of the MRVS

find_real_file.png

 

 

1 ACCEPTED SOLUTION

John Clyde Ap_a
Giga Expert

Thread closed: 

Please refer to this link for displaying the data:  How to display data from a client script to an HTML widget and display its value in a text field.

In short, for the calculation and displaying of data. I used a custom with label variable and created a widget

find_real_file.png

with the following client script:

api.controller = function($scope) {
    /* widget controller */
    var c = this;
    $scope.$watch(function() {
        //Internal Name of Variable Set 'item_details'
        return $scope.page.g_form.getValue('choose_your_drink_s');
    }, function(value) {
        if (value) {
            var mrvsData = JSON.parse(value.replace(/'/g, ''));
            console.log(value);
            var totalCost = 0;

            for (var i = 0; i < mrvsData.length; i++) {
                var mrvs = mrvsData[i];
                totalCost += parseFloat(mrvs.pricePerSize) * parseFloat(mrvs.quantity);
                alert('Calculating: ' + totalCost + ' for --> ' + mrvs.pricePerSize);
            }
						
            //Set Value in the Total Cost
            alert(totalCost);
					 c.data.cost = totalCost;
           $scope.page.g_form.setValue('total_amount',totalCost);
						alert(c.data.cost);
        }
    });
};

and HTML:

<div>
<input type="text" value ={{c.data.cost}} readonly>
</div>

To achieve this:

find_real_file.png

 

~Melvin B.

View solution in original post

12 REPLIES 12

Yousaf
Giga Sage

Hi,

Please refer to this link you may have to change script a little as per your choice.

Calculating totals from mult-row variable set rows in to a variable on a record producer?


Mark Correct or Helpful if it helps.


***Mark Correct or Helpful if it helps.***

Hello Yousaf,

There seems to be only one variable being calculated. How can I parse two variables? Since the quantity will still need to be multiplied based on the price per size of the drink and will it be an onChange script? Where will I apply the script?

 

edit: 

I have tried but I cannot seem to make it work..

find_real_file.png

 

Here is my config and script:

find_real_file.png

 

I am currently getting this error

find_real_file.png

 

For more reference, here is the config for the mrvs.

find_real_file.png

Ankur Bawiskar
Tera Patron
Tera Patron

@John Clyde Ap[as 

onChange of MRVS won't work directly

check this link and you will get solution/way on detecting add/removal of row and based on that you can calculate

Add the total cost in multirow variable set

Regards
Ankur

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

Hello Ankur,

I have tried it but I cannot seem to see the field.

This is the widget alongside the script:

find_real_file.png

It is showing like this, I cannot seem to see the field but the data can be seen in the alert.

find_real_file.png

Here is the config for the variable.

find_real_file.png

find_real_file.png