filter a list of records based on another variable

Hafila Hatta
Tera Guru

i have 2 variables below in my catalog item:

 

variable A: building code

variable B: asset code

 

variable A is a lookup select box based on another variable 

once variable A is selected, a list of values for asset code will be displayed based on the selected variable A , and multiple values can be selected

 

these 2 variables are from the same table

 

what is the best way to achieve this?

 

what i have tried:

- lookup select box for variable b but only single value can be selected

- list collector but unable to filter the values based on variable A

- variable set but the values are outside of the variableset 

4 REPLIES 4

Fabian Kunzke
Mega Sage

Heyhey,

 

If you only want one value to be selected, a lookup select box is probably best. For multiple selection, use a lookup multiple choice or a list collector (if you have a lot of choices). To re-filter the variable get some inspiration here: https://www.servicenow.com/community/developer-articles/creating-dependent-variables-in-service-cata...

 

If you want to reuse this variable combination, then yes, put it into a variable set.

 

Hope this helps,
Regards

Fabian

Ankur Bawiskar
Tera Patron

@Hafila Hatta 

variable B should be list collector type for allowing multiple selection and can restrict values based on other variable using reference qualifier

Also why both variables are referring to same table?

share some screenshots

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

@Hafila Hatta 

any update to this?

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

Sandesh Powar
Giga Guru

Hey @Hafila Hatta ,

Great question! You want to filter a multi-select variable based on another variable's selection. Here's the best approach:

Solution: Use a List Collector with Reference Qualifier

  1. Variable A (Building Code) - Keep as Lookup Select Box or Reference field
    • Type: Lookup Select Box or Reference
    • Reference: Your table (e.g., cmn_building)
  2. Variable B (Asset Code) - Set up as List Collector
    • Type: List Collector
    • Reference: Same table as Variable A
    • Reference Qualifier: javascript:current.variables.variable_a_name

Detailed Setup for Variable B:

In the Reference Qualifier field, use:

 
 
javascript
javascript:'building_code=' + current.variables.building_code

Replace building_code with your actual field name and building_code with Variable A's name.

If they're from the same table and you need to filter by the selected record:

Reference Qualifier:

 
 
javascript
javascript:'sys_id!=' + current.variables.building_code + '^building_code=' + current.variables.building_code.building_code

Alternative: Use Client Script for Dynamic Filtering

If the reference qualifier doesn't work perfectly, add an onChange Client Script on Variable A:

 
 
javascript
function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }
    
    // Get Variable B (list collector)
    var assetCodeField = g_form.getReference('asset_code');
    
    // Set reference qualifier dynamically
    var refQual = 'building_code=' + newValue;
    g_form.setReferenceQualifierForVariable('asset_code', refQual);
    
    // Clear previous selections
    g_form.clearValue('asset_code');
}

Pro Tips:

  • Make sure Variable A comes before Variable B in the order
  • Test in the Service Portal if you're using it - List Collectors work differently there
  • If using Variable Sets, both variables need to be in the same set for client scripts to work properly

If List Collector still doesn't filter: Check your Table ACLs - the reference qualifier might be getting blocked by security rules. You may need to adjust read ACLs on your table.

Let me know if you need help with the exact field names or run into any issues!

Hope this helps! If this resolves your issue, please mark it as the accepted answer so others can find the solution easily.

Best regards!
Sandesh