Tip: getReference()

Community Alums
Not applicable

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,

1 REPLY 1

Paul Curwen
Giga Sage

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.  

 

 

***If Correct/Helpful please take time mark as Correct/Helpful. It is much appreciated.***

Regards

Paul