- Post History
- Subscribe to RSS Feed
- Mark as New
- Mark as Read
- Bookmark
- Subscribe
- Printer Friendly Page
- Report Inappropriate Content
2 hours ago
Last week, I was looking through some old code and realized just how much extra weight we often put on our server calls. Let’s break down the "Wrong" vs. the "Right" way to handle GlideAjax so your instances stay fast and your code stays clean.
The Common Trap: The "Wrong" Way
We’ve all written code like this. It’s functional, but it’s heavy.
Client Script -
function onChange(control, oldValue, newValue) {
var ga = new GlideAjax('UserUtils');
ga.addParam('sysparm_name', 'getUserDetails');
ga.addParam('sysparm_user', newValue);
ga.getXML(handleResponse);
}
Script Include -
getUserDetails: function() {
var gr = new GlideRecord('sys_user');
gr.get(this.getParameter('sysparm_user'));
return gr; // ❌ The mistake: sending the entire record object
}
-
Calls on every change: Without a check, this fires even when the field is cleared.
-
Returns full record: Sending an entire
GlideRecordobject back to the client is massive overhead. -
Triggers excessive server calls: This creates unnecessary traffic and high latency.
The "Right" Way
Only call the server when you have to and only bring back exactly what you need.
Client Script -
function onChange(control, oldValue, newValue) {
if (!newValue) return; // ✅ Check if newValue exists before calling the server
var ga = new GlideAjax('UserUtils');
ga.addParam('sysparm_name', 'getUserLocation');
ga.addParam('sysparm_user', newValue);
ga.getXMLAnswer(function(answer) {
// Handle logic here
});
}
Script Include (Client Callable) -
getUserLocation: function() {
var userId = this.getParameter('sysparm_user');
var gr = new GlideRecord('sys_user');
if (gr.get(userId)) {
return gr.location.toString(); // ✅ Return only the specific value you need
}
}
Why this is better:
-
Calls only when needed: The
if (!newValue) return;line prevents empty calls. -
Returns single value: It sends a small string instead of a giant object.
-
Minimizes server load: It keeps the transaction light and the user interface snappy.
Summary -
-
Use only for targeted server calls: Don't use
GlideAjaxfor data you could have already available on the client. -
Return minimal data: If you only need a Location ID, don’t return the whole User record. If you need multiple values, return a small JSON string.
-
Test with the Network tab: Open your browser's developer tools, go to the Network tab, and look at the size and time of your
xmlhttp.docalls. If it looks bloated, it’s time to optimize.
