Notifications

195 views

Description

getRefRecord() returns a GlideRecord object for a given reference element. This is used widely in business rule scripts but incorrect usage of this can cause some major issues. This article describes some issues seen and the best practice to follow to avoid these. 

Issues

  1. Printing values from the record obtained using getRefRecord() might not print anything. 
  2. Orphan records get inserted when updating the record obtained using getRefRecord(). 
  3. Using values from record obtained using getRefRecord() in other script results in "undefined" errors.


Resolution

If the reference field has an empty value, getRefRecord() doesn't throw any error but it does return a GlideRecord object. That's why it is important to check if the returned object is a valid GlideRecord object or not. 

1
2
3
4
5
6
7
var grUser = current.user.getRefRecord();
if(grUser.isValidRecord()) { // << only perform operations on it if it's a valid record
    if(grUser.email != current.email_address){
        grUser.email = current.email_address;
        grUser.update();
    }
}

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

If expected field is on a child table, make sure the reference field points to the child table and not the parent table.  Otherwise the getRefRecord() returns the GlideRecord object from the parent table which will not have the field that exists on the child table resulting in "undefined" errors.

Article Information

Last Updated:2020-10-07 11:55:13
Published:2020-10-07