- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-09-2024 10:32 PM
How can I use the g_form.getReference() method to populate a reference field based on the value in another field?
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-10-2024 01:59 AM
Hi @mahesh009 ,
To use the "g_form.getRefernce()" method in ServiceNow for populating a reference field based on another field's value, you need to create a Client Script that triggers on the "onChange" event of the relevant field. Within this script, call "g_form.getRefernce()" to retrieve the record associated with the first field, using a callback function to handle the response asynchronously. This allows you to access specific fields from the retrieved record and set them in another reference field without blocking the user interface.
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue === '') {
return;
}
g_form.getReference('assigned_to', function(assignee) {
if (assignee) {
g_form.setValue('manager', assignee.manager.sys_id);
}
});
}
Thanks & regards,
Ramesh

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-10-2024 02:11 AM
@mahesh009 Here is an example.
function onChange(control, oldValue, newValue, isLoading) {
// Make sure the form is not loading before running the script
if (isLoading || newValue == '') {
return;
}
// Get the reference field value (assuming 'caller_id' is the reference field)
g_form.getReference('caller_id', function(reference) {
// reference is the object containing fields from the referenced record
var callerName = reference.name;
var callerEmail = reference.email;
// Log the caller's name and email to the console
console.log('Caller Name: ' + callerName);
console.log('Caller Email: ' + callerEmail);
// Optionally, set values to other fields based on the reference record
g_form.setValue('caller_name', callerName);
g_form.setValue('caller_email', callerEmail);
});
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-11-2024 06:01 AM
Hi @mahesh009 ,
This can help you to find the better solution for your question. Please refer this once:
Solved: Auto populate Reference Field based on other Selec... - ServiceNow Community
While g_form.getReference() is immensely useful, it’s important to use it judiciously. Remember, every use of this method involves a server call, which can introduce latency and affect page performance. Avoid overusing it in scripts where possible, and always prefer callback functions to prevent browser lock-ups.
===================================***************=========================================
"If you found my answer helpful, please give it a like and mark it as the accepted solution. It helps others find the solution more easily and supports the community!"
Thanks & Regards,
Aditya
=====================================***********==========================================
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-14-2024 03:02 AM
Hi @mahesh009
The g_form.getReference() method retrieves the entire record of a specified reference field asynchronously, allowing you to access various fields from that record. This is useful for populating another field based on the data retrieved.
Implementation Steps:
- Identify the Fields
- Create an onChange Client Script
- Use g_form.getReference():
Here’s an example of how to implement this:
function onChange(control, oldValue, newValue, isLoading) {
// Check if the form is loading or if there's no new value
if (isLoading || newValue === '') {
return;
}
// Use getReference to retrieve the user record
g_form.getReference('requested_for', function(userRef) {
// Check if userRef is valid
if (userRef) {
// Set the location field based on the user's location
g_form.setValue('location', userRef.location.toString());
}
});
}
Please go through the below link for further details:
Solved: What is use of getReference(). - ServiceNow Community
Thanks, and Regards
Vishaal
Please mark this response as correct or helpful if it assisted you with your question.