- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-01-2019 03:07 AM
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.
Solved! Go to Solution.
- Labels:
-
Service Catalog

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-02-2019 09:48 PM
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

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-01-2019 03:15 AM
Hi there,
Could you describe how/where you want this calculation. The picture only shows an estimate now. Should those both prices be added, and displayed in a variable outside the MRVS for example?
Kind regards,
Mark
---
Kind regards,
Mark Roethof
Independent ServiceNow Consultant
10x ServiceNow MVP
---
~444 Articles, Blogs, Videos, Podcasts, Share projects - Experiences from the field
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-01-2019 05:13 PM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-01-2019 05:36 PM

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-01-2019 06:22 PM
Since this is a MRVS Variable set, you can't create a catalog client script on the item. You will have to create the client script on the variable set itself.
But there is one problem, you can't access the variables outside MRVS from client script of MRVS variable set.
i.e.,
g_form.getValue('total_estimated_amount')
OR
g_form.getValue('total_estimated_amount', totalCalculatedAmount)
isn't going to work.
My suggestion here would be, create a UI Macro with a button to Calculate Total Amount, and set the value to the total estimated amount field. But this will need an onSubmit validation as well, to see if the total amount was calculated before submission.
You will need to associate that macro variable with a UI Macro as well as a Widget (for service portal)
UI Macro Sample Code:
<?xml version="1.0" encoding="utf-8" ?>
<j:jelly trim="false" xmlns:j="jelly:core" xmlns:g="glide" xmlns:j2="null" xmlns:g2="null">
<script>
function calculateTotal() {
try {
//Replace mrvs with your variable set name
var jsonStr = 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
g_form.setValue("variable_to_set", result);
} catch (e) {
console.log("Error in the UI Macro Client Script");
}
}
</script>
<button type="button" class="btn btn-primary" onclick="calculateTotal()">Calculate</button>
</j:jelly>
Widget Sample Code:
HTML Template
<div>
<button type="button" class="btn btn-primary" onclick="calculateTotal()">Calculate Total Amount</button>
</div>
Client 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");
}
}
}
Hope this helps!
Cheers,
Manish