getReference() - best practice or not?

patricklatella
Mega Sage

I'm seeing in the Wiki, "Other methods, GlideRecord and g_form.getReference() are also available for retrieving server information. However, these methods are no longer recommended due to their performance impact.".   However in my Implementation Bootcamp book it has "getReference()" listed as a best practice when used with a callback function.   I'm looking for clarification on this is specific reference to the Implementation Specialist exam, and whether the exam would follow the book or the Wiki.   Or is it possible I'm not seeing this correctly.   Thanks!

1 ACCEPTED SOLUTION

coryseering
ServiceNow Employee
ServiceNow Employee

Hi Patrick,



g_form.getReference just ends up calling the client-side GlideRecord API. In my opinion, client-side GlideRecord should be avoided as much as possible, since it usually results in a lot of data being transferred needlessly. If you are going to use it, use a callback (g_form.getReference accepts a callback as the second parameter). Client-side GlideRecord can also take a callback (passed in to the query method).



If you are going to use client-side GlideRecord (either through getReference, or directly), do so with a callback so it's non-blocking. However, you rarely need to get the entire record back when doing lookups, so a GlideAjax script which can return only the necessary information is typically a better choice.



Thanks


Cory


View solution in original post

14 REPLIES 14

coryseering
ServiceNow Employee
ServiceNow Employee

Hi Patrick,



g_form.getReference just ends up calling the client-side GlideRecord API. In my opinion, client-side GlideRecord should be avoided as much as possible, since it usually results in a lot of data being transferred needlessly. If you are going to use it, use a callback (g_form.getReference accepts a callback as the second parameter). Client-side GlideRecord can also take a callback (passed in to the query method).



If you are going to use client-side GlideRecord (either through getReference, or directly), do so with a callback so it's non-blocking. However, you rarely need to get the entire record back when doing lookups, so a GlideAjax script which can return only the necessary information is typically a better choice.



Thanks


Cory


My thoughts...GlideAjax is the better choice for this..however it is recognised to do an callback function within a CS and this is in their training material so I guess cant be a bad thing..however....doing the callback will flag up in ACE reports


Brad Tilton
ServiceNow Employee
ServiceNow Employee

I think as long as you use a callback function and aren't doing multiple getReference() calls you're ok. I think if you have a requirement that's going to cause you to use multiple getReference() calls at once then you'll probably want to use GlideAjax.


The SN Nerd
Giga Sage
Giga Sage

At the end of the day, you've got the trade off of simplicity vs efficiency.


As you can see, getReference w/ callback give you the best balance of simplicity   vs efficiency.



Client Script - Simplicity vs Efficiency



APISimplicityEfficiencyDescription
GlideRecord getReference1st5th
  • 1 L.O.C.
  • Poor UX (Blocks Browser)
  • Returns entire record
GlideRecord2nd4th
  • ~5 L.O.C.
  • Poor UX (Blocks Browser)
  • Returns entire record
GlideRecord getReference w/ callback3rd3rd
  • 5-10 L.O.C.
  • Best UX (Does not block browser)
  • Returns entire record
GlideRecord w/ callback4th2nd
  • ~10 L.O.C.
  • Best UX (Does not block browser)
  • Returns entire record
GlideAjax5th1st
  • ~20+ L.O.C. (Client Script + S.I.)
  • Best UX (Does not block browser)
  • Returns only the data you need


Terms


L.O.C. - Lines of Code (Just estimates on the top of my head)


UX - User Experience


S.I. - Script Include



ServiceNow Nerd
ServiceNow Developer MVP 2020-2022
ServiceNow Community MVP 2019-2022