GlideRecord On The Client vs Server
If you ever see a GlideRecord query, anywhere on the client side, it’s a HUGE red flag.
It indicates that whomever implemented the solution doesn’t have any respect for performance and doesn’t know what they’re doing in ServiceNow.
If I haven’t made it apparent already, never use a GlideRecord query on the client.
Instead, learn how to create a GlideAjax script.
A GlideAjax script is a performant way to access server side data, from the client side.
Here is how you write a GlideAjax script: How To Use GlideAjax in ServiceNow
Let’s review below why using GlideAjax is a better idea than writing a client side GlideRecord query.
What Happens When You Run A GlideRecord On The Client?
Let’s review what’s really going on, behind the scenes when someone tries to execute a GlideRecord query on the client.
Think about this idea from the context of a ServiceNow environment, and from the perspective of one of your end users.
If you have an instance where you have even one GlideRecord query on the client, I’d strongly consider you take some time and implement a GlideAjax solution in place of it.
When a server-side call is made from the client-side, it involves sending a request from the client’s web browser to the server. The server then processes the request and sends a response back to the client.
Depending on the size of the table, this could hang the users session for several seconds or even minutes, if it’s a larger table.
This can add a significant amount of overhead and can have an impact on the performance of the application.
This performance hit degrades the quality and reputation of the system.
One of the main reasons that it is generally a bad idea to make server-side calls from the client-side is that it can lead to slower page load times and a less responsive user interface. This is because the client must wait for the server to process the request and send a response before it can continue executing any further code.
In addition, making server-side calls from the client-side can also increase the load on the server, as it must process multiple requests from the client in addition to handling requests from other clients. This can lead to poor performance and scalability issues.
Does this sound like something you’d like to implement in your instance?
Of course not, so stay far away.
It is generally better to minimize the number of server-side calls made from the client-side and to use client-side code as much as possible to improve performance and reduce the load on the server.
As mentioned before, if you need to access server side data on the client, use GlideAjax.
What Is A GlideRecord Query?
In ServiceNow, a GlideRecord is a class that is used to query the database and retrieve records. It is often used to perform database operations such as inserting, updating, and deleting records.
This should always be a server side operation.
To use a GlideRecord to query the database, you can specify a number of parameters to define the criteria for the query.
For example, you can specify the name of the table you want to query, the fields you want to retrieve, and any conditions that must be met for a record to be included in the results.
Once you have defined the query, you can execute it using the query()
method of the GlideRecord class. This will return a collection of records that meet the specified criteria. You can then use the next()
method to iterate through the results and process them as needed.
Here is an example of a simple GlideRecord query in ServiceNow:
var gr = new GlideRecord('incident');
gr.addQuery('active', 'true');
gr.query();
while (gr.next()) {
// process the record
}
This example creates a new GlideRecord for the incident
table and specifies that only active records should be included in the results. The query()
method is then called to execute the query, and the next()
method is used to iterate through the resulting records.
If this were to be done on the client, the users session would hang until the entire query finished, which could take many seconds to many minutes, which would mean the user would have to wait for the query to finish, before being able to use the system again.
Why Is It Bad To Run A GlideRecord Query On The Client?
The main reasons are related to performance and scalability, as we’ve discussed above.
But to close this article out, I will re-summarize below for you.
If you ever come across someone taking the short cut of using a GlideRecord on the client, be very concerned.
This is actually a phenomenal interview question.
If someone understands the content of this post they are likely a solid resource and don’t cut corners.
Using a GlideRecord query in a client script can be a bad idea because it can lead to poor performance in the user interface. When a GlideRecord query is run in a client script, it must first retrieve all of the records that meet the specified criteria and then return them to the client. This can take a significant amount of time, especially if there are a large number of records that meet the criteria.
Always consider how your solution impacts end users.
In addition, when a GlideRecord query is run in a client script, it is not possible to control the order in which the records are returned, so the user may not see the records in the order they expect.
It is always better to use server-side GlideRecord queries in ServiceNow, rather than client-side queries, to improve performance and provide more control over the data being returned. When needing to use client-side queries, again – use GlideAjax.
Some people will try to get around it and run a GlideRecord query asynchronously on the client, stating that it’s not an issue.
This is another shortcut, just use GlideAjax, which is the preferred and supported method.