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

gnagaraj4u
Kilo Contributor
@Manish Vinayak , the code you have mentioned is working for macro in ITIL view but the code used for widget is not working in service potal, could you please tell me what would have been the reason
 
 
Widget Client Controller Script:

function($scope) {
/* widget controller */
var c = this;

calculateTotal = function() {
try {
//Replace mrvs with your variable set name
var jsonStr = $scope.page.g_form.getValue("mrvs");
var objList = JSON.parse(jsonStr);
var result = 0;
for (var i = 0; i < objList.length; i++) {
//Replace variable_name with your variable's name
result += parseFloat(objList[i].variable_name);
}
//Replace variable_to_set with the name of variable where you want to set the value
$scope.page.g_form.setValue("variable_to_set", result);
} catch (e) {
console.log("Error in the UI Macro Client Script");
}
}
}
 
 
@Si Min , may i know did this code worked for you?