Option with Dropdown --None--

_bhishek
Tera Guru

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.

 

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
    if (isLoading || newValue === '') {
        return;
    }
    if (newValue == '1Hour') {
        g_form.removeOption('u_classification, 'Business Application', "Business Application");
    } else if (newValue == '') {
        g_form.addOption('u_classification', 'Business Application', "Business Application");
    } else {
        g_form.addOption('u_classification', 'Business Application', "Business Application");
    }
    //Type appropriate comment here, and begin script below

}
5 REPLIES 5

Abbas_5
Tera Sage
Tera Sage

Hello @_bhishek,

 

To resolve the issue where the "1 hour" option disappears when "--None--" is selected after choosing "1 hour", you need to ensure your script explicitly handles the "--None--" case and makes all options visibleHere's a breakdown of the problem and the solution with an example script:
 
Problem:
The original script likely hides the "1 hour" option when "1 hour" is selected and doesn't handle the scenario where the user then reverts to "--None--". The script needs to specifically check for "--None--" and make all options visible again.
Solution:
The 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. 
 
JavaScript
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

Ashish Parab
Mega Sage

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

 

Ankur Bawiskar
Tera Patron
Tera Patron

@_bhishek 

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.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

@_bhishek 

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.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader