What is the use of getReference()?

ishankaggarwal
Tera Contributor

What is the use of getReference()?

15 REPLIES 15

Performance impact is the reason. This article covers it well: 

 

https://community.servicenow.com/community?id=community_question&sys_id=2d08436ddb1cdbc01dcaf3231f96...

***If Correct/Helpful please take time mark as Correct/Helpful. It is much appreciated.***

Regards

Paul

Hello @Göran WitchDoctor Lundqvist ,

Can you please show where ServiceNow is saying that getReference() should no longer be used?  I'm thinking based on this API function that it's FINE TO USE, as long as you specify a callback function with it:

https://developer.servicenow.com/dev.do#!/reference/api/rome/client/c_GlideFormAPI#r_GlideForm-GetRe...

Thoughts?

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.