Create dependency between two Lists fields - Type Lists
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-25-2023 07:10 AM - edited 01-25-2023 07:15 AM
- Hi All,
I have two list fields 1. Region 2. Zones. I am using lists field to be able to select multiple values from both the fields.
User should be able to see the values in Zones field based on Region field.
For example:
if Region is selected as Asia-location, then I have to show only values - asia-east, asia-east1,asia-west, asia-north in Zone field.
And if Region is selected Europe -location, then i have to show values - Europe-east, europe-west in Zones field.
If they select both Asia-location and Europe-location, then they should be able to see only values of those two fields choice values.
I have written script include function and calling it in reference qualifier but Script include function works well in background script but whenever i am trying to call in reference qualifier it is not getting called
script include function for reference:
getDependent: function(lists){
var test = [];
var tester = new GlideRecord('sys_choice');
tester.addEncodedQuery('filter query of the Zones field choice values from sys_choice table ');
tester.query();
while(tester.next()){
test.push(tester.sys_id.toString());
}
return 'sys_idIN' + test;
},
Reference qualifier on Zone field
javascript: new scriptincludename.getDependent(current.u_region);
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-25-2023 09:41 AM
Reference qualifiers only work with reference fields.
If you want to limit the selections in a choice list you can set the choice list up as a dependent field and make each choice dependent on the value of another field.
If that doesn't work for you then you can use one of the 'g_form.removeOption' or 'g_form.addOption' methods described on the client script to manually add and remove options as other values on your form changes.
The best option if you really need reference qualifier-type functionality is to use a reference field.
Regards,
Johns
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-26-2023 06:10 AM
Hi @Johns Marokky - I have already configured choice lists and set the dependent value on the zone field. Since I need to select multiple values from both fields - Trying with list type.
I did try using client scripts as you mentioned: Seems that is also not working with list type and also I have almost 51 choices for Region field and 218 choices for Zone field(where it took me lot of time configure dependent value).
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-25-2023 09:53 AM
Hi @Nanda Kumar Thi,
i think i have a sollution for you trouble:
Go to the table where you have defined the Region and Zones fields.
Navigate to the "Dictionary" for the Zones field.
Scroll down to the "Dependent Fields" section and click on "New".
In the "Dependent Field" field, select the Region field.
In the "Reference Qualifier" field, enter the following script:
javascript:current.u_region.nil()
- In the "Reference Qualifier" field, enter the following script to filter the Zones based on the selected Region:
javascript:current.u_region.nil() || current.u_region.changes() || g_form.getReference('u_zone', populateZones);
function populateZones() {
var region = g_form.getValue('u_region');
var gr = new GlideRecord('u_zone');
gr.addQuery('u_region', region);
gr.query();
g_form.clearOptions('u_zone');
while (gr.next()) {
g_form.addOption('u_zone', gr.sys_id, gr.name);
}
}
This will make the Zone field dependent on the Region field. The script in the reference qualifier field will filter the available options in the Zones field based on the selected Region.
Note that you need to adjust the field names and table name in the script to match your specific implementation.
Click "Submit" to save the dependent field.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-25-2023 10:47 AM
What's the encoded query you're using? I don't think you need toString() and can just run test.push(tester.sys_id) instead. You can't return the array and do need to make it a string before the returning. try this after the while loop.
var str = test.toString();
return str;