How do I refresh a reference-qualified dropdown choice list

johnsgp
Mega Expert

I have set up a pair of fields on a form, call them first_choice and second_choice.

It doesn't really matter what the first_choice field is but for simplicity think of a String with a fixed Choice List.   For example, a list of countries.

The second_choice field is a Reference field referring to some table which has at least two non-system columns - the first column is a value from the first_choice list (like a foreign key).   For example, think of a list of cities in each country.   The Second Choices table has a column for country and a column for city.   It has a row for each {country, city} pair which is of interest to the application.

Requirement: When the user selects a first_choice (a country), I want the second_choice to be a dropdown list of values related to the first_choice (the cities in that country).

Problem: The second_choice dropdown list does not refresh its contents when I change the first_choice (the cities of the country that was present when the form was loaded remain in the dropdown list).   I have to save/update the form to refresh the list.   This is a problem for two reasons.   Firstly, it lets me save wrong combinations (there is no error/warning when the city doesn't relate to the country).   Secondly, if second_choice is a mandatory field, I cannot save a new record because it starts off being empty and after I select a value for first_choice, the second_choice dropdown remains empty.   I cannot submit/save to refresh the dropdown because the second_choice is mandatory.

I have almost completed the solution.   The second_choice field has this configuration:

  • Type: Reference
  • Use reference qualifier: Advanced
  • Reference qual: javascript: new ChoiceMapper().getSecondChoice(current.first_choice);
  • Attributes: ref_auto_completer=AJAXReferenceChoice
  • Choice List Specification: Dropdown with -- None --

(ChoiceMapper is a custom Script Include.)

I also have a client script which sets the second_choice field to blank when the first_choice is changed to prevent invalid combinations being selected.

function onChange(control, oldValue, newValue, isLoading, isTemplate) {

    if (isLoading || newValue == '') {

          return;

    }

    g_form.setValue('second_choice', '');

}

How can I force a refresh of the dropdown list contents (i.e. call the reference qualifier again) when the first_choice is changed?

Note: There is no problem if I do not use a dropdown list (i.e. have a field where I can enter text and a magnifying glass to find the valid second_choices.   But this is not as user-friendly.

1 ACCEPTED SOLUTION

johnsgp
Mega Expert

I found the answer here, with thanks to John Lampson.



The answer is to add this to the Attributes field of the Dictionary Entry of the second_choice field: ref_qual_elements=first_choice



In other words, my complete Attributes are: ref_auto_completer=AJAXReferenceChoice,ref_qual_elements=first_choice



It's also noted in the wiki, but is difficult to find on a very busy page, especially if like me you're searching for 'refresh' and not 'update': Dictionary Attributes - ServiceNow Wiki


View solution in original post

4 REPLIES 4

johnsgp
Mega Expert

I found the answer here, with thanks to John Lampson.



The answer is to add this to the Attributes field of the Dictionary Entry of the second_choice field: ref_qual_elements=first_choice



In other words, my complete Attributes are: ref_auto_completer=AJAXReferenceChoice,ref_qual_elements=first_choice



It's also noted in the wiki, but is difficult to find on a very busy page, especially if like me you're searching for 'refresh' and not 'update': Dictionary Attributes - ServiceNow Wiki


We are facing a similiar issue. The correct answer here does not work as expected in Jakarta Patch 1:



Use Case:


  1. Incident Form has two Reference Fields, that shall be rendered as choice list: "u_firstRefChoice", "u_secondRefChoice"
  2. u_secondRefChoiceshould has a Reference Qualifier javascript: "u_filterField=" + current.u_firstRefChoice,
  3. User opens the Incident and changes the Value of "u_firstRefChoice" from "Hardware" to "Software"


Problem: We observed two different behaviours for the Reference Qualifier while reading the field current.u_firstRefChoice in the Reference Qualifier


  • If u_secondRefChoice is not rendered as a Choice List: Reference qualifier is "u_filterField=Software" (the new Value of u_firstRefChoice that is not stored in the Database)
  • If u_secondRefChoice is rendered as a Choice List: Reference qualifier is "u_filterField=Hardware" (the old Value stored in the Database)


Does anybody have a clue why this is happen and how it can be fixed?


Community Alums
Not applicable

Hi Niclas,

 

Did you find a solution for this use case? We are facing similar issues.

 

Thank You,

Shruthi U B

G24
Kilo Sage

Here is an answer with screenshots and a link to working documentation:

https://www.servicenow.com/community/developer-forum/how-to-quot-refresh-quot-reference-qualifier/m-...