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 for dynamic text onChange

rajeevsaraf
Tera Contributor

Hi ,

 

I like to generate a dynamic URL based on the user selected on catalog form, hence client controller script should be triggered onChange as well. 

I am using widget as custom variable.

 

Widget defination

HMTL Body template

<div>
<!-- your widget template -->
<a>In alignment with {{c.data.requested_for}} I accept the processing of order</a>
</div>

 

 

Server script

(function() {

/* populate the 'data' object */
/* e.g., data.table = $sp.getValue('table'); */


var gr = new GlideRecord("sys_user");
gr.addQuery("sys_id", input.message);
gr.query();
if (gr.next()) {
data.requested_for = gr.first_name;   
    }
})();

 
function($scope) {
  /* widget controller */
 var c = this;
    var g_form = $scope.page.g_form;
$scope.$watch(function () {
return $scope.page.g_form.getValue('requested_for');
}, function (value) {
    c.data.message = value;
    c.server.update().then(function(response){
});
    });
}
 
this is how it looks on portal
 
find_real_file.png
 
Many thanks in advance.
1 ACCEPTED SOLUTION

Sai Kumar B
Mega Sage

@rajeevsaraf 

Replace {{c.data.requested_for}} with {{data.requested_for}}

Replace gr.first_name with gr.getValue('first_name');

View solution in original post

2 REPLIES 2

Sai Kumar B
Mega Sage

@rajeevsaraf 

Replace {{c.data.requested_for}} with {{data.requested_for}}

Replace gr.first_name with gr.getValue('first_name');

Many thank, it did work. the only answer I am looking at - how this can be made dynamic when the request for user is changed. I other word the client script should be called on 'onChange' event as well.