Filtering out records from the Reference Variable associated with Variable Set

Mike50
Kilo Guru

Hello Community,

 

I am looking for a way to filter out specific records from the Variable associated with a Variable Set, in the Catalog Item view.

Variable is coming from a generic Variable Set and it is of type Reference (to the core_company table) with a specific reference qualifier set. However, for one specific Catalog Item, I want to hide a few more companies from the available records, so that they cannot be selected.

 

Can you please advise if and how this can be achieved via Client Script?

Is there any alternative?

 

Thank you in advance for your help!

2 REPLIES 2

Ratnakar7
Mega Sage

Hi @Mike50 ,

 

You can use a client script. Here's an example of how you can achieve this:

  1. Identify the variable name of the reference variable in your variable set. Let's assume the variable name is company.

  2. Write a client script for the Catalog Item view. You can attach the client script to the catalog item or the variable set, depending on your requirements.

  3. In the client script, use the g_form API to modify the reference field's reference qualifier. The reference qualifier allows you to specify additional conditions to filter the available records.

 

function onLoad() {
  // Get the catalog item variable
  var variable = g_form.getControl('variable_name'); // Replace 'variable_name' with the actual variable name

  // Add a reference qualifier to the variable
  variable.setAttribute('reference_qual', 'active=true^u_field_to_filter!=value_to_exclude');
}

 

       4.  Customize the reference_qual attribute value according to your needs. You can use any valid ServiceNow query conditions to filter the records. In this example, active=true ensures that only active records are shown, and u_field_to_filter!=value_to_exclude filters out records where the u_field_to_filter field is not equal to the specified value_to_exclude. Replace u_field_to_filter and value_to_exclude with the appropriate field name and value for your scenario.

        5. Save and test the catalog item. The client script will dynamically apply the reference qualifier to the reference variable, filtering out the specific records based on your conditions.

Note: Keep in mind that client scripts run on the client-side, and users can potentially modify or bypass them. If you have strict security requirements, you may need to implement additional server-side logic to enforce the filtering.

Alternatively, you can explore other options such as business rules or UI policies to achieve the desired filtering behaviour. The specific approach may depend on your exact use case and requirements.

 

Thanks,

Ratnakar

onwork1948
Tera Contributor

To filter out records from a Reference Variable associated with a Variable Set in ServiceNow, you can use a dynamic filter option or a reference qualifier.