Filtering out records from the Reference Variable associated with Variable Set
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-17-2023 02:42 PM
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!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-18-2023 09:41 AM
Hi @Mike50 ,
You can use a client script. Here's an example of how you can achieve this:
Identify the variable name of the reference variable in your variable set. Let's assume the variable name is company.
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.
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-26-2024 08:38 PM
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.
