What is the use of getReference()?

ishankaggarwal
Tera Contributor

What is the use of getReference()?

16 REPLIES 16

M Naveed
Kilo Contributor

function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
var abc = g_form.getReference('request_for', currentUser1);
return;
}

function currentUser1(abc) {

g_form.setValue('mobile_phone', abc.mobile_phone);
g_form.setValue('manager_approval', abc.manager);
}

//Type appropriate comment here, and begin script below

}

Swarup Patra
Kilo Guru

The getReference() method in ServiceNow is used to retrieve a single GlideRecord referenced in a field. It is a powerful method that allows you to access data from related records without having to do a separate GlideRecord query. Here are some key points about getReference(): - It is used to retrieve the data from a referenced field in a record. - It allows you to access fields from a referenced table. - It is a method of the GlideRecord class. - It can be used in server-side scripting. - It is useful when you need to access data from a related record without doing a separate GlideRecord query. Here is a sample code snippet using getReference(): javascript var incidentGR = new GlideRecord('incident'); incidentGR.get('number', 'INC0010001'); var caller = incidentGR.caller_id.getDisplayValue(); gs.info(caller); In this example, 'caller_id' is a reference field on the 'incident' table. The getReference() method is used to get the display value of the caller_id field. nowKB.com

Rajdeep Ganguly
Mega Guru

The getReference() method in ServiceNow is used to retrieve a single GlideRecord referenced in a field. It is a powerful method that allows you to access data from related records without having to write a separate GlideRecord query. Here are some key points about getReference(): - It is used to retrieve a GlideRecord for a reference field. - It allows you to access fields from a referenced record in a single line of code. - It is often used in business rules, script includes, and other server-side scripting. - It can be used to perform operations on the referenced record, such as updating or deleting it. - It is a synchronous operation, meaning it will block other operations until it completes. Here is a sample code snippet using getReference(): javascript var incidentGR = new GlideRecord('incident'); if (incidentGR.get('number', 'INC0010023')) { var callerGR = incidentGR.getReference('caller_id'); gs.info('Caller Name: ' + callerGR.name); } In this example, getReference('caller_id') retrieves the GlideRecord for the user record referenced in the 'caller_id' field of the incident. This allows you to access the 'name' field of the caller directly.

Rajdeep Ganguly
Mega Guru

The getReference() method in ServiceNow is used to retrieve a single GlideRecord referenced in a field. It is a powerful method that allows you to access data from related records without having to do a separate GlideRecord query. Here are some key points about getReference(): - It is used to retrieve a GlideRecord for a record that is referenced in a field on another record. - It is useful when you need to access fields from a referenced record, without having to do a separate GlideRecord query. - It can be used in both client-side and server-side scripting. - It is a synchronous call, which means it will wait for the database operation to complete before continuing with the script execution. Here is a sample code snippet using getReference(): javascript var incidentGR = new GlideRecord('incident'); incidentGR.get('number', 'INC0010001'); var caller = incidentGR.caller_id.getDisplayValue(); gs.info(caller); In this example, the getReference() method is used to retrieve the display value of the caller_id field from the incident record with number 'INC0010001'. The caller's name is then logged to the system log.

Afrith Shariff
Tera Guru

Hi @ishankaggarwal ,


The getReference() method is used in GlideRecord and GlideForm (client scripts) to retrieve the full record of a reference field.

Key Points

  • A reference field only stores the sys_id of the related record.
  • If you want details (like the user’s name, email, or department from a caller_id field), you use getReference() to pull the entire referenced record.
  • On the client side, it works asynchronously — you provide a callback function to handle the returned record.

 

 

Example – Client Script
// On a form with a reference field 'caller_id'
g_form.getReference('caller_id', function(userRecord) {
var phone = userRecord.phone;
var email = userRecord.email;
alert("Caller Phone: " + phone + " | Email: " + email);
});

 

 

Thanks,

Afrith