
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-01-2022 08:51 AM
Widget novice here.
I have a widget that we use to dynamically display some data on a catalog item. It monitors changes to a variable (configuration item) and then makes a server call to see if some specific data exists for that ci. If it does it renders the HTML. After the c.server.update() happens I also want to update a variable on the catalog item that lets me know if data was found. (Using $scope.page.g_form.setValue('ci_data', c.data.display);)
My issue is that the value of c.data.display is the value before the most recent call to c.server.update(); How do I update my catalog item variable to a value set in the most recent run of the server script?
HTML
<div ng-if="c.data.display">CI information goes here.</div>
Client Controller
$scope.$watch(function () {
return $scope.page.g_form.getValue('configuration_item');
}, function (ci) {
if (ci) {
c.data.ci = ci;
c.server.update();
$scope.page.g_form.setValue('ci_data', c.data.display);
}
});
Solved! Go to Solution.
- Labels:
-
Scripting and Coding
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-01-2022 10:45 AM
c.server.update()
returns a Promise
, so if you write the code
c.server.update();
$scope.page.g_form.setValue('ci_data', c.data.display);
the 2nd instruction will be executed as soon as the server call is issued, long before the answer arrives.
Perhaps try
c.server.update().then(onPromiseFulfilled(c, $scope.page.g_form));
function onPromiseFulfilled (c, g_form) {
return function (data) {
c.data.display = data.display;
g_form.setValue('ci_data', c.data.display);
}
}
You can also "save" a $watch
by setting up an event handler:
$scope.page.g_form.$private.events.on('onChange', function (fieldName, originalValue, newValue) {
// Replace with actual code checking the modified field and acting when necessary
console.log('onChange', fieldName, originalValue, newValue);
});
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-01-2022 10:45 AM
c.server.update()
returns a Promise
, so if you write the code
c.server.update();
$scope.page.g_form.setValue('ci_data', c.data.display);
the 2nd instruction will be executed as soon as the server call is issued, long before the answer arrives.
Perhaps try
c.server.update().then(onPromiseFulfilled(c, $scope.page.g_form));
function onPromiseFulfilled (c, g_form) {
return function (data) {
c.data.display = data.display;
g_form.setValue('ci_data', c.data.display);
}
}
You can also "save" a $watch
by setting up an event handler:
$scope.page.g_form.$private.events.on('onChange', function (fieldName, originalValue, newValue) {
// Replace with actual code checking the modified field and acting when necessary
console.log('onChange', fieldName, originalValue, newValue);
});

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-04-2022 05:58 AM
Most excellent, this worked great. Thanks!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-04-2022 08:08 AM
Glad it works and you're most welcome. I appreciate the feedback too!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-28-2024 10:17 PM
https://support.servicenow.com/kb?id=kb_article_view&sysparm_article=KB0715296
g_setValue is not working for me what is the alternative @Ryan M