The CreatorCon Call for Content is officially open! Get started here.

Issue adding options via client script to Reference type choice list when field on change

jimin han
Kilo Contributor

Hi, I want to change software model choice list of reference type depending on other choice list filed on change

but it didn't work 

How can I resolve this problem?

 

find_real_file.pngfind_real_file.png

1 ACCEPTED SOLUTION

To filter the results of a reference type field based on another field, choose 'Advanced' in the Use reference qualifier field, then one of the following examples for the Reference qual

javascript:'manager=' + current.u_manager

This will return records where the (manager) field on the reference table (sys_user in my example) matches the value selected in the u_manager field.  The list of records will refresh when u_manager changes.

javascript: new userUtils().getUser(current.u_manager);

This example calls a Script Include named userUtils and a function within it named getUser, passing in the value selected in the u_manager field to use in the script.  This will allow you to do other/ more advanced queries.  The key to this working is that the return of the Script Include is similar to this:

return 'sys_idIN' + usrArr.join(',');

where usrArr is an array I was using in the GlideRecord query to push sys_ids - so it's just a comma-separated list of sys_ids that can be found on the reference table of the field this is used on.

View solution in original post

3 REPLIES 3

Brad Bowman
Kilo Patron
Kilo Patron

You shouldn't have GlideRecord in a Client Script, so get rid of that whole block as it's not being executed anyway, and is likely preventing the rest of the script from running.  If the u_software_model field is of the Type 'Choice' (not 'Reference') clearOptions and addOption will work.  If you need to look up the choice values and labels to add from the sys_choice table or wherever depending on the newly selected other field value, then use a GlideAjax to make the server call

https://docs.servicenow.com/bundle/rome-application-development/page/script/client-scripts/concept/c...

Your answer is really helpful for me. Thank you. And I have one more question, How can I set choice List of reference type? If I use GlideAjax, what return type of it? Could you give me those of examples?

To filter the results of a reference type field based on another field, choose 'Advanced' in the Use reference qualifier field, then one of the following examples for the Reference qual

javascript:'manager=' + current.u_manager

This will return records where the (manager) field on the reference table (sys_user in my example) matches the value selected in the u_manager field.  The list of records will refresh when u_manager changes.

javascript: new userUtils().getUser(current.u_manager);

This example calls a Script Include named userUtils and a function within it named getUser, passing in the value selected in the u_manager field to use in the script.  This will allow you to do other/ more advanced queries.  The key to this working is that the return of the Script Include is similar to this:

return 'sys_idIN' + usrArr.join(',');

where usrArr is an array I was using in the GlideRecord query to push sys_ids - so it's just a comma-separated list of sys_ids that can be found on the reference table of the field this is used on.