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.

Widget set catalog item variable after c.server.update()

Ryan M
Giga Guru

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);
        }
    });

 

 

1 ACCEPTED SOLUTION

-O-
Kilo Patron
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);
});

View solution in original post

7 REPLIES 7

-O-
Kilo Patron
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);
});

Most excellent, this worked great.  Thanks!

Glad it works and you're most welcome. I appreciate the feedback too!

Subhendu1
Tera Expert

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