- 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-09-2024 10:48 PM
Hi @mahesh009 ,
GlideForm - getReference(String fieldName, Function callBack)
Returns the GlideRecord for a specified field.
If a callback function is present, this routine runs asynchronously, and browser (and script) processing will continue normally until the server returns the reference value, at which time the callback function will be invoked. If a callback function is not present, this routine runs synchronously and processing will halt (causing the browser to appear to hang) while waiting on a server response.
Callback function support for ServiceCatalogForm.getReference is available.
fieldName | String | Name of the field. |
callBack | Function | Name of the call back function. |
Returns:
Type Description
GlideRecord | The GlideRecord object for the specified field. If the specified reference cannot be found, then it returns an initialized GlideRecord object where currentRow = -1 and rows.length = 0. |
function onChange(control, oldValue, newValue, isLoading)
{
var caller = g_form.getReference('caller_id', doAlert); // doAlert is our callback function
function doAlert(caller) { //reference is passed into callback as first arguments
if (caller.vip == 'true')
alert('Caller is a VIP! and Caller email is: '+ caller.email);
}
MarkCorrect if this solves your issue and also mark
Helpful if you find my response worthy based on the impact.
Regards
Moin
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-09-2024 11:29 PM
Hi @mahesh009
getReference() gets the record used in another reference field. You can refer below posts to understand it but I will suggest you to use GlideAjax instead of getReference() since getReference() is used client side to make a call to the server which may impact performance if used too heavily.
Thanks and Regards
Amit Verma
Please mark this response as correct and helpful if it assisted you with your question.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-09-2024 11:31 PM - edited ‎10-09-2024 11:33 PM
Hi @mahesh009
You can create the below script :-
var caller = g_form.getReference('caller_id', doAlert); //doAlert is a call back function
function doAlert(caller) { //reference is passed into callback as first arguments
alert('Caller is a VIP! and Caller email is: '+ caller.email);
More Info :- https://servicenowguru.com/client-scripts-scripting/gform-getreference-callback/
If you found my response helpful, I would greatly appreciate it if you could mark it as "Accepted Solution" and "Helpful."
Your support not only benefits the community but also encourages me to continue assisting. Thank you so much!
Thanks and Regards
Ravi Gaurav | ServiceNow MVP 2025,2024 | ServiceNow Practice Lead | Solution Architect
CGI
M.Tech in Data Science & AI
ï”— YouTube: https://www.youtube.com/@learnservicenowwithravi
ï”— LinkedIn: https://www.linkedin.com/in/ravi-gaurav-a67542aa/
- 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