Catalog Client Script help

DreDay3000
Giga Guru

Hello, I am trying to hide a field option based on value selected from another field: 

  1. Hide ePO None option for ANG non-co-located
    - If “Type is ANG non-co-located”, hide the “None” option (created in step 2) for the “Which ePO affected?” question. We do not want the user to be able to select “None” as an option for that question which only shows when ANG is non-co-located.
    DreDay3000_0-1747057799102.png

     

     
    DreDay3000_0-1747057912793.png

     

1 ACCEPTED SOLUTION

Nilesh Pol
Tera Guru

@DreDay3000 

the issue with your current approach is that you're trying to manipulate the dropdown options using field.options[i].style.display = 'none', which won’t work reliably in the ServiceNow catalog environment.

use and verify below script thatsimplify your script using the g_form.removeOption() method, which is the recommended approach in ServiceNow for catalog variables: 

function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue === '') {
return;
}

var epoField = 'which_epo_affected'; 

g_form.addOption(epoField, 'none', 'None');

if (newValue === 'ANG_non_co_located') { 
g_form.removeOption(epoField, 'none');
}
}

View solution in original post

4 REPLIES 4

SumanthDosapati
Mega Sage
Mega Sage

@DreDay3000 

Two solutions :

  1. Make the field mandatory so that user can't select none and submit the request.
  2. uncheck the 'include none' checkbox in your variable configuration and create a new choice with name 'None' 
    so that in your client script you can simply add an if condition and write g_form.removeOption

Accept the solution and mark as helpful if it does, to benefit future readers.
Regards,
Sumanth

Nilesh Pol
Tera Guru

@DreDay3000 

the issue with your current approach is that you're trying to manipulate the dropdown options using field.options[i].style.display = 'none', which won’t work reliably in the ServiceNow catalog environment.

use and verify below script thatsimplify your script using the g_form.removeOption() method, which is the recommended approach in ServiceNow for catalog variables: 

function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue === '') {
return;
}

var epoField = 'which_epo_affected'; 

g_form.addOption(epoField, 'none', 'None');

if (newValue === 'ANG_non_co_located') { 
g_form.removeOption(epoField, 'none');
}
}

None still shows after updating the script

DreDay3000_1-1747063324841.pngDreDay3000_2-1747063346442.png

 

DreDay3000_0-1747063277512.png

 

@DreDay3000 Remove the None option in your question choices list and keep the script as it is.

 

Accept the solution and mark as helpful if it does, to benefit future readers.
Regards,
Sumanth