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.

Dependent choice values

vinod6
Tera Contributor

I have Field 2 dependent on Field 1.

Field 1:
A
B
C
D

Field 2:
Unknown
None
Empty
Populated

Can I have a choice on Field 2 show if any of the choices from Field 1 are selected? For example, I want Unknown to show if any of the choices are selected in Field 1. Or, I want Unknown to show if A, B or C is selected in Field 1. 
Can please provide a client script for this it is helpful. Thanks!

5 REPLIES 5

Ankur Bawiskar
Tera Patron
Tera Patron

@vinod6 

you can make Field 2 dependent on multiple values from Field 1 using client script

1 onChange on Field 1 like this

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

    // Show "Unknown" in Field 2 only if Field 1 is A, B, or C
    var showUnknown = ['A', 'B', 'C'].indexOf(newValue) !== -1;

    // Clear all choices in Field 2
    g_form.clearOptions('field_2');

    // Always add other options
    g_form.addOption('field_2', 'None', 'None');
    g_form.addOption('field_2', 'Empty', 'Empty');
    g_form.addOption('field_2', 'Populated', 'Populated');

    // Add "Unknown" only if condition met
    if (showUnknown) {
        g_form.addOption('field_2', 'Unknown', 'Unknown');
    }

    // Reset Field 2 value if current choice is not valid anymore
    var val = g_form.getValue('field_2');
    if (val === 'Unknown' && !showUnknown) {
        g_form.setValue('');
    }
}

💡 If my response helped, please mark it as correct and close the thread 🔒— this helps future readers find the solution faster! 🙏

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