Restrict list choices based on multiple fields
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-10-2024 06:07 AM - edited ‎04-10-2024 06:13 AM
Hey all,
I've been trying to make this work for a day or so and can't get the results I'm looking for.
It's simple to restrict the choices in a list field based on a dependent field. Category and Subcategory are already setup for this functionality, but I need a way to restrict the Subcategory field based on both the Category field and the Incident Type field.
I have two Incident Types: Dental and IT. I've already restricted the categories available based on the Incident type that is chosen, but I then need to restrict the subcategories based on this as well. So, if I choose Dental as the incident type and Hardware as the category, I only want a specific set of choices from the list of choices available under Hardware. Because Hardware is a choice for both Dental and IT Incident Types, I'm always getting all choices for Hardware under both Dental and IT.
I've tried using a Business Rule and a Client Script on the Incident table, but neither are doing what I need.
Client Script that I've tried:
function onChange() {
var category = g_form.getValue('category');
var type = g_form.getValue('u_incident_type');
if (type == 'Dental') {
alert('Help');
if (category == 'Hardware') {
g_form.clearOptions('subcategory');
g_form.addOption('subcategory', 'Computer', 'Computer');
}
}
}
The alert works, so I know the script is running, but it's not clearing the options. I can see that it is acting on the subcategory because it sets the value to Computer, but the other options are still there.
Also, I need this to work in the Service Portal as well.
Any help is much appreciated.
I've also posted this question on reddit.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-10-2024 06:13 AM
Hi @JB6
Go through this Community link : https://www.servicenow.com/community/developer-forum/client-script-g-form-removeoption/m-p/1407339
If this solution resolves your query, kindly mark it as the accepted solution and give it a thumbs up.
Thanks,
Subhashis Ratna
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-10-2024 07:12 AM
Hi @Subhashis Ratna,
I appreciate the example, but it's basically doing the same thing I'm trying. It seems like there is some other script or logic somewhere that is overwriting the script I'm writing. I can see when the conditions are met that the Subcategory field changes, but then it immediately changes back and all the options are still there. I've tried a UI Policy with Order > 1000, but that hasn't helped.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-10-2024 06:18 AM
You should be able to setup something like below choices without using scripts:
Incident Type | Category | Subcategory |
IT (it) | Hardware (it_hw) | Laptop (hw_laptop) |
Dental (dental) | Hardware (dental_hw) | Computer (hw_computer) |
Category it_hw will be dependent on 'it'.
Category dental_hw will be dependent on 'dental'.
Regards,
Sharad
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-10-2024 06:25 AM
Hey @maroon_byte, This issue that I'm running into is that Hardware is the same under both Dental and IT. So when you choose Hardware as the Category, all the choices for Subcategory are displayed no matter what the Incident type is. The only way I've been able to make your suggestion work is by creating separate Categories. For instance, instead of having just Hardware, I had to create Dental Hardware as the category and then the Subcategories would get the correct values because they were dependent on the Category field. I need a way to update the list of Subcategories based on what the Incident Type is. I can uncheck the 'Use dependent field' checkbox in the dictionary for Subcategory, but I would then have to write an unnecessarily long script to add back all the options for both Incident types.