Glide Ajax 101-Beginner Level ( Inspired by Shawn Dowler)

sadif_raja
Tera Guru

Overview


Overview

GlideAjax can be tricky to use due to its client-server structure. To simplify its implementation, I created this color-coded example to ensure all details are covered when setting up a new call. This guide includes an image, text, and a downloadable Word document for your convenience.

Important: Ensure the Client callable option is checked in your Script Include for GlideAjax to work.


Color Codes Explanation

🟡 Yellow:
The name of the class in the Script Include, often identical to the Script Include's name.

🟣 Magenta:
The name of the function in the Script Include. You can have multiple functions within a single Script Include, each handling different parameters and processes.

🟢 Green:
Parameters passed via the URL during the AJAX call. Typically, these are used to retrieve data using a GlideRecord query.

🔴Red:
The callback function that waits for a server response. This function, referenced by getXML(), handles operations dependent on the server response. Code that doesn’t need to wait for a response can be placed directly after the getXML() call.

🔵Cyan:
Data returned from the server, encapsulated in an object. These values can be utilized for various tasks, such as setting field values in a form.


Client-Side Code (Client Script)

function onChange(control, oldValue, newValue, isLoading) { if (isLoading || newValue === '') { return; } var ga = new GlideAjax('asu_GetLocationData'); ga.addParam('sysparm_name', 'getCampus'); ga.addParam('sysparm_buildingid', g_form.getValue("u_building")); ga.getXML(updateCampus); } function updateCampus(response) { var answer = response.responseXML.documentElement.getAttribute("answer"); var clearValue; // Undefined by default if (answer) { var returnedData = JSON.parse(answer); g_form.setValue("campus", returnedData.sys_id, returnedData.name); } else { g_form.setValue("campus", clearValue); } }

Server-Side Code (Script Include):

var asu_GetLocationData = Class.create();

asu_GetLocationData.prototype = Object.extendsObject(AbstractAjaxProcessor, {
getCampus: function () {
var buildingId = this.getParameter('sysparm_buildingid');
var loc = new GlideRecord('cmn_location');

if (loc.get(buildingId)) {
var campus = new GlideRecord('cmn_location');

if (campus.get(loc.parent)) {
return JSON.stringify({
"sys_id": campus.getValue("sys_id"),
"name": campus.getValue("name")
});
}
}
return null;
}
});

 


Thanks
Credits: Shawn Dowler

Tera Guru
1 REPLY 1

Zach Koch
Giga Sage
Giga Sage

You should give credit to the original article, as yours is using the exact same code (just changing getXMLAnswer to getXML) and color structuring with a few words changed in the color descriptions, instead of passing it off as your own. This makes it confusing for people who are looking for information on their issues.

Original article:

GlideAjax Example Cheat Sheet (UPDATED) 

If this information helped resolve your issue, please remember to mark response correct and thumbs up to help future community members on this information, thanks!