Dynamically populate choice list based on user

Ehab Pilloor
Mega Sage

Hi all,

 

I have a catalog item in my instance where the options available in a choice list depend on the selection made in a reference field. What is the best practice way to implement this? 

 

Regards,

Ehab

1 ACCEPTED SOLUTION

Ubada Barmawar
Giga Guru

Hi @ehab

 

below is the feasible solution and included script snippets.

 

Create a Script Include:

  • Define a Script Include that contains a function to return options for the choice list based on the selected reference field value.

 

 

var DynamicChoiceList = Class.create();
DynamicChoiceList.prototype = {
   initialize: function() {},
   
   // Method to return choice list options based on reference field value
   getOptions: function(referenceValue) {
      var options = [];
      
      // Customize options based on referenceValue
      switch(referenceValue) {
         case 'Option1':
            options.push({ value: 'Option1A', label: 'Option 1A' });
            options.push({ value: 'Option1B', label: 'Option 1B' });
            break;
         case 'Option2':
            options.push({ value: 'Option2A', label: 'Option 2A' });
            options.push({ value: 'Option2B', label: 'Option 2B' });
            break;
         // Add more cases as needed
      }
      
      return options;
   }
};

 

 

Create a Catalog Client Script:

  • Implement a Catalog Client Script to dynamically populate the choice list options based on user input.

 

 

function onChange(referenceField) {
   var choiceList = g_form.getControl('choice_list_field_name');
   var selectedValue = g_form.getValue(referenceField);
   
   // Call the Script Include function to get options based on selectedValue
   var dynamicOptions = new DynamicChoiceList().getOptions(selectedValue);
   
   // Clear existing options
   choiceList.innerHTML = '';
   
   // Populate choice list with dynamic options
   dynamicOptions.forEach(function(option) {
      choiceList.options.add(new Option(option.label, option.value));
   });
}

 

 

please value the efforts of community members, if my answer helped then please mark this solution as helpful and accepted.

 

Regards,

Ubada Barmawar.

View solution in original post

3 REPLIES 3

Deepak Shaerma
Kilo Sage

Hi 

This is a common scenario is to have dynamic choice lists whose options depend on the selection made in a reference field. This can be implemented effectively using Catalog Client Scripts.
Type: onchange
 Field Name: u_reference_field

 

 

 

 

 

 

if (isLoading || newValue === '') {
    return;
}

// Clear the current options in the choice field
g_form.clearOptions('u_choice_field');

// Call a Script Include to get the choices based on the reference field value
var ga = new GlideAjax('GetEmployeeOptions');
ga.addParam('sysparm_name', 'getOptions');
ga.addParam('sysparm_department', newValue); // Pass the reference field value
ga.getXMLAnswer(function(response) {
    var options = JSON.parse(response);
    options.forEach(function(option) {
        g_form.addOption('u_choice_field', option.value, option.label);
    });
});

 

 

 

 

 

 

Create the Script Include

Go to System Definition > Script Includes and create a new Script Include:

Name: GetEmployeeOptions
Client Callable: True
Script:

 

 

 

 

 

 

var GetEmployeeOptions = Class.create();
GetEmployeeOptions.prototype = Object.extendsObject(AbstractAjaxProcessor, {
    getOptions: function() {
        var department = this.getParameter('sysparm_department');
        var employees = [];

        var gr = new GlideRecord('sys_user');
        gr.addQuery('department', department);
        gr.query();
        while (gr.next()) {
            employees.push({
                label: gr.name.toString(),
                value: gr.sys_id.toString()
            });
        }

        return new JSON().encode(employees);
    }
});

 

 

 

 

 

 

Please Mark this Helpful and Accepted Solution. If this Helps you to understand. This will help both the community and me..
- Keep Learning ‌‌
Thanks & Regards
Deepak Sharma 

Slava Savitsky
Giga Sage

Does your second field have to be a choice field? If you could use a reference field instead, you would be able to use a reference qualifier to control available choices based on selection in your other field.

Ubada Barmawar
Giga Guru

Hi @ehab

 

below is the feasible solution and included script snippets.

 

Create a Script Include:

  • Define a Script Include that contains a function to return options for the choice list based on the selected reference field value.

 

 

var DynamicChoiceList = Class.create();
DynamicChoiceList.prototype = {
   initialize: function() {},
   
   // Method to return choice list options based on reference field value
   getOptions: function(referenceValue) {
      var options = [];
      
      // Customize options based on referenceValue
      switch(referenceValue) {
         case 'Option1':
            options.push({ value: 'Option1A', label: 'Option 1A' });
            options.push({ value: 'Option1B', label: 'Option 1B' });
            break;
         case 'Option2':
            options.push({ value: 'Option2A', label: 'Option 2A' });
            options.push({ value: 'Option2B', label: 'Option 2B' });
            break;
         // Add more cases as needed
      }
      
      return options;
   }
};

 

 

Create a Catalog Client Script:

  • Implement a Catalog Client Script to dynamically populate the choice list options based on user input.

 

 

function onChange(referenceField) {
   var choiceList = g_form.getControl('choice_list_field_name');
   var selectedValue = g_form.getValue(referenceField);
   
   // Call the Script Include function to get options based on selectedValue
   var dynamicOptions = new DynamicChoiceList().getOptions(selectedValue);
   
   // Clear existing options
   choiceList.innerHTML = '';
   
   // Populate choice list with dynamic options
   dynamicOptions.forEach(function(option) {
      choiceList.options.add(new Option(option.label, option.value));
   });
}

 

 

please value the efforts of community members, if my answer helped then please mark this solution as helpful and accepted.

 

Regards,

Ubada Barmawar.