Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

How to create onChange Client Script against Multi-Row Variable Set?

AndresGT1
Giga Expert

I'm looking for a way to create an onChange catalog client script (employee center request) to watch over the row values.

 

The Multi-Row variable set has a field called "Amount" that will be added on each input and outside of it there's a field called "Total Amount" that should be updated every time a row is added.

 

Anyone has been on a similar requirement? Any idea works.

7 REPLIES 7

Samaksh Wani
Giga Sage

Hello @AndresGT1 

Catalog Client Script
Type: onChange
Applies to Variable: your MRVS variable (e.g. mrvs_items)
Applies on: Catalog Client Script (not MRVS client script)



function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue === oldValue) {
        return;
    }

    try {
        // Get the MRVS JSON
        var mrvsData = g_form.getValue('mrvs_items');
        var total = 0;

        if (mrvsData) {
            var rows = JSON.parse(mrvsData);

            // Loop through each row and sum the Amount values
            rows.forEach(function(row) {
                var amount = parseFloat(row.amount || 0);
                if (!isNaN(amount)) {
                    total += amount;
                }
            });
        }

        // Set the total in the Total Amount field
        g_form.setValue('total_amount', total);
    } catch (e) {
        console.log('Error calculating total amount: ' + e.message);
    }
} 

 
Pls mark my solution as accept, if you find it helpful and give thumbs up.

Regards,

Samaksh Wani

Sarthak Kashyap
Mega Sage

Hi @AndresGT1 ,

 

Please check below link this will help you 

https://www.servicenow.com/community/developer-forum/multi-row-variable-set-client-script-to-do-some...

https://www.servicenow.com/community/developer-articles/accessing-multi-row-variable-set-value-outsi...

 

Please mark my answer correct and helpful if this works for you

Thanks and Regards,

Sarthak

Additionally I also tried same thing in my PDI and it is working fine for me 

I created a Variable set 

SarthakKashyap_0-1761393634941.png

 

Inside that I created a Multi row variable

SarthakKashyap_1-1761393686972.png

 

I also created a client script which works onLoad on multirow variable 

SarthakKashyap_2-1761393798874.png

and Add below script 

function onChange(control, oldValue, newValue, isLoading) {
   if (isLoading || newValue == '') {
      return;
   }

   //Type appropriate comment here, and begin script below
   alert("Here" + g_form.getValue("IO:05a554759374f2509305f520ed03d618"));
   
   
}

 

SarthakKashyap_3-1761393843135.png

Please mark my answer correct and helpful if this works for you

Thanks and Regards,

Sarthak

Brad Bowman
Kilo Patron
Kilo Patron

This post shows how to watch for MRVS changes on Service Portal / ESC

https://www.servicenow.com/community/developer-forum/widget-that-reacts-to-onchange-event-of-variabl...