How to calculate the sum of the value in MRVS (multi row variable set)

Si Min
Giga Expert

Hi,

Coming across using the MRVS and wish to calculate the total amount for the number of books as shown in the pic.

Appreciate if anyone can enlighten me. I did do a checkup from this post but can't really get the idea. find_real_file.png

1 ACCEPTED SOLUTION

Hi Si Min,

 

The Client Script used in a widget is different than an on submit client script for a record / record producer / catalog item. It needs to use $scope to get access to g_form. However client scripts on Catalog Item, Record Producer and Ticket record have access to the GlideForm (g_form) client side API. 

Try the following for your onSubmit client script:

function onSubmit() {
	
	var totalEstimatedAmount = parseFloat(g_form.getValue('total_estimated_amount_sgd'));
	var jsonStr = g_form.getValue("book_details");
	var objList = JSON.parse(jsonStr);
	var calculatedAmount = 0;
	for (var i = 0; i < objList.length; i++) {
		
		calculatedAmount += parseFloat(objList[i].estimated_price_sgd);
		
	}
	calculatedAmount = calculatedAmount.toFixed(2);
	
	if (calculatedAmount != totalEstimatedAmount) {
		g_form.addErrorMessage('Total estimated amount does not match with entered Book Details info. Please click the Calculate Total Estimated Price again before submission.');
		return false;
	}
}

Hope this helps!

Cheers,

Manish

View solution in original post

20 REPLIES 20

Hi Manish,

I have a similar calculation on the Multi Row Variable. I followed the script you mentioned above but it doesn't work.

We need to sum up the Size column.

 

find_real_file.png

Ah clear.

Like Manish mentioned, "But there is one problem, you can't access the variables outside MRVS from client script of MRVS variable set." At least, until Madrid this is the thing. I haven't yet tested this on New York.

For a similar requirement, we have a sum field, filled upon submission of the request. Actually not thru a onSubmit Catalog Client Script, though thru an Async Business Rule.

If my answer helped you in any way, please then mark it as helpful.

Kind regards,
Mark

---

LinkedIn

 

Kind regards,

 

Mark Roethof

Independent ServiceNow Consultant

10x ServiceNow MVP

---

 

~444 Articles, Blogs, Videos, Podcasts, Share projects - Experiences from the field

LinkedIn

Just tried in New York. Still not possible thru onChange Catalog Client Script.

If my answer helped you in any way, please then mark it as helpful.

Kind regards,
Mark

---

LinkedIn

 

Kind regards,

 

Mark Roethof

Independent ServiceNow Consultant

10x ServiceNow MVP

---

 

~444 Articles, Blogs, Videos, Podcasts, Share projects - Experiences from the field

LinkedIn

Hi... I have something similar.. Not sure where it goes wrong.

 

find_real_file.png

 

You can see cost and total cost. Cost is in MRVS and total cost is a different variable.

 when we add rows and enter cost all the rows cost should add and populate in total cost.

Not able to do this. 

 Any help?

 

TIA 🙂 

siva_
Giga Guru

Hello Si Min, 

 

Please refer to the blog detailed by Mark here

 

Here he describes the way we can access the details in MRVS 

by using g_form.getValue("IO:Sys_id of MRVS");

you will get the details filled in MRVS as an array of  JSON objects where you can parse that array element objects and get the values for the particular column , add it and set them back

 

Hope this helps :

Mark this response as correct if that really helps

 

Thanks,

Siva