client scripts

mahesh009
Tera Contributor

How can I use the g_form.getReference() method to populate a reference field based on the value in another field?

1 ACCEPTED SOLUTION

Ramesh_143
Giga Guru

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

View solution in original post

7 REPLIES 7

Moin Kazi
Kilo Sage
Kilo Sage

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.

 

Important: It is strongly recommended that a callback function be used.

Callback function support for ServiceCatalogForm.getReference is available.



Note: This requires a call to the server so using this function will require additional time and may introduce latency to your page. Use with caution. See Avoid Server Lookups.
Parameters:
Name Type Description
fieldNameStringName of the field.
callBackFunctionName of the call back function.

Returns:


Type Description
GlideRecordThe 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);
}

MarkMoinKazi_0-1728539070477.pngCorrect if this solves your issue and also markMoinKazi_1-1728539070483.pngHelpful if you find my response worthy based on the impact.

 

Regards

Moin

 

Amit Verma
Kilo Patron
Kilo Patron

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.

 

https://www.servicenow.com/community/developer-forum/what-is-the-use-of-getreference/m-p/1433807/pag...

https://www.servicenow.com/community/developer-forum/g-form-getreference-variable-callback-function-...

 

Thanks and Regards

Amit Verma


Please mark this response as correct and helpful if it assisted you with your question.

Ravi Gaurav
Giga Sage
Giga Sage

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/

Ramesh_143
Giga Guru

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