UI Page onChange get reference value

Katie A
Mega Guru

I have the following requirement:

I have a UI Page where the user selects the Assigned To User in a reference field.

We need to be able to get the Assigned To User's location value after that field is changed, but before the form is submitted.

Is it possible to call an AJAX script when a value changes on a UI Page?

1 ACCEPTED SOLUTION

Hi Chuck, I solved this by just running an AJAX call from an onChange script in the reference field.



<!--HTML-->


<g:ui_reference name="example_reference" id="example_reference" table="sys_user" query="active=true" onchange="exampleAjax=()"/>



//Client Script



function exampleAjax() {


  var example = gel("example_reference").value;


  var ajaxexample = new GlideAjax('ScriptIncludeName');


  ajaxexample.addParam('sysparm_name','methodName');


  ajaxexample.addParam('sysparm_examplevar', example);


  ajaxexample.getXML(getResponse);


}


View solution in original post

4 REPLIES 4

Chuck Tomasi
Tera Patron

You can do this in the Client Script section of the UI page. However, objects like g_form are not available to you as it is not a "true" client script from the database.



You might consider using AngularJS and a REST call to retrieve the information in a responsive way.



This might be helpful:



ServiceNow Developers


Hi Chuck, I solved this by just running an AJAX call from an onChange script in the reference field.



<!--HTML-->


<g:ui_reference name="example_reference" id="example_reference" table="sys_user" query="active=true" onchange="exampleAjax=()"/>



//Client Script



function exampleAjax() {


  var example = gel("example_reference").value;


  var ajaxexample = new GlideAjax('ScriptIncludeName');


  ajaxexample.addParam('sysparm_name','methodName');


  ajaxexample.addParam('sysparm_examplevar', example);


  ajaxexample.getXML(getResponse);


}


Hello All,



I have a similar requirement where the catalog item has a variable of reference type referring to "sys_user" table.



I need to get the selected user first name,middle name, country,etc.



How to achieve it in UI Page?



Please share your thoughts.


Community Alums
Not applicable

Hi kutvaram,

If you have selected your user then you have to just call this function in your client script part of UI page.

 

function getDetails(){

 var sysIdContainer = //"in this variable you will get your selected sys id"
 var user_sys_name = '';  //it will store name and country of user

 var gr = new GlideRecord('sys_user');
 gr.addQuery('sys_id', sysIdContainer);
 gr.query();
 if (gr.next()) {
  user_sys_name = (gr.first_name+' '+gr.last_name+' '+gr.location);
}