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.

Using reference qualifier to show values based on another field.

Shrivatsa Hegde
Tera Contributor

I have 2 reference fields in a catalog item. Both refers to different tables. I want to display second field's choices based on the first field choice selection. 

 

field1: issue - issue table

field 2 issue subcategory - issue subcategory table. 

 

issue is a common field in both the custom tables which refers to issue master table. 

 

How can I achieve this?

 

2 ACCEPTED SOLUTIONS

Vengateshwaran
Mega Guru

Hi @Shrivatsa Hegde ,

You can write a script include and call it in reference qualifier. I have done the same for category and sub-category I have attached the screenshot below.Script includeScript include

 

Screenshot (146).png

 

Mark my answer Helpful & Accepted if I have answered your question.

View solution in original post

Amit Gujarathi
Giga Sage
Giga Sage

HI @Shrivatsa Hegde ,
I trust you are doing great.
Please find the below catalog client script code

function onChange(control, oldValue, newValue, isLoading) {
   if (isLoading || newValue === '') {
      return;
   }
   
   var field1Value = g_form.getValue('field1'); // Get the value of the first reference field.

   // Clear the choices in field 2.
   g_form.clearOptions('field2');

   if (field1Value) {
      // Query the issue subcategory records based on the selected issue.
      var gr = new GlideRecord('issue_subcategory');
      gr.addQuery('issue', field1Value);
      gr.query();

      // Populate the choices in field 2 with the matching issue subcategories.
      while (gr.next()) {
         var value = gr.getUniqueValue();
         var displayValue = gr.getValue('name'); // Replace 'name' with the display field in the issue subcategory table.
         g_form.addOption('field2', value, displayValue);
      }
   }
}

Was this answer helpful?


Please consider marking it correct or helpful.


Your feedback helps us improve!


Thank you!


Regards,


Amit Gujrathi



View solution in original post

2 REPLIES 2

Vengateshwaran
Mega Guru

Hi @Shrivatsa Hegde ,

You can write a script include and call it in reference qualifier. I have done the same for category and sub-category I have attached the screenshot below.Script includeScript include

 

Screenshot (146).png

 

Mark my answer Helpful & Accepted if I have answered your question.

Amit Gujarathi
Giga Sage
Giga Sage

HI @Shrivatsa Hegde ,
I trust you are doing great.
Please find the below catalog client script code

function onChange(control, oldValue, newValue, isLoading) {
   if (isLoading || newValue === '') {
      return;
   }
   
   var field1Value = g_form.getValue('field1'); // Get the value of the first reference field.

   // Clear the choices in field 2.
   g_form.clearOptions('field2');

   if (field1Value) {
      // Query the issue subcategory records based on the selected issue.
      var gr = new GlideRecord('issue_subcategory');
      gr.addQuery('issue', field1Value);
      gr.query();

      // Populate the choices in field 2 with the matching issue subcategories.
      while (gr.next()) {
         var value = gr.getUniqueValue();
         var displayValue = gr.getValue('name'); // Replace 'name' with the display field in the issue subcategory table.
         g_form.addOption('field2', value, displayValue);
      }
   }
}

Was this answer helpful?


Please consider marking it correct or helpful.


Your feedback helps us improve!


Thank you!


Regards,


Amit Gujrathi