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

Working on it, and almost there 🙂


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

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.

HI @Peter Bodelier ,
Thanks for your help,
but no luck for me , it's not working 
I have created the hidden variable (quantity_updated) which is type as single line text
i don't see ant value on Total Quantity field, please help on this

vasu17_3-1700226003888.png

 


this is my onload client script

vasu17_0-1700225692884.png

Onchange client script 

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("quantity");// this is MRVS Variable 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);

}
vasu17_1-1700225752590.png

 Finally onsubmit client script 

vasu17_2-1700225828667.png

 

Hi @vasu17,

 

Sorry, I didn't specify the field type. quantity_updated should be a checkbox type.

 

In line 9 of the Onchange Client script, is the name of the MRVS quantity? I think this is just the field name. You need to fill the name of the MRVS.

Also you might need to turn off 'Isolate script' on the client scripts.


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

HI @Peter Bodelier ,
i have modified the script ,Even though it's not working 

vasu17_1-1700227922467.png

 

vasu17_0-1700227866716.png