Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

How to add Drop down in string field for perticular condition

Manu143
Tera Contributor

Hi Community,

 

Urgent Help Require:

  I need to add drop down to the string field and that drop down should be visible only for the model category computer in hardware table.

 

Thanks,

Manu

4 REPLIES 4

Tai Vu
Kilo Patron
Kilo Patron

Hi @Manu143 

Let's try this API in your client script. You will be able to add choice value in a specific condition.

addOption(String fieldName, String choiceValue, String choiceLabel)

 

To make a string field act as dropdown list. You'll need to update the Choice List Specification in Dictionary.

TaiVu_0-1699337159934.png

 

Cheers,

Tai Vu

Manu143
Tera Contributor

Hi Tai Vu,

  I tried that as well but if I will  go with that approach the dropdown will be visible to other model category as well.That field will get change for everywhere and I just want that only for model category computer.

 

Thanks,

Manu

Hi,

You need to use addOption() along with removeOption() to get it worked as per your use case.

Hi @Manu143 

We can validate the Hardware Model Category before calling the API.

Sample below.

 

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

    if (newValue === '') {
        g_form.removeOption('<field_name>', '<choice_value>');
        return;
    }

    if (g_form.getValue('model_category') === '81feb9c137101000deeabfc8bcbe5dc4') {
        g_form.addOption('<field_name>', '<choice_value>', '<choice_label>');
        return;
    }

    g_form.removeOption('<field_name>', '<choice_value>');

}

 

 

If you'd like the script runs on load, just remove the isLoading condition.

If you don't want to hard-coding the sys_id of the Computer category, you can store it in a system property then call an AJAX to do the validation.

 

Cheers,

Tai Vu