- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-29-2022 06:03 AM
Good Morning everyone,
i am task to create an expense request form in ServiceNow and have decided to use the MRV for the expense line items.
But one of the requirements is to get the total item cost listed on the form as they are entered into the MRV.
What is the best approach for this?
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-29-2022 07:32 AM - edited 11-29-2022 07:35 AM
Hello @Peter Williams ,
IF you want to do it on change of the MVRS then i can suggest you a solution which i used in my requirement and it worked for me .But its bit a lengthy process but we can do .
Follow this process step by step :
1)You need to handle this via hidden widgets on the record producer
create a widget with this code
api.controller = function($scope) {
/* widget controller */
var c = this;
var g_form = $scope.page.g_form;
$scope.$watch(function() {
return g_form.getValue('your_mvrs_internal_name');
}, function(value) {
if (value) {
var jsonData = JSON.parse(value);
var total = 0;
for (var i = 0; i < jsonData.length; i++) {
total=total+parseInt(jsonData[i].your_cost_field_name_inside_mvrs);
}
//Set Value in the total field
g_form.setValue('total_cost_field_name_outside_mvrs', total);
}
});
};
2)After this create a Variable of type Custom and add the Widget in the widget field like below
Hope this helps
Mark my answer correct if this helps you
Thanks
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-29-2022 07:56 AM - edited 11-29-2022 07:57 AM
This will work for both service catalog and record producers in service portal
also just FYI this will work only for service portal but in order to make it work for native view then we might have to do some jelly scripting in macro and add that macro above the widget field in the same variable
Hope this help
Mark the answer correct if this helps you
Thanks
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-29-2022 07:17 AM - edited 11-29-2022 10:05 AM
Hi Peter,
You can do this with a simple onSubmit Catalog Client Script that Applies to the MRVS, not the Catalog Item. It will retrieve any previously-added MRVS rows, sum the Cost, then add the Cost of the row that was just added and update the Catalog Item variable all by the time the MRVS dialog window closes.
function onSubmit() {
var totalCost = 0;
var mrvs = g_service_catalog.parent.getValue('expense_mrvs'); //MRVS internal name
if (mrvs.length > 2) { //native UI returns [] for empty MRVS value which causes a parsing error
var obj = JSON.parse(mrvs);
for (var i = 0; i < obj.length; i++) {
totalCost += parseInt(obj[i].cost); //your MRVS variable name
}
}
totalCost += parseInt(g_form.getValue('cost'));
if (this) { //Service Portal method
this.cat_g_form.setValue('total_cost, totalCost); //your variable name
} else { // native UI method
parent.g_form.setValue('total_cost', totalCost); //your variable name
}
}
If you are using Service Portal, there's another little onLoad script you need that applies to the Catalog Item:
function onLoad() {
if (this) {//we only need to do this for Service Portal
//We need to make the g_form object for the parent item available from the MRVS window
this.cat_g_form = g_form;
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-29-2022 07:21 AM
Thank you for this Brad,
do you know of a onChange script that we can used to sum up the total within the request and not wait on after its submitted?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-29-2022 07:32 AM
i am getting this from the script
function onSubmit() {
//Type appropriate comment here, and begin script below
var totalcost = 0;
var expense = parent.g_form.getValue('expense');
if(expense.length >2){
var objExpense = JSON.parse(expense);
for (var i=0; i<objExpense.length; i++){
totalcost +=parse(objExpense[i].cost);
}
}
totalcost+=parseInt(g_form.getValue('cost'));
parent.g_form.setValue('total_cost', totalcost);
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-29-2022 07:51 AM - edited 11-29-2022 10:07 AM
It sounds like you created the script with the Applies to field = 'A Catalog Item' instead of = 'A Variable Set'. MRVS onSubmit triggers when the Add button is clicked within the MRVS dialog window after populating the MRVS variables, so by the time you see the Total Cost variable back on the Catalog Item form, it will be updated - and all of this happens before the Catalog Item is submitted.