Skip to Content

Reference Field Filtering

Filter options in a reference field based on another field's value (e.g., show only assignment groups for the selected category).

onChange Table: incident Field: category
#reference-field #filter #dependent-fields #onChange

Script Code

JavaScript
1function onChange(control, oldValue, newValue, isLoading, isTemplate) {
2  // Exit if form is loading
3  if (isLoading) {
4    return;
5  }
6
7  // Configuration: Define reference field to filter and the relationship
8  var referenceFieldToFilter = 'assignment_group';
9  var filterField = 'u_category';  // Field on the reference table to match against
10
11  // Clear the reference field when category changes
12  if (oldValue !== newValue) {
13    g_form.clearValue(referenceFieldToFilter);
14  }
15
16  // Build and apply the filter
17  if (newValue !== '') {
18    // Create the filter query
19    // This will show only assignment groups where u_category matches the selected category
20    var filter = filterField + '=' + newValue;
21
22    // Apply the filter
23    g_form.addFilter(referenceFieldToFilter, filter);
24
25    // Optional: Add additional filters (e.g., only active groups)
26    g_form.addFilter(referenceFieldToFilter, 'active=true');
27  } else {
28    // If category is cleared, remove all filters
29    g_form.clearFilter(referenceFieldToFilter);
30  }
31}

How to Use

1. Create an onChange Client Script on your table 2. Set the field to the one that will control the filtering 3. Update `referenceFieldToFilter` and `filterField` for your scenario 4. Adjust the filter logic as needed for your data model

Explore More Scripts

Browse our complete library of ServiceNow scripts

View All Scripts