Option with Dropdown --None--
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-17-2025 06:02 AM - edited 06-17-2025 06:36 AM
I have created one on change client script.' Time taken'(on change field) is selected as 1 hour then business application option should be removed and in other options 1 hour should be visible .
When i am selecting time taken as 1 hour then business application not visible (as expected).But after this i am selecting --None --- the 1 hour is still not visible.
Please confirm how can i so all option would be visible if --None-- slected.Providing the script ,which i am using.Please suggest.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-17-2025 06:54 AM
Hello @_bhishek,
g_form.getControl()
method is used to get the HTML element of the choice list, and then removeAttribute()
is used to remove the "hidden" attribute applied earlier.
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
var timeTakenControl = g_form.getControl('time_taken_field_name'); // Replace 'time_taken_field_name'
var businessAppOption = timeTakenControl.querySelector('option[value="business_application"]'); // Replace 'business_application'
if (newValue === '1 hour') {
// Hide Business Application option when '1 hour' is selected
if (businessAppOption) {
businessAppOption.setAttribute('hidden', 'hidden');
}
// Make sure other options are visible
for (var i = 0; i < timeTakenControl.options.length; i++) {
if (timeTakenControl.options[i].value !== '1 hour' && timeTakenControl.options[i].value !== 'business_application')
timeTakenControl.options[i].removeAttribute('hidden');
}
} else if (newValue === '--None--') {
// Show all options when '--None--' is selected
for (var i = 0; i < timeTakenControl.options.length; i++) {
timeTakenControl.options[i].removeAttribute('hidden');
}
} else {
// Handle other cases (e.g., other time options)
if (businessAppOption) {
businessAppOption.setAttribute('hidden', 'hidden');
}
for (var i = 0; i < timeTakenControl.options.length; i++) {
if (timeTakenControl.options[i].value !== '1 hour' && timeTakenControl.options[i].value !== 'business_application')
timeTakenControl.options[i].removeAttribute('hidden');
}
}
}
If this is helpful, please hit the thumbs up button and accept the correct solution by referring to this solution in future it will be helpful to them.
Thanks & Regards,
Abbas Shaik
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-17-2025 06:56 AM
Hello @_bhishek,
I will suggest you to go with UI policy.
When to Apply : Time taken is 1Hours
Check the Run Script checkbox and add below logic.
UI Type : All
Execute if true:
function onCondition() {
g_form.removeOption('u_classification','Business Application');
}
Execute if false:
function onCondition() {
g_form.addOption('u_classification', 'Business Application', 'Business Application');
}
Please mark this as "correct" and "helpful" if you feel this answer helped you in anyway.
Thanks and Regards,
Ashish
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-17-2025 07:19 AM
update script as this
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading) {
return;
}
// Remove "Business Application" when time taken is 1 hour
if (newValue == '1Hour') {
g_form.removeOption('u_classification', 'Business Application');
} else if (newValue == '' || newValue == '--None--') {
// Re-add "Business Application" when "--None--" is selected
// The value and label must match the original field definition
g_form.addOption('u_classification', 'Business Application', 'Business Application', 0);
}
}
If my response helped please mark it correct and close the thread so that it benefits future readers.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-17-2025 08:04 PM
Hope you are doing good.
Did my reply answer your question?
If my response helped please mark it correct and close the thread so that it benefits future readers.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader