How to auto populate reference fields with client scripts
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-03-2021 10:50 AM
How do I write a client script to auto populate a reference field when "requested for/name" field is populated?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-03-2021 11:20 AM
A little more detail would be helpful here.
What is the reference field you are wanting to populate?
What type of field is 'requested for/name'?
Are you wanting this to run onLoad, onChange or onSubmit?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-03-2021 11:24 AM
Hi Lanre,
The complexity of this script depends on what you want to populate in the reference field - what table is it referencing, and how does it relate to the sys_user table that is triggering it? Assuming Requested for is a reference field to sys_user, the value stored in it will be the sys_id of the selected record on that table - even though the name is displayed. Likewise, to populate a different reference field, you need to supply the sys_id of the record on the table that this second field is referencing. If it's a simple lookup, for example the second field is 'Manager' and you want to populate it with the Manager of the Requested for, then you can do this with a getReference as shown below. If there's more querying that needs to be done to find the record on the second field, then you'll need a GlideAjax call to a script include. I can provide an example of that if you need it. For getReference using my example, your onChange Client Script would look something like this
function onChange(control, oldValue, newValue, isLoading) {
if (newValue == '') {
return;
}
var user = g_form.getReference("requested_for", populateManager);//confirm your field name
}
function populateManager(user) {
g_form.setValue("manager", user.manager);//confirm your field name and referenced table field name
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-06-2021 05:17 AM
Thanks the response is very helpful.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-15-2022 11:06 AM
In Catalog Client Scripts click New >select your name >select your catalog item and Type on load then in Script
function onLoad()
{
var user = g_form.getReference('requested_for', populateInameinfo);
function populateInameinfo(user) {
g_form.setValue('requested_for_first_name', user.first_name);
g_form.setValue('requested_for_last_name', user.last_name);
}
}
note: requested_for_first_name = Cataloge item variable filed First name backend value
requested_for_last_name = Cataloge item variable filed Last name backend value
sys_user table first name and last name backend value
first_name
first_name