Filtering choice field options based on a multi-select parent field

sanskarmish
Tera Contributor

Hi everyone,

I’m working on a form in ServiceNow (standard table form, not Service Catalog) and I’m trying to understand whether a specific requirement is technically possible.

Scenario

  • I have a field called record_type

    • Type: List (multi-select choice)

    • Example values: type_a, type_b, type_c

  • I also have a field called category

    • Type: Choice

    • This field contains all category options for all record types

Requirement

  • If record_type = type_a, the Category field should show only categories related to type_a

  • If record_type = type_b, it should show only categories related to type_b

  • If multiple values are selected (for example type_a and type_b), the Category field should show the combined set of categories for both

  • If nothing is selected, ideally no category options should be available



    Is there any solution 

4 REPLIES 4

Brad Bowman
Kilo Patron

Due to the multiple selections/combinations, a simple dependency won't work.  You'll need a Client Script similar to this onChange of the record_type variable.  With each value selected, the choice list will be wiped out then each valid choice is added based on your logic for the record type(s) selected.

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
   if (isLoading) {
      return;
   }

	g_form.clearOptions('category');

	var recTypeArr = newValue.toString().split(',');
	for (var i = 0; i < recTypeArr.length; i++) {
		//add an if block for each record_type value
		if (recTypeArr[i] == '0a52d3dcd7011200f2d224837e6103f2') { //sys_id of the record from the list table
			//add a line for each choice for this record_type
			g_form.addOption('category', 'cat_value', 'cat_string');
		}
    }
}

Thanks for the suggestion. Just to confirm, this field is a dictionary-based table field (not a Service Catalog variable). My understanding is that g_form.addOption() is supported for catalog variables but not for table choice fields. Can you confirm if this approach is supported in that context as well?

imanf
Kilo Contributor

his client script approach makes sense for handling multiple selections. Using g_form.clearOptions and then g_form.addOption for each selected record_type is a clean way to dynamically populate the category field.

Ankur Bawiskar
Tera Patron

@sanskarmish 

approach from @Brad Bowman  should work

Try that and share feedback

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader