Auto-populate a field based based on another field
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
2 hours ago
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!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
2 hours ago
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
41m ago
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:
There is also a ServiceNow Community article showing the exact User -> Manager auto-populate pattern:
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