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

Hi  @Brad Bowman  ,
Thanks for Response,
even i have tried that by creating Widget, but it's not working
My total cost variable is outside of MRVS 
when used the type as custom even it is not showing the variable in the form
i need to populate that value to total quantity field

You won't see the custom variable on the form, it's just a place-holder for the widget code.  I don't think the custom variables are supported in Service Portal / Employee Center.  There are a bunch of posts on this topic, so one of them ought to work with these interfaces.

Sohithanjan G
Kilo Sage
Kilo Sage

Hi @harinya ,

Try this 

// Client Script - onChange script for the quantity field in MRVS

function onChange(control, oldValue, newValue, isLoading) {
    // Check if the script is running during the initial loading of the form
    if (isLoading || newValue === '') {
        return;
    }

    // Get the current form
    var g_form = g_form.get();

    // Get the MRVS variable field name and the total quantity field name
    var mrvsFieldName = 'your_mrvs_variable_name'; // Replace with the actual name of your MRVS variable
    var totalQuantityFieldName = 'total_quantity'; // Replace with the actual name of your total quantity field

    // Get the MRVS variable value
    var mrvsValue = g_form.getValue(mrvsFieldName);

    // Calculate the total quantity
    var totalQuantity = calculateTotalQuantity(mrvsValue);

    // Update the total quantity field
    g_form.setValue(totalQuantityFieldName, totalQuantity);
}

function calculateTotalQuantity(mrvsValue) {
    var totalQuantity = 0;

    // Iterate through the MRVS rows and sum up the quantity values
    for (var i = 0; i < mrvsValue.length; i++) {
        totalQuantity += parseInt(mrvsValue[i].quantity, 10) || 0; // Convert quantity to integer, default to 0 if not a number
    }

    return totalQuantity;
}
Please mark as Accepted Solution if this solves your query and HIT Helpful if you find my answer helped you. This will help other community mates too..:)