Auto-populate a field based based on another field

DeIvory Gordon
Tera Guru

Hello,

 

I am creating a catalog item and I added a variable/field named "Name of user receiving phone which references the sys_user table.   I created a second variable/field named "New user’s manager" which should auto-populate based on ‘Name of user receiving phone’ field.  I am unsure how to do this.  It may be a UI Policy, but I need a little help setting it up.  Thanks!                 

DeIvoryGordon_0-1786475389151.png

 

2 REPLIES 2

DrewW
Mega Sage

You are going to have to use an onChange client script attached to the "Name of user receiving phone" to do this.  In that script you will have to query the server for the user record and have it return the manager and set the other field to that.  Then just make the other question read only so the user cannot change it.

 

Abhishek Pal
Giga Guru

Hi @DeIvory Gordon ,

You do not need a UI Policy or Client Script for this requirement if you are on Utah or later.

ServiceNow provides an OOB Auto-populate feature specifically for this scenario.

Your requirement is:

Name of user receiving phone
-> Reference to User [sys_user]

New user's manager
-> Automatically populate the Manager of the selected user

Recommended configuration:

1. Open the "Name of user receiving phone" variable.

Configure:

Type:
Reference

Reference:
User [sys_user]

2. Open the "New user's manager" variable.

Configure:

Type:
Reference

Reference:
User [sys_user]

You can also make this variable Read-only if users should not change the manager manually.

3. On the "New user's manager" variable, open the:

Auto-populate

section/tab.

Configure:

Dependent question:
Name of user receiving phone

Lookup field / Dot-walk path:
Manager

Conceptually:

Name of user receiving phone
-> sys_user.manager
-> New user's manager

That's all that is required.

Expected behavior:

User selects John Smith

John Smith.manager = Jane Doe

Then:

New user's manager = Jane Doe

If the user changes "Name of user receiving phone", the Manager variable is automatically recalculated.

If the selected user is cleared, the auto-populated Manager value is also cleared.

This is preferable to:

- Catalog Client Script
- g_form.getReference()
- GlideAjax
- UI Policy scripting

because this is now supported directly by the Service Catalog variable Auto-populate functionality.

If you do not see the Auto-populate section on the variable form, check whether your Variable form has been customized. You can also configure it through Catalog Builder.

For releases before Utah, the fallback would be an onChange Catalog Client Script with GlideAjax/getReference, but for Utah and later I would use Auto-populate.

Official ServiceNow documentation:

https://www.servicenow.com/docs/r/servicenow-platform/service-catalog/config-auto-populate-value-for...

There is also a ServiceNow Community article showing the exact User -> Manager auto-populate pattern:

https://www.servicenow.com/community/developer-articles/auto-populate-a-variable-based-on-a-referenc...

Hope this helps!

If this response helped, please mark it as Helpful.
If it resolves your issue, please Accept it as Solution.

Kind Regards,
Abhishek Pal