Dependent choices not display

JCanlas
Tera Contributor

Hi Team! Has anyone experienced this as well. Upon creation of the record. The choices we created doesn't display on the field which is dependent on the location. Regardless if the Location field is pre-populated. 

 

I have tried utilizing the Parent field on the cmn_location field and having the dependent value as sys_id, Parent's name, and Parent's sys_id. Set the dependent field dot-walks into Location>Parent as well and it still doesn't work. The only solution in mind is having all choices exist into each location (4 choices as 4 location). Let me know if you have any best solution in mind. Thanks!

JCanlas_2-1764577711269.png

 

3 REPLIES 3

adityahubli
Tera Contributor

Use a client script to dynamically control the choices

Since ServiceNow does not support making a choice field dependent on a reference field (like Location) using the built-in dependent choice feature, the reliable approach is to use an onChange client script. In this method, whenever the user selects or changes the Location, the script clears the existing options in the dependent field and then adds back only the choices that are valid for that specific location. This allows you to fully control which choices appear without needing to duplicate them for every location. It also works even when the Location is pre-populated, because the script runs and rebuilds the choice list based on the selected value.

function onChange(control, oldValue, newValue) {
g_form.clearOptions('dependent_field');

if (newValue == 'locationA_sysid') {
g_form.addOption('dependent_field', 'choice1', 'Choice 1');
g_form.addOption('dependent_field', 'choice2', 'Choice 2');
}

if (newValue == 'locationB_sysid') {
g_form.addOption('dependent_field', 'choice3', 'Choice 3');
g_form.addOption('dependent_field', 'choice4', 'Choice 4');
}
}


Alternative— Convert the dependent field into a reference field

Instead of using a choice list, you can convert the dependent field into a reference field and store its valid values in a table. Once it becomes a reference field, ServiceNow allows you to filter its available values using a reference qualifier. This reference qualifier can look at the selected Location and only return the records related to that location. This method avoids hardcoding and makes the relationship scalable, since new locations or options only need entries in the table rather than editing choice lists or scripts.

 

 

if this helps you then mark it as helpful and accept as solution

Regards,

Aditya

Ankur Bawiskar
Tera Patron
Tera Patron

@JCanlas 

share how the dependency is handled.

is it via script or some other logic?

I think it's coming from some client script and possibly onChange

If yes then ensure that onChange also handles the dependency logic for new record by allowing it to run when form loads for new record

💡 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

Hi Ankur, thank you for your reply! It seems that I just need to use the sys_id's of the dependent choices and add it to each 'dependent value' on the choices I created.