Activate Choice List via Client Script

SebF
Tera Contributor

I have a String field named [u_keyword] on sys_user_group.  The choice List Specification is set to --None-- in the Dictionary entry.  This results in the field displaying on the form as free-form entry (as expected).

 

The requirement is that this field remain free-form unless another field [u_check] is set to 'true'.  In that scenario, the desire is to activate the choices on u_keyword.

 

I have found many posts about using g_form.addOption to add values to the choice list.  e.g.:

g_form.addOption('u_keyword', 'Option 1', 'Option 1');
 
But so far I've not found a method to activate the choice functionality on a field that is otherwise free-form.  Is that possible in a Client Script?
1 ACCEPTED SOLUTION

Community Alums
Not applicable

Hi @SebF ,

 

Your requirement for switching between a free-form text field and a choice list dynamically based on another field's value on a ServiceNow form is complex requirement, especially when considering that ServiceNow does not natively support toggling a field between a choice list and a text input directly.

You should consider the below ways as per best practice-

1. Either make u_keyword field read only unless u_check field is checked this way you can restrict users to only update when u_check is enabled. Not sure why text input is needed if its going to be a choice field later.

 

2. Or another effective way to handle this scenario is by using two separate fields: one as a free-form text field and the other as a choice field. You can then show or hide the appropriate field based on the value of the u_check field.

Steps-

Create a new choice field, e.g., u_keyword_choice

Set up the Choice List for u_keyword_choice as you require

 

Create a onChange Client Script to Toggle the fields-

function onChange(control, oldValue, newValue, isLoading) {
    toggleKeywordField();
}

function onLoad() {
    toggleKeywordField();
}

function toggleKeywordField() {
    var checkField = g_form.getValue('u_check');
    if (checkField == 'true') {
        // Show choice field and hide text field
        g_form.setDisplay('u_keyword_choice', true);
        g_form.setDisplay('u_keyword', false);
    } else {
        // Show text field and hide choice field
        g_form.setDisplay('u_keyword_choice', false);
        g_form.setDisplay('u_keyword', true);
    }
}

Note- Above are efficient ways to achieve your requirement, as there is no solution to change the field type from freeform to choice as its at dictionary level.

 

If my response has resolved your query, please consider giving it a thumbs up ‌‌ and marking it as the correct answer‌‌!

 

Thanks & Regards,

Sanjay Kumar

View solution in original post

1 REPLY 1

Community Alums
Not applicable

Hi @SebF ,

 

Your requirement for switching between a free-form text field and a choice list dynamically based on another field's value on a ServiceNow form is complex requirement, especially when considering that ServiceNow does not natively support toggling a field between a choice list and a text input directly.

You should consider the below ways as per best practice-

1. Either make u_keyword field read only unless u_check field is checked this way you can restrict users to only update when u_check is enabled. Not sure why text input is needed if its going to be a choice field later.

 

2. Or another effective way to handle this scenario is by using two separate fields: one as a free-form text field and the other as a choice field. You can then show or hide the appropriate field based on the value of the u_check field.

Steps-

Create a new choice field, e.g., u_keyword_choice

Set up the Choice List for u_keyword_choice as you require

 

Create a onChange Client Script to Toggle the fields-

function onChange(control, oldValue, newValue, isLoading) {
    toggleKeywordField();
}

function onLoad() {
    toggleKeywordField();
}

function toggleKeywordField() {
    var checkField = g_form.getValue('u_check');
    if (checkField == 'true') {
        // Show choice field and hide text field
        g_form.setDisplay('u_keyword_choice', true);
        g_form.setDisplay('u_keyword', false);
    } else {
        // Show text field and hide choice field
        g_form.setDisplay('u_keyword_choice', false);
        g_form.setDisplay('u_keyword', true);
    }
}

Note- Above are efficient ways to achieve your requirement, as there is no solution to change the field type from freeform to choice as its at dictionary level.

 

If my response has resolved your query, please consider giving it a thumbs up ‌‌ and marking it as the correct answer‌‌!

 

Thanks & Regards,

Sanjay Kumar