How to write on change client script to get the sum of the MRVS

harinya
Tera Contributor

HI ,
Can someone please help on this
I have MRVS with two variables quantity and month, if i enter the value of quantity records 

i want the sum of the quantity values to be displayed in total quantity variable which is outside of the MRVS
need a on change client script as user can change the quantity , so Total quantity should show updated value

harinya_0-1700132696058.png

 

2 ACCEPTED SOLUTIONS

Hi @harinya 

 

There are a few steps you need to take:

 

First on your catalog item, create a (hidden) field 'quantity_updated' (type checkbox).

 

Then Create a onLoad Client script on your catalog item:

 

function onLoad() {
    //We need to make the g_form object for the MRVS available for later
    this.cat_g_form = g_form;
}

 

 

Second, create a onChange Client script on your catalog item, which triggers on change of quantity_updated. Please note that you need to fill the name of your MRVS.

 

 

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '' || newValue == 'no') {
        return;
    }
    g_form.setValue('quantity_updated', false);

    //Need to set a timeout because MRVS is not readable directly.
    setTimeout(function() {
        var mrvsRows = g_form.getValue("<<FILL MRVS NAME>>");
        var oMrvs = JSON.parse(mrvsRows);
        var total_quantity = 0;
        for (var i in oMrvs) {
            total_quantity += parseInt(oMrvs[i].quantity);
        }

        g_form.setValue("total_quantity", total_quantity);

    }, 500);

}

 

 

And last create a onSubmit Client script in your MRVS:

 

function onSubmit() {

        //Service Portal logic
        if (this) {
            this.cat_g_form.setValue('quantity_updated', true);
        } else {
            //Native UI logic
            parent.g_form.setValue('quantity_updated', true);
        }
}

 

 

That should be it.


Help others to find a correct solution by marking the appropriate response as accepted solution and helpful.

View solution in original post

Hi @harinya 

 

You can replace 

   total_quantity += parseInt(oMrvs[i].quantity);

 

by 

   total_quantity += parseFloat(oMrvs[i].quantity);

Help others to find a correct solution by marking the appropriate response as accepted solution and helpful.

View solution in original post

22 REPLIES 22

vasu17
Tera Contributor

changed the quantity_updated  variable type to checkbox
and made isolate script check box to false

Strange, it's working just fine for me.

 

PeterBodelier_0-1700228438202.png

 

 

Is the isolate script checkbox false on all 3 client scripts, and are all used field names correct?

 

 


Help others to find a correct solution by marking the appropriate response as accepted solution and helpful.

You could also try to set the timeout value to 1000 ms instead of 500, to give the instance more time to process the submit.


Help others to find a correct solution by marking the appropriate response as accepted solution and helpful.

Hi @Peter Bodelier ,
Yes ,isolate script checkbox false for all 3 client scripts, field names also given correctly
Thanks for your Time 
even i tried in PDI as well but still No luck

Add alerts to each script to confirm they are running and to see how far it's getting, then you'll discover where you have a script not configured correctly, or a typo or whatever. You didn't show the full configuration of your onSubmit script - is it applied to the Variable Set, not the Catalog Item?  It may also clarify things to see your MRVS definition screen.

 

Keep in mind that once this is working for you that the deletion of row(s) via the X icon or Remove All button will not trigger a re-calculation.  You could repeat the onChange code in an onSubmit script (applied to the Catalog Item) to ensure the total is correct when the item is submitted.