How to Show/Hide Options using g_form in client script.

Prabhu20
Tera Contributor

Hi,

Actually I need to show/hide options on one field based on another field.
I can achieve this using g_form.clearOptions() , addOption() & removeOption(). But, its actually removing or adding  options.

Is there any other way available to show or hide options other than the above specified. ?

1 ACCEPTED SOLUTION

Hi Prabhu,

you should create entry into UI messages table for those drop down values and adding option should be possible using getMessage()

refer below links for help

translations for onChange catalog client script through getmessage() method not working

Coding Help for retrieving the value from sys_message table

How to use gs.getMessage() to get the string translated?

Regards
Ankur

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

View solution in original post

9 REPLIES 9

Ankur Bawiskar
Tera Patron
Tera Patron

Hi,

Unlike other fields such as reference where you can apply filter and hide value

Nothing is present for Choice

Only way is to show/hide using addOption() and removeOption()

Regards
Ankur

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

Hi Ankur,

Thanks for the reply.

What issue I am actually facing is, I am using spanish translations for options and if the client script (add/remove options) gets executed, then it translated texts will be removed.

Whether can I make use of getMessage() inside addOption() to preserve the translations part ?


With Regards,
Prabhu V

Hi Prabhu,

you should create entry into UI messages table for those drop down values and adding option should be possible using getMessage()

refer below links for help

translations for onChange catalog client script through getmessage() method not working

Coding Help for retrieving the value from sys_message table

How to use gs.getMessage() to get the string translated?

Regards
Ankur

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

Hi Ankur,

I have modified my client script like the above shared links.

if(selectedValue == 'Demo'){

        getMessage('Option1', function(msg){
        g_form.addOption('relationship_with_employee','Option1',msg);
        });


        getMessage('Option2', function(msg1){
        g_form.addOption('relationship_with_employee','Option2',msg1);
        });


        getMessage('Option3', function(msg2){
        g_form.addOption('relationship_with_employee','Option3',msg2);
        });

}

And I have created records in the Messages table also.

Here, What issue I am facing is I am getting translated value of only Option1. Option2 and Option3 remains untranslated.

Whether you got any suggestions ?



With Regards,
Prabhu V