onChange Widget?

conanlloyd
Giga Guru

I have a requirement to display a description, image and clickable url for the selected ergonomic keyboard.   I have a catalog client script that uses glideAjax to get the description, image and url from the u_ergonomic_equipment table and sets the value of the macro field that uses the widget to   "Item Description:\n" + answer.description + "\n\n\nItem Image:\n" + answer.image + "\n\n\nItem URL:\n" + answer.url  

Since a UI Macro isn't available through the portal I'm going the widget route.   I have a working rough draft using this code

Body HTML template:

<div>      

  {{ c.data.message }}  

</div>

Client controller:

function($scope) {  

        var c = this;  

 

        //Watch for changes in the name variable  

        $scope.$watch(function () {  

                  return $scope.page.g_form.getValue('ergo_keyboard_choice');  

        }, function (value) {  

        //Update local data object with data from variable  

        c.data.message = value ? value : '';  

});  

}  

Which produces this:

Screen Shot 2017-11-28 at 8.27.19 PM.png

Can anyone help me populate this HTML template instead?

<div>      

  <p><b>Description: </b><br>{item description here}</p>

  <p><b>Image: </b><br>{item image here}</p>

  <p><b>Link: </b><br>{item url here}</p>

</div>

1 ACCEPTED SOLUTION

conanlloyd
Giga Guru

With nathanfirth's guide as a starting point, I managed to figure it out.   Here's my code in case it helps someone else in the future.



HTML:


<div>


<strong>Item Description:</strong><br />{{c.data.description}}<br />


</div>


<div align="center"><img src="{{c.data.image}}" width="75%" height="75%">


<br />


<br />


</div>



Server Script:


(function() {


  /* populate the 'data' object */


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


var gr = new GlideRecord("u_ergonomic_equipment");


gr.addQuery("sys_id", input.message);


gr.query();


if (gr.next()) {


data.url = gr.getDisplayValue('u_link');


data.image = gr.getDisplayValue('u_image');


data.description = gr.getDisplayValue('u_item_description');  


}





})();



Client Controller:


function($scope) {  


        var c = this;  


 


        //Watch for changes in the name variable  


        $scope.$watch(function () {  


                  return $scope.page.g_form.getValue('ergo_keyboard_choice');  


        }, function (value) {  


        //Update local data object with data from variable  


c.data.message = value;


c.server.update().then(function(response){


});




});


 


}



Pics of the finished product.



Before selecting an Ergo Keyboard:


Screen Shot 2018-01-24 at 7.13.34 PM.png



And after selecting a keyboard:


Screen Shot 2018-01-24 at 7.14.01 PM.png



Hope this helps someone.


View solution in original post

6 REPLIES 6

I tried doing this as well but my images aren't stored locally, they are just a URL listed on the table form, so if the vendor updates the image our image updates as well.  I have c.data.url set as the source in the HTML but all I get is blank space.  I know if I do a background script query using 

 

getCurrent();

var currentVar;
var value;

function getCurrent() {
var gr = new GlideRecord('u_accessory');
gr.addQuery('sys_id', '319f8397dbf8eb8093deea42ca961933');
gr.query();
while(gr.next()) {
currentVar = gr.getValue('u_name');
gs.print("The name is " +gr.u_name);
gs.print(currentVar);
value = gr.u_image_link;
gs.print(value);
}
}

 

and insert the sys ID of the entry manually, the print does show it successfully grabs the URL.  

 

Not sure why this isn't working at this point.  If a URL works in the widget as a src, and I know it does, and if this query grabs the URL, I believe the code you posted should also successfully grab it.  So I'm drawing a blank as to why this isn't working.

 

 

ServiceNowSteve
Giga Guru

After countless hours of searching this solved my issue, thank you!