Contents
There are many different ways to retrieve data from a web browser (client-side) from the server in ServiceNow. Just when you think you know all the ways to do it, ServiceNow will have just released a new no-code/low-code way to do it. Alas, what is best practice in one release is not best practice in the next!
When looking for the best practice ways to do things on the ServiceNow Community, there are a few things you should check:
- How old is the post
- Has there recently been a new release that may offer new functionality
- Is the post endorsed by MVP’s or users of a high community standing
- Is it boasting “Best Practice” understand if it is applicable to your situation
- There is an exception to every rule – Best Practice applied incorrectly can become an anti-pattern
- Cross-reference with ServiceNow’s Technical Best Practices
There are a few things there to check! Rather than looking for a “Best Practice”, I think it is more useful to look for design patterns and understand which patterns are suitable for particular requirements. This is why I have started creating “Ultimate Guides” to summarise the best design patterns to approach each situation in each version.
As you should with an article on the Community, I suggest following the checklist and note that this is just my opinion, which may be contested by others!
Client-Side Data Retrieval Design Pattern Matrix
Version / Requirement |
Catalog Item – Data from Reference fields | Form – On load | Form – On demand | Form – on submit (scoped) | Form (scoped) / Service Portal – on submit |
Utah | Auto-populate Variable | Display Business Rule and Scratchpad | GlideAjax /w Client Callable Script Include | GlideAjax & getXMLWait() | getXMLwait alternative |
Quebec / Rome / San Diego / Tokyo | Catalog Lookup Definition | Display Business Rule and Scratchpad | GlideAjax /w Client Callable Script Include | GlideAjax & getXMLWait() | getXMLwait alternative |
Paris | GlideAjax /w Client Callable Script Include | Display Business Rule and Scratchpad | GlideAjax /w Client Callable Script Include | GlideAjax & getXMLWait() | getXMLwait alternative |
Orlando | GlideAjax /w Client Callable Script Include | Display Business Rule and Scratchpad | GlideAjax /w Client Callable Script Include | GlideAjax & getXMLWait() | getXMLwait alternative |
Catalog Lookup Definition
ServiceNow has enhanced an existing feature called Catalog Lookup Definition in the Quebec release, introducing a no-code solution that may eliminate the need for complex GlideAjax lookups. Where GlideAjax with a callback would have been the Best Practice in Quebec for retrieving data based on reference fields, using a No-Code approach is the way to go. Developer MVP 2021 Mark Roethof shows how to do this in his blog article Catalog Data Lookup Definition on any table, eliminating Catalog Client Scripting.
GlideAjax /w Client Callable Script Include
It is a good idea to create your own re-usable GlideAjax API for data retrieval.
If you are feeling lazy, you can just download my Share Project SmartAjax which has simplified client-side data retrieval into 1 Line of Code, recently updated for Agent Workspace.
Display Business Rule and Scratchpad
GlideAjax is not always the answer. If data isn’t needed on change or on submit, the use of GlideAjax becomes an anti-pattern, as it adds unneeded complexity to your environment. It is much simpler to perform your server code in a display business rule and pass values through the scratchpad.
GlideAjax & getXMLWait()
When you need data on demand (such as when a field changes) you’ll need to get down and dirty with some client-side code. If you are just doing a simple data retrieval based on a reference field, I recommend creating a re-usable data retrieval script – or just see my SmartAjax Share Project.
getXMLWait() alternative
getXMLWait() is not allowed in Scoped Forms or Service Portal. This can be a real pain when you need to get some data from the server, which may prevent a form from being submitted. Thankfully, there is a design pattern using the scratchpad that can be used for both Service Portal and Scoped forms: getXMLwait alternative for Service Portal
Honorable Mentions
The following can be used for niche situations but there are generally better design patterns to be used.
getReference() with Callback
getReference() is a low-code approach to retrieving data from the server, however it does have it’s issues, as outlined in my GlideAjax blog series.
It is no longer considered Best Practice to use this, it is recommended to use GlideAjax instead.
REST
REST can be used to retrieve data using the Table API or Scripted Endpoints. If you are interested in using REST instead of GlideAjax, check out this article: getReferenceAdvanced, g_form.getReference and GlideAjax Alternatives
Dishonorable Mentions
The following methods are best placed to be used to spin up Proof of concept solutions and refactored later, never reaching your production environment, as they are quick and dirty.
GlideRecord()
Not supported in Scoped Applications, this ancient API has little use in the tool today.
getReference() without Callback
With no callback function, this blocks the browser and retrieves the entire record. You don’t want this code to find its way into production.
Further Reading
Technical Best Practices – Client Scripting