ServiceNow Learning 86: Best practice when using getRefRecord().
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-29-2023 08:13 AM
Hi All,
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 will explain you some issues seen and the best practice to follow to avoid these.
What could be possible issues?
- 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.
What to do to avoid the 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.
Script to use:
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(); } }
Hope it helps.
I hope this article helpful. Please mark it as helpful and bookmark if you like it.
Regards,
Shamma
- 7,008 Views
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-29-2023 08:21 AM
I would suggest to post these as articles instead of questions as that would be more appropriate.
Do mark this response as HELPFUL if it helps.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-29-2023 09:13 AM
Thanks for commenting.
I will shift all my articles once I get the article access. Unortunately, I dont have the article access on community yet. Hence sharing my knowledge here.
Request you to please ignore those which starts from ServiceNow Learnings.
Thanks,
Shamma
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-30-2024 09:08 AM
Why are you copying content from a ServiceNow Knowledge Base article and basically claiming it as yours?
https://support.servicenow.com/kb?id=kb_article_view&sysparm_article=KB0745222
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-11-2024 03:17 AM
Agreed. Unless they actually wrote the article, but then again, they wouldn't post it like this...
I see very often this practice, I can imagine that the perceived importance and relevance of the ranking in the Community drives this; it might even count as a sort of KPI milestone in some companies 🙂