Change choice list in incident form through Client Script
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-21-2023 03:52 AM - edited 11-21-2023 04:04 AM
I have got a good amount of data in category and subcategory. For below code clarification, Subcategory is dependent on Category and Category is dependent on Services (Reference field). So, when I have same category for different services the subcategory adds up for all services i.e.
- Desktop Services (Service)
- Bloomberg (Category)
- Access (Subcategory)
- Test (Subcategory)
- Bloomberg (Category)
- Market Intelligence (Service)
- Bloomberg (Category)
- User Permission (Subcategory)
- Bloomberg (Category)
For this when I select Desktop Services as Service and Bloomberg as Category, it shows following options:
- Access (Subcategory)
- Test (Subcategory)
- User Permission (Subcategory)
How can I change this dynamically based on Service? Below I have written a script and It is not changing the choice list.
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
var services = g_form.getValue('business_service');
//alert(services);
if (newValue == 'bloomberg' && services=='42d08ef81ba2b5104d5920a2b24bcbd6') {
alert('desktop services');
g_form.clearOptions('u_subcategory');
g_form.addOption('u_subcategory', 'Access', 'access');
g_form.addOption('u_subcategory', 'test', 'test');
//g_form.removeOption('u_subcategory','user_permission');
}
else if (newValue == 'bloomberg' && services=='22d0cef81ba2b5104d5920a2b24bcb16') {
alert('Market Intelligence');
g_form.clearOptions('u_subcategory');
g_form.addOption('u_subcategory', 'User Permission', 'user_permission');
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-21-2023 06:53 AM - edited 11-21-2023 06:55 AM
Are your Category and Sub Category fields custom fields? Why because You mentioned like it is Incident table but you are using u_ for category and sub category. If they are not custom fields, try the below script.
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
var services = g_form.getValue('business_service') + '';
if (newValue == 'bloomberg' && services == '42d08ef81ba2b5104d5920a2b24bcbd6') {
g_form.clearOptions('subcategory');
g_form.addOption('subcategory', 'Access', 'access');
g_form.addOption('subcategory', 'test', 'test');
} else if (newValue == 'bloomberg' && services == '22d0cef81ba2b5104d5920a2b24bcb16') {
g_form.clearOptions('subcategory');
g_form.addOption('subcategory', 'User Permission', 'user_permission');
}
}
Please mark my answer helpful and accept as a solution if it helped 👍✔️
Anvesh
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-21-2023 09:08 PM - edited 11-21-2023 10:02 PM
Actually anvesh this is not used in Incident but is used in other form where subcategory field in not present. So u_subcategory is correct.
Thanks for your help.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-21-2023 07:21 AM
A different approach would be to have, for example, 2 Bloomberg categories with different values and dependent values so that the correct category is selected when the service is selected, then update the dependent values of your subcategories so that only the correct subcategories are displayed with no scripting. If you are unable to change the data, in this script attempt are you getting the expected alerts? If/once you are, and assuming u_subcategory is the correct field name, you are likely not seeing the choice values change due to the dependency - when category changes the system is retrieving the dependent subcategory choices, but your onChange script has already completed. What you can do is wrap each clearOptions/addOption, or removeOption (whichever is fewer actions/lines of code) block in an Interval function so that it waits a bit before running that part of the script:
if (newValue == 'bloomberg' && services=='42d08ef81ba2b5104d5920a2b24bcbd6') {
alert('desktop services');
setInterval(function() {
g_form.removeOption('u_subcategory','user_permission');
}, 2000);
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-21-2023 10:02 PM
Solution you provided is working fine but when I switch between Market Intelligence and Desktop Services and select bloomberg then all the options are removed after couple of switches.
Thank You for helping.