Find your people. Pick a challenge. Ship something real. The CreatorCon Hackathon is coming to the Community Pavilion for one epic night. Every skill level, every role welcome. Join us on May 5th and learn more here.

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!

6 REPLIES 6

Ankur Bawiskar
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  ||  10x ServiceNow MVP  ||  ServiceNow Community Leader

@vinod6 

Hope you are doing good.

Did my reply answer your question?

💡 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  ||  10x ServiceNow MVP  ||  ServiceNow Community Leader