Add the total cost in multirow variable set

William Dalogdo
Kilo Contributor

How can I multiply 'Unit Price'   and  'Quantity' then display it on 'Price' Field and then  display the sum in  'total cost'.

find_real_file.png

1 ACCEPTED SOLUTION

Kartik Sethi
Tera Guru
Tera Guru

Hi @William Dalogdog 

 

You can follow the community reply by @Aldwin --> "MRVS detect when a row is removed or deleted". On the same ground, I have prepared an Update Set which you can import in your PDI and check the results.

find_real_file.png
Please find step by step solution below:

  1. Create a Widget and add the below-provided 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('item_details');
        }, 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 += parseInt(mrvs.price);
                    alert('Calculating: ' + totalCost + ' for --> ' + mrvs.price);
                }
    
                //Set Value in the Total Cost
                alert(totalCost);
                $scope.page.g_form.setValue('total_cost', totalCost);
            }
        });
    };
  2. Create a Variable of type Custom and add the Widget created in Step 1
  3. Variable details are provided below:
    • item_details: Internal Name of the MRVS Variable Set
    • total_cost: Variable on the Catalog Item (not inside MRVS variable Set)

Please follow the above steps and import the Update set provided in the attachment.


Please mark my answer as correct if this solves your issues!

If it helped you in any way then please mark helpful!

 

Thanks and regards,

Kartik

View solution in original post

14 REPLIES 14

Aman Kumar S
Kilo Patron

Hey,

You can follow below article:

Accessing Multi-row variables set client side in ServiceNow

 

Feel free to mark correct, If I answered your query.

Will be helpful for future visitors looking for similar questions 🙂

Best Regards
Aman Kumar

Thank you. can I use onchange instead of onload?

 

 

Yes, you can .

Follow below article:

Accessing Multi-Row Variable Set value outside the Multi-Row Variable Set [Catalog Item, Client Side...

 

Feel free to mark correct, If I answered your query.

Will be helpful for future visitors looking for similar questions 🙂

Best Regards
Aman Kumar

Hey,

Didn't hear back on this.

Is your issue resolved? If yes, feel free to mark helpful/correct, so it will be helpful for others looking for similar query.

Best Regards
Aman Kumar