← Script library

Client Scripts

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).

JavaScript
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
  // Exit if form is loading
  if (isLoading) {
    return;
  }

  // Configuration: Define reference field to filter and the relationship
  var referenceFieldToFilter = 'assignment_group';
  var filterField = 'u_category';  // Field on the reference table to match against

  // Clear the reference field when category changes
  if (oldValue !== newValue) {
    g_form.clearValue(referenceFieldToFilter);
  }

  // Build and apply the filter
  if (newValue !== '') {
    // Create the filter query
    // This will show only assignment groups where u_category matches the selected category
    var filter = filterField + '=' + newValue;

    // Apply the filter
    g_form.addFilter(referenceFieldToFilter, filter);

    // Optional: Add additional filters (e.g., only active groups)
    g_form.addFilter(referenceFieldToFilter, 'active=true');
  } else {
    // If category is cleared, remove all filters
    g_form.clearFilter(referenceFieldToFilter);
  }
}

How to use it

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

Adapt the table names, fields, and conditions to your instance. Test the behavior in a development environment before using it in production.