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

ismailsn
ServiceNow Employee
ServiceNow Employee

I would use GlideAjax. Even though getReference might work for you in the beginning, and very well could be quick- the issue begins when bad practice overtime builds up.



Regards,


Spot on.



When your instance is 2+ years old, you'll find performance degrades substantially.


Seen so many customers with years of bad practice resulting in almost unusable systems.



Food for thought:


For the use case of 'Get data from reference field and put in another field' I wrote a re-usable UI Script, putting a GlideAjax call down to 1 LOC.



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

Sorry for necroing a 2 year old thread, but I'm interested in the reusable script that you developed.   Does it take into account any of the different data types available?



Thanks


It doesn't do any data type validation, you just input the fields you want to map based on a reference field and it will go and retrieve them.



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

patricklatella
Mega Sage

hey thanks to everyone for chiming in on this thread.   Seems like there is a pretty consistent opinion that GlideAjax is much preferred to getReference for many reasons.



great info!