User selects a display name, populates another field with user ID

triborro
Tera Contributor

I have tried many times to get this to work. Full disclosure, I know only enough JS to be dangerous. 

 

I am trying to create a catalog item where a user is selected from a lookup select box. The resulting UI policy script should fill in another field with the user ID (user_name). Try as I might, I cannot get this to work the way I expect it to. The field remains blank. 

 

My catalog UI policy script is as follows:

function onCondition() {
u_termed_user.value = u_name.displayValue.user_name;
}

 

As seen in the pic, despite selecting a user, the user ID field remains blank. What am I doing wrong? Appreciate any help.

1 ACCEPTED SOLUTION

Brad Bowman
Kilo Patron
Kilo Patron

You can do this with a scripted Catalog UI Policy, but best practice is to use UI Policies to set a field/variable as visible, mandatory, and/or read only, or not, and everything else should be done in a Catalog Client Script.  Either need to use the same syntax, which at the client level means no dot-walking to get a value from a table based on the value of the field/variable.

 

If you can change your variable to a reference type, which is more typical when selecting a user, you can create an onChange Catalog Client script that uses getReference to retrieve the user_name field.

https://docs.servicenow.com/bundle/rome-application-development/page/app-store/dev_portal/API_refere... 

 

If you're going to stick with the lookup select box, then you need to use a GlideAjax call in your onChange Catalog Client Script to a Script Include.  This is the best practice anyway, and a good tool to have in your belt.  Here is an excellent primer on how to do this:

https://www.servicenow.com/community/developer-articles/glideajax-example-cheat-sheet-updated/ta-p/2... 

View solution in original post

2 REPLIES 2

Brad Bowman
Kilo Patron
Kilo Patron

You can do this with a scripted Catalog UI Policy, but best practice is to use UI Policies to set a field/variable as visible, mandatory, and/or read only, or not, and everything else should be done in a Catalog Client Script.  Either need to use the same syntax, which at the client level means no dot-walking to get a value from a table based on the value of the field/variable.

 

If you can change your variable to a reference type, which is more typical when selecting a user, you can create an onChange Catalog Client script that uses getReference to retrieve the user_name field.

https://docs.servicenow.com/bundle/rome-application-development/page/app-store/dev_portal/API_refere... 

 

If you're going to stick with the lookup select box, then you need to use a GlideAjax call in your onChange Catalog Client Script to a Script Include.  This is the best practice anyway, and a good tool to have in your belt.  Here is an excellent primer on how to do this:

https://www.servicenow.com/community/developer-articles/glideajax-example-cheat-sheet-updated/ta-p/2... 

Thanks Brad. Being perfectly honest, I didn't pay attention to things like scoping and client/server side references. That makes total sense. TBH I am a novice at scripting and immediately blamed myself for bad code rather than an imperfect understanding of the environment I was working in. Given your references, and the state at which I am at on this, I am going to start over from scratch with your advice in mind. Thanks again!