What is the use of getReference()?

ishankaggarwal
Tera Contributor

What is the use of getReference()?

15 REPLIES 15

Shishir Srivast
Mega Sage

you can use the getReference() to glide that particular record from client side and access the related record of that Glide Object, you can find the more details here: GlideForm - Client


Uncle Rob
Kilo Patron

GlideForm - Client



Returns the GlideRecord for the field (must be a reference field) you specify in the parameters.


Rajesh Mushke
Mega Sage
Mega Sage

Hey Ishank,



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:
NameTypeDescription
fieldNameStringName of the field.
callBackFunctionName of the call back function.

Returns:


TypeDescription
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!');
}

Please Refer:
GlideForm - Client


https://my.metlife.com/teams/ServiceNow_AD/_layouts/15/osssearchresults.aspx?u=https%3A//my%2Emetlif...




Thanks,
Rajashekhar Mushke
Rising star : 2022 - 2024
Community Leader -2018
Connect me on LinkedIn : Rajashekhar Mushke

Rajesh Mushke
Mega Sage
Mega Sage

getReference gets the record used in another reference field. For example you have the Caller ID (reference to the sys_user table) and with getReference, you will retrieve the User record and save this as a gliderecord into a variable. The main reason to use this function is to get more information from the referenced record.



Since this is used client side, it IS a call to the server which may impact performance if used too heavily.



Example:


var gr = g_form.getReference('caller_id');


alert('Caller Email is: ' + gr.email);



Since this is a server call, as already said above, I would recommend using the callback feature (new in Aspen):


var caller = g_form.getReference('caller_id', doAlert); // doAlert is our callback function



function doAlert(caller) { //reference is passed into callback as first arguments


alert('Caller Email is: ' + caller.email);


}



Please refer:


Client Script Examples — ServiceNow Elite





Thanks,
Rajashekhar Mushke
Rising star : 2022 - 2024
Community Leader -2018
Connect me on LinkedIn : Rajashekhar Mushke