Blog-Mastering GlideAjax: Making Client-Server Interaction Smooth in ServiceNow
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
4 hours ago
When you are working on ServiceNow it is really important to have communication between the client and server. This is what makes your applications work well and be easy to use. GlideAjax is a technology that helps with this. It lets the client and server talk to each other without having to reload the page. This makes things a lot faster and nicer for the user.
In this post we will take a look at GlideAjax. We will explore what it can do how to use it and some good ways to make the most of it. Whether you are new to ServiceNow or have been working with it for a while understanding GlideAjax is crucial for making your applications the best they can be.
Understanding GlideAjax
GlideAjax is a ServiceNow tool that helps the client and server talk to each other. It does this in a way that does not block the user interface so things stay smooth and fast. When the client sends a request to the server it can keep doing things while it waits for the answer. When the server responds the client gets the answer. Can use it to update the page.
Some important things about GlideAjax are:
* It works in the background so the user interface does not freeze.
* It only works with server-side scripts that are marked as "client callable".
* It can send data from the client to the server. Get data back from the server to the client.
* It is faster and more efficient than ways of doing things like using g_form.getReference().
Using GlideAjax on the Client Side
To use GlideAjax on the client side you need to do a things. First you create a GlideAjax object and tell it which server-side script to talk to. Then you add any parameters you need to send to the server. Finally you send the request to the server. Wait for the answer.
Here are the steps:
1. Create a GlideAjax object and tell it which script to use.
2. Add any parameters you need to send to the server.
3. Send the request to the server.
4. Get the answer from the server. Use it to update the page.
Using GlideAjax on the Server Side
On the server side GlideAjax works with scripts called Script Includes. These scripts have the business logic that the client needs to use. To make a script work with GlideAjax you need to make it "client callable" and define methods to handle requests from the client.
When the client sends a request the server uses the process() method to handle it. You can get parameters from the client using this.getParameter(). Then do what you need to do. To send data back to the client you can use this.newItem(). Return a JSON string.
Good Ways to Use GlideAjax
To get the most out of GlideAjax here are some things to keep in mind:
* Keep your scripts focused on one thing so they are easy to understand and use.
* Always. Clean up any data you get from the client to keep things safe.
* Use getXMLAnswer() when you only need to send an answer back to the client.
* Use JSON to send complex data, like multiple values or objects.
* Do not use GlideRecord directly on the client side use GlideAjax to call a server-side script.
* Try to make as requests to the server as possible to keep things fast.
* Handle errors well. If something goes wrong you can fix it and tell the user what happened.
* Write comments and documentation so others can understand what you did and why.
Examples
Here are a couple of examples to show how to use GlideAjax.
Example 1: Retrieving a Single Value
i.Client-Callable Script Include (e.g., UserAjax)
var UserAjax = Class.create();
UserAjax.prototype = Object.extendsObject(AbstractAjaxProcessor, {
getUserEmail: function() {
var userID = this.getParameter('sysparm_userID');
var grUser = new GlideRecord('sys_user');
grUser.get(userID);
return grUser.email.toString();
},
type: 'UserAjax'
});ii.Client Script
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
var ga = new GlideAjax('UserAjax');
ga.addParam('sysparm_name', 'getUserEmail');
ga.addParam('sysparm_userID', newValue);
ga.getXMLAnswer(function(answer) {
g_form.setValue('user_email', answer);
});
}Example 2: Retrieving Multiple Values (JSON)
i.Client-Callable Script Include (e.g., UserDetailsAjax)
var UserDetailsAjax = Class.create();
UserDetailsAjax.prototype = Object.extendsObject(AbstractAjaxProcessor, {
getUserDetails: function() {
var userID = this.getParameter("sysparm_userID");
var userDetails = {};
var grUser = new GlideRecord("sys_user");
if (grUser.get(userID)) {
userDetails.email = grUser.email.toString();
userDetails.phone = grUser.phone.toString();
}
return JSON.stringify(userDetails);
},
type: "UserDetailsAjax"
});ii. Client Script (e.g., onChange on a User Reference Field)
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === ") {
return;
}
var ga = new GlideAjax("UserDetailsAjax");
ga.addParam("sysparm_name", "getUserDetails");
ga.addParam("sysparm_userID", newValue);
ga.getXMLAnswer(function(answer) {
var userDetails = JSON.parse(answer);
g_form.setValue("user_email", userDetails.email);
g_form.setValue("user_phone", userDetails.phone);
});
}
Conclusion:-
GlideAjax is a tool for ServiceNow developers. It helps you make your applications more dynamic and efficient. By using GlideAjax you can make your applications more responsive and user-friendly. Just remember to follow practices like keeping your scripts focused and handling errors well. With GlideAjax you can unlock the potential of client-side scripting in ServiceNow.
