How to hide choices from choice list using client script onchange
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-15-2017 11:46 PM
How to hide choices from choice list using client script onchange, i want to hide few choices in subcategories when category is changed, i tried using g_form.removeOption('subcategory', '7'); but no luck, how to solve this?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-15-2017 11:53 PM
Hi Swathi,
Just check the value and fieldname while using removeOption method and it should work
Mark Correct if this solves your issue and also hit Like and Helpful if you find my response worthy based on the impact.
Thanks
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-15-2017 11:56 PM
Hi,
You can achieve this by making the Subcategory field dependent on the Category field as mentioned below:
1) Right click on the Subcategory field and click on configure Dictionary and then scroll down to the Dependent Tab and select the Dependent field as "Category" as shown below:
Once the dependent field has been setup, then While defining the choices there is a field called "Dependent Value" where you can define the parent choice value on which the child value should be dependent on as shown below:
Like for e.g. in the above screen shot Dependent Value is Hardware present as a choice value in the category field.
Or
You can do this with the help of a client script also as mentioned below:
Write an on change Client Script on the Category field and write the below script:
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
var cat = g_form.getValue('Category');
if(newValue == 'New Category Value') // Replace the New Category Value with the value you want to have the validation
{
g_form.clearOptions('Field Name');
g_form.addOption('u_Subcategory', 'choiceValue', 'choiceLabel ');
g_form.addOption('u_Subcategory', 'choiceValue', 'choiceLabel ');
}
else if(newValue == 'New Category Value')
{
g_form.clearOptions('Field Name');
g_form.addOption('u_Subcategory', 'choiceValue', 'choiceLabel ');
g_form.addOption('u_Subcategory', 'choiceValue', 'choiceLabel ');
}
Hope this helps.Mark the answer as correct/helpful based on impact.
Regards,
Shloke
Regards,
Shloke
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-16-2017 12:04 AM
Hi Swathi,
Try writing an UI policy.
In the 'When to Run' condition, give the required category name.
Goto Advanced View.
in the script, apply g_form.remove condition.
Regards,
Venkata Sneha

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-16-2017 12:06 AM
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
var val = g_form.getValue('fieldname');
alert(val);
if(val=='value you want')
{
alert('inside');
g_form.removeOption('fieldname', 'value');
}
Harish