UI action Help Needed

SandeepKSingh
Kilo Sage

I have been asked with creating a user selection form in ServiceNow where the list of users displayed depends on the selected location. Using Jelly scripting and JavaScript, implement a solution that dynamically updates the reference qualifier for the user field based on the location selected by the user.

  • Instructions:
    • Implement a Jelly script that creates a reference field for selecting a location from the cmn_location table.
    • Add another reference field to select a user from the sys_user table, which should be filtered dynamically based on the selected location.
    • Ensure that when a location is selected, the reference qualifier for the user field is updated to only show users associated with that location.
    • Include JavaScript logic to handle the dynamic filtering based on the selected location.
1 ACCEPTED SOLUTION

Ravi Gaurav
Giga Sage
Giga Sage

Hello,

you can write the below script in the UI page:-

 

HTML :-

<j:jelly trim="false" xmlns:j="jelly:core" xmlns:g="glide" xmlns:j2="null" xmlns:g2="null">


Select the Location: <g:ui_reference name="location" id="location" table="cmn_location" completer="AJAXTableCompleter" columns="" onchange="setLocationReference()"/><br />


Select the User: <g:ui_reference name="user" id="user" table="sys_user" completer="AJAXTableCompleter" columns="" query=""/>
</j:jelly>
and Client Script as  :-
function setLocationReference(){


  // Get the elements & update the qualifier


  var locationVal = gel('location').value;
alert(locationVal)



  var x = gel('sys_display.user');


  x.onfocus();


  x.ac.referenceSelect('', '');


  x.ac.setRefQual("location=" + locationVal);


  x.ac.cacheClear();



  // Update the onclick for the lookup


  var userLookup = gel('lookup.user');


  var userOnClick = "mousePositionSave(event);reflistOpen( 'user', 'not', gel('userTABLE').value, '', 'false', 'QUERY:location=" + locationVal + "', 'location=" + locationVal + "', '')";


  userLookup.setAttribute('onclick', userOnClick);

}

It should work Ex:-
RaviGaurav_0-1724335305998.png

 

--------------------------------------------------------------------------------------------------------------------------


If you found my response helpful, I would greatly appreciate it if you could mark it as "Accepted Solution" and "Helpful."
Your support not only benefits the community but also encourages me to continue assisting. Thank you so much!

Thanks and Regards
Ravi Gaurav | ServiceNow MVP 2025,2024 | ServiceNow Practice Lead | Solution Architect
CGI
M.Tech in Data Science & AI

ï”— YouTube: https://www.youtube.com/@learnservicenowwithravi
ï”— LinkedIn: https://www.linkedin.com/in/ravi-gaurav-a67542aa/

View solution in original post

3 REPLIES 3

Ravi Gaurav
Giga Sage
Giga Sage

Hello,

you can write the below script in the UI page:-

 

HTML :-

<j:jelly trim="false" xmlns:j="jelly:core" xmlns:g="glide" xmlns:j2="null" xmlns:g2="null">


Select the Location: <g:ui_reference name="location" id="location" table="cmn_location" completer="AJAXTableCompleter" columns="" onchange="setLocationReference()"/><br />


Select the User: <g:ui_reference name="user" id="user" table="sys_user" completer="AJAXTableCompleter" columns="" query=""/>
</j:jelly>
and Client Script as  :-
function setLocationReference(){


  // Get the elements & update the qualifier


  var locationVal = gel('location').value;
alert(locationVal)



  var x = gel('sys_display.user');


  x.onfocus();


  x.ac.referenceSelect('', '');


  x.ac.setRefQual("location=" + locationVal);


  x.ac.cacheClear();



  // Update the onclick for the lookup


  var userLookup = gel('lookup.user');


  var userOnClick = "mousePositionSave(event);reflistOpen( 'user', 'not', gel('userTABLE').value, '', 'false', 'QUERY:location=" + locationVal + "', 'location=" + locationVal + "', '')";


  userLookup.setAttribute('onclick', userOnClick);

}

It should work Ex:-
RaviGaurav_0-1724335305998.png

 

--------------------------------------------------------------------------------------------------------------------------


If you found my response helpful, I would greatly appreciate it if you could mark it as "Accepted Solution" and "Helpful."
Your support not only benefits the community but also encourages me to continue assisting. Thank you so much!

Thanks and Regards
Ravi Gaurav | ServiceNow MVP 2025,2024 | ServiceNow Practice Lead | Solution Architect
CGI
M.Tech in Data Science & AI

ï”— YouTube: https://www.youtube.com/@learnservicenowwithravi
ï”— LinkedIn: https://www.linkedin.com/in/ravi-gaurav-a67542aa/

KevinBellardine
Kilo Sage

This isn't what you asked but is there a reason you're using jelly script and a UI Page? You could do this using g_modal, which has the advantage of being supported in workspace as well. See the link below to another community page.

 

https://www.servicenow.com/community/developer-forum/filter-a-reference-field-in-g-modal-in-workspac...

Jim Coyne
Kilo Patron

As @KevinBellardine mentioned, why the requirement for Jelly?  And what exactly do you mean by "a user selection form"?  What is the "form" used for?  What's the use case?

 

The "Instructions" are a bit odd: is this for a test/exam?