Update Variable cart total for line items in MRVS

Tyler Johnson
Tera Expert

I recently have been trying to utilize previous solutions in the community to update my cart quantity based on the number of line items in my MRVS. My attempts have so far failed. If anyone can please look at my code and steps below to see where I messed up, please let me know. 

Created a MRVS 
Internal Name: contacts

Title: Contacts

 

Created single line text Quantity

Internal Name: quantity

Question: quantity

 

Created a Client Script

 

onLoad 

function onLoad() {
function onChange(control, oldValue, newValue, isLoading) {
   if (isLoading || newValue == '') {
      return;
   }
	var mvrsValue = g_form.getValue('contacts'); // change to variable set name
	if(mvrsValue){//if it has a value
	var jsonObj = JSON.parse(mvrsValue);
	quantity = jsonObj.length;
	alert('There are ' + quantity + ' rows');
	g_form.setValue('quantity', quantity);


} else { // if i recall correctly, .getValue returns null if empty
	alert('No Rows');
	g_form.setValue('quantity', 1);
}
}
};

I then created a BR 

Name: Cart Update

When to run: Before- Insert/Update

Table: sc_item_option

Filter Conditions : Question is quantity

 

(function executeRule(current, previous /*null when async*/ ) {

    // Add your code here
    // Get the Current Cart Item and Value from the catalog item option.


    var cartItem = current.cart_item;


    var qty = current.value;





    // Query the cart and update it's quantity value with the variable's value.


    var gr = new GlideRecord('sc_cart_item');


    gr.addQuery('sys_id', cartItem);


    gr.query();



    while (gr.next()) {


        gr.quantity = qty;


        gr.update();


    }


});
(current, previous);

 

1 REPLY 1

Community Alums
Not applicable

Hi @Tyler Johnson ,

 

If the calculation is based on MVRS then please use onChange Client script to update the quantity properly, onLoad will not work here.

Here is the updated script-

 

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

    var mvrsValue = g_form.getValue('contacts'); // MRVS variable set name
    if (mvrsValue) { // if it has a value
        var jsonObj = JSON.parse(mvrsValue);
        var quantity = jsonObj.length;
        g_form.setValue('quantity', quantity);
    } else { // if no rows are present
        g_form.setValue('quantity', 0); // setting to 0 instead of 1 for accuracy
    }
}

 

 

Updated BR-

Check the table where this is being triggered as it should be sc_item_option_mtom Please check once

 

(function executeRule(current, previous /*null when async*/) {
    // Get the Current Cart Item and Value from the catalog item option.
    var cartItem = current.cart_item;
    var qty = current.value;

    // Query the cart and update its quantity value with the variable's value.
    var gr = new GlideRecord('sc_cart_item');
    gr.addQuery('sys_id', cartItem);
    gr.query();
    while (gr.next()) {
        gr.quantity = qty;
        gr.update();
    }
})(current, previous);

 

 

Use alert to check the values being calculated to debug the client script.

for BR use gs.info or log to validate the data.

 

If my response has resolved your query, please consider giving it a thumbs up ‌‌ and marking it as the correct answer‌‌!

 

Thanks & Regards,

Sanjay Kumar