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.
- Printing values from the record obtained using getRefRecord() might not print anything.
- Orphan records get inserted when updating the record obtained using getRefRecord().
- Using values from record obtained using getRefRecord() in other script results in "undefined" errors.
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.
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.