Tip: getReference()

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-10-2024 06:01 AM
getReference() - The ServiceNow method all developers might be overlapping.
I diving deep into ServiceNow optimization lately, and I had to share this gem, If you are tried of writing extra queries for reference records, getReference() will become our new best fried.
What is special in getReference()?
- It returns a GlideRecord object instantly.
- Eliminates the need for additional database queries.
- Makes your code significantly cleaner when handling reference fields.
Instead of writing multiple lines to get reference fields values, you can access them directly, It's changed how I approach reference field handling.
Thank you,
- 408 Views
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-11-2024 01:41 AM - edited 12-11-2024 01:44 AM
getReference() is not the best method for performance due to the fact the entire record is returned from the server side affecting client side performance.
- If you're using getReference you will get ALL data for each record you "get" whether you need it or not.
- If you use GlideAjax (recommended) then you only get what you need which could be a single value, because you will explicitly return the exact information you need and nothing else minimising client performance impact.
There is a good article here covering the pitfalls of getReference() here: Avoid using g_form.getReference()
If you are absolutely must to use client-side GlideRecord through getReference(), do so with a callback so at least it's non-blocking.
For that reason, client side GlideAjax is almost always the way to go.
Regards
Paul