remove a dropdown option from the choice list

Prasnajeet1
Giga Guru

Hi All

I have a small query.

I am writing a onChange client script. my requirement is if "model_category" = "computer" then I need to remove a option from the "substatus" field in the alm_hardware table. model_category is a read-only reference field.

Below is my script.

onChange client script: is not working.

field: State

unction onChange(control, oldValue, newValue, isLoading, isTemplate) {
    if (isLoading || newValue === '') {
        return;
    }
    if (g_form.getValue('model_category') == '81feb9c137101000deeabfc8bcbe5dc4') {
        alert('test');
        g_form.removeOption('substatus', 'lease_return');
    }
Above script is working in onLoad client script but not working in OnChange client script. Can someone please help me. Where I am doing wrong or what need to change in it.
 
function onLoad() {
    //Type appropriate comment here, and begin script below
    if (g_form.getValue('model_category') == '81feb9c137101000deeabfc8bcbe5dc4') {
       g_form.removeOption('substatus', 'lease_return');
    }
}

}
1 ACCEPTED SOLUTION

@Prasnajeet1 , I just checked in my PDI so in your case in alm_hardware the substate is a dependent field on state so i tried onchange of state checked model category the choice value is not getting removed or getting added you can remove dependent value in substate but it will change for every model category.

 

if it is a dependent value then removeOption will work on onload but not on onchange so you might have to write a combo of both or as workaround if you know on which State choice you want to remove the option write it in onload client script only.

 

Servicenow 

https://support.servicenow.com/kb?id=kb_article_view&sysparm_article=KB0681253

 

Regards,

Swathi Sarang

View solution in original post

6 REPLIES 6

Subhashis Ratna
Tera Guru

Hi @Prasnajeet1 

For me it's working . could u please retry and let me know .

Scenario : When Caller is "Abel Tuter" then Remove "Chat" Option from Channel.

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

   //Type appropriate comment here, and begin script below

   if (g_form.getValue('caller_id') == '62826bf03710200044e0bfc8bcbe5df1') { // Caller = "Abel Tuter"
        alert('test'); 
        g_form.removeOption('contact_type', 'chat');
    }
   
}


 CS :

SubhashisRatna_0-1711124467994.png

 

O/P :
Now You can see the Caller is "David Milller" and Channel has same "Chat" option.

SubhashisRatna_2-1711124723001.png

 



after Caller changes to "Abel Tuter" , "Chat" option is not available in Channel.

SubhashisRatna_1-1711124697297.png

 

Note : If you are trying to remove a specific choice value from a variable , then kindly go through these links :


Link 1 : https://www.servicenow.com/community/developer-forum/g-form-removeoption-is-not-working-on-the-varia...

Link 2 : https://support.servicenow.com/kb?id=kb_article_view&sysparm_article=KB0678206

Please mark this comment as Correct Answer/Helpful if it helped you.

Regards,

Subhashis

 



Amit Pandey
Kilo Sage

Hi @Prasnajeet1 

 

Your script seems to be Okay. Have you selected correct field name? I think it should be Model Category instead of State. Try changing UI Type to All. And also, you can give a try to following script-

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
    if (isLoading || newValue === '') {
        return;
    }
    var modelCategory = g_form.getDisplayValue('model_category');
 
    if (modelCategory == 'computer') {
        g_form.removeOption('substatus', 'lease_return');
    }
}

 
Please mark my answer helpful and correct.

 

Regards,
Amit