Calculate total for MRVS

Casey Jones
Tera Contributor

I'm trying to calculate the price of a line in a MRVS. I will need to multiply the unit price and quantity and place that value into a total for that line.

 

I have been looking on the community for articles on how to complete this, but I have been unable to find something. I do see a lot of articles that would add the totals for each line and place that value into a field outside the MRVS.

3 REPLIES 3

jonsan09
Giga Sage
Giga Sage

I was able to get a value calculated but setting up two catalog client scripts on the MRVS. They are both on-change scripts one for when the price field changes and another for the when the quantity changes. The script multiples the two fields and then populates the total. Below is the script and some screenshots

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

    //Type appropriate comment here, and begin script below
    var calcQty = g_form.getValue('qty');
    var calcPrice = g_form.getValue('price');
    var calcTotal = calcPrice * calcQty;

    g_form.setValue('total', calcTotal);
}


2023-09-11 14_47_57-Calculate Total - QTY _ Catalog Client Scripts _ ServiceNow — Mozilla Firefox.png2023-09-11 14_47_49-Calculate Total - Price _ Catalog Client Scripts _ ServiceNow — Mozilla Firefox.png2023-09-11 14_47_37-Samsung Galaxy S7 Edge _ ServiceNow — Mozilla Firefox.png

what roles do we require to set the values in a catalog form. because my script is calculating the sum but it is not displaying in the field.

Siva Jyothi M
Mega Sage

Hi @Casey Jones,

 

To calculate the multiplication of two fields, ensure that both fields only accept numerical values.

Below is the script you need to write on change of both unit price and quantity.

quantity - qty_mr; unit price - unit_price_mr.

Onchange Client Script - Validate Interger Numeric(Unit price) and Validate Interger Numeric(Quantity)

 

function onChange(control, oldValue, newValue, isLoading) {
   if (isLoading || newValue == '') {
      return;
   }
 var regexp = /^[+]?\d*$/;
 if(!regexp.test(newValue)){
  alert('Please enter numeric value');
 g_form.setValue('qty_mr','');
 }
  }

 

Remember to modify the script if the fields should accept the decimal values.

You have to create two onchange client scripts for calculating the total price.

Onchange Client Script - Populate Total Price(Qty)

function onChange(control, oldValue, newValue, isLoading) {
   if (isLoading || newValue == '') {
      return;
   }
g_form.setValue('total_price_mr', parseFloat(g_form.getValue('unit_price_mr')) * parseFloat(newValue));
}

Onchange Client Script - Populate Total Price(Unit price)

function onChange(control, oldValue, newValue, isLoading) {
   if (isLoading || newValue == '') {
      return;
   }
	g_form.setValue('total_price_mr', parseFloat(g_form.getValue('qty_mr')) * parseFloat(newValue));
}

Replace parseFloat with parseInt if there are not decimal numbers.

 

Mark this answer as correct and helpful if it solves your issue.

 

Regards,

Siva Jyothi M.