Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

About the client controller for Service Portal widgets

Ro5
Tera Expert

hello.
I have a question about the Service Portal widget.
I want to create a new button type widget and when the button is clicked I want to get the value of the catalog item variable set and calculate the sum and display it in another variable field.
Therefore, I wrote the following script in the client controller of the newly created widget, but it did not work well.

 

api.controller=function($scope) {
/* widget controller */
var c = this;
c.setTotalAmount = function() {
var g_form = $scope.page.g_form;
var value = g_form.getValue('testset');
if (value) {
var mrvsData = JSON.parse(value.replace(/'/g, ''));

for (var i = 0; i < mrvsData.length; i++) {
var mrvs = mrvsData[i];
// get quantity
quantity = g_form.getValue(mrvs.name);
// calculate
total += parseInt(mrvs.price * quantity);
}
}

//Set Value in the Total Cost
g_form.setValue('total_price', total);
};
};

 

I would like to know if anyone knows where I am going wrong.

Regards,
Ro

1 ACCEPTED SOLUTION

Can tou tre below:

var g_form = $scope.page.g_form;     // remove this line
var value =$scope.page.g_form.getValue('testset');     // try this directly and alert value to check if you get the value of your variable.


Raghav
MVP 2023
LinkedIn

View solution in original post

5 REPLIES 5

hello @Ro5 are you sure that you are using the correct MVRS internal name ?

 

try double checking the internal name once 

 

Also replace your JSON.parse line to below 

var mrvsData = JSON.parse(value);