getRefRecord() function in ServiceNow.

#ServiceNow.

Many a times we need to fetch data through a particular Reference field on a table, so we either go by dot-walking approach or we do multiple GlideRecords to fetch it. Below is a much more efficient way to do it using “getRefRecord()” method which returns us a GlideRecord object for that particular record referenced in our Reference field.

getRefRecord() should always be followed by isValidRecord() check. 

var gr = new GlideRecord(“incident”);
gr.get(“bbfff36b2f232010268ce33df699bGhjk”); //sys_id of the incident record 
var caller_object = gr.caller_id.getRefRecord(); // Get a GlideRecord object for the referenced sys_user record
gs.info(caller_object.email); //you can use object to access various fields
gs.info(caller_object.mobile_phone);

It’s useful to fetch the data as well as more useful to update data as that cannot be achieved via dot walk. Below is the example:

Update data of a reference field table without GlideRecord.
var caller = current.caller_id.getRefRecord();
caller.email = ‘[email protected]’;
caller.update();

You can pass this object as a parameter to your other functions as well.
P.S. : If the reference element does not contain a value, it returns an empty GlideRecord object, not a NULL object.

Please feel free to post your thoughts in the comment sections. 😊

servicenow #servicenowdeveloper #learngrowadvance

Join the Conversation

8 Comments

  1. This approach is helpful.
    Get the reference object and fetch the data.
    Keep it up Maroof.

  2. It’s useful to fetch the data as well as more useful to update data as that cannot be achieved via dot walk. Below is the example:

    Update data of a reference field table without GlideRecord.
    var caller = current.caller_id.getRefRecord();
    caller.email = ‘[email protected]’;
    caller.update();

Leave a comment

Your email address will not be published. Required fields are marked *