The CreatorCon Call for Content is officially open! Get started here.

Fabian Kunzke
Kilo Sage
Kilo Sage

When working on different projects i often hear the same question: Why do i have to use a Glide Ajax here? It's just a Glide Record. Can't i just use that instead. Often times Glide Ajax is not used because it seems to be very time consuming to establish. Within the following few lines i'll provide a short and easy way of using GlideRecords on the client side. With this, there should no longer be an excuse to not use client scripts with ajax calls.

(short note: The example below is just an introduction to Glide Ajax. The main advantage of GlideAjax is to do more complex datamanipulation than "just" a GlideRecord without impacting the client side interactions of the user. If your goal is to just retrieve record information or related information use the GlideRecoord or .getReference()) (thanks to @David Dubuis for this addition)

This essentially contains a generic server side script include as well as some easy GlideAjax. You can just copy and paste the code examples below.

 

Generic server side script include:

On the form of the script-include ensure to check the box "Client callable" (thanks to @Allen A. for this addition):

find_real_file.png

Within the script part add: 

var AJAXGlideRecord = Class.create();
AJAXGlideRecord.prototype = Object.extendsObject(AbstractAjaxProcessor, {
	
	getRecord: function()
	{
		
		var table = this.getParameter('sysparm_table');
		var query = this.getParameter('sysparm_query');

		if(!table || !query)
		        return; //-> if the table or query is missing do nothing
		
		var record = new GlideRecordSecure(table);
		record.addEncodedQuery(query);
		record.query();
		if(record.next())
			return JSON.stringify(record);
		
		return;
	},
	
	type: 'AJAXGlideRecord'
});

Generic AjaxCall:

function doTheAjaxCallForTheGlideRecord()
{
	var glideAjax = new GlideAjax('AJAXGlideRecord');
	glideAjax.addParam('sysparm_name', 'getRecord');
	glideAjax.addParam('sysparm_table', 'insert tablename here');
	glideAjax.addParam('sysparm_query', 'insert encoded query here');
	glideAjax.getXML(doSomethingWithMyRecord);
}

function doSomethingWithMyRecord(response)
{
	var recordAsJSON = response.responseXML.documentElement.getAttribute("answer");
	var record = JSON.parse(recordAsJSON);
	// => do some stuff with the record here
}

[Edit: Thanks to @Daniel Oderbolz for the contribution on pointing out the missing error handling and the use of GlideRecordSecure]

Now you can reuse this ajax call wherever you need a single GlideRecord record within the client side based on providing a table name as well as an encoded query. If you want, you can extend the server side script include with more functions. Here are some ideas i use frequently:

- A function that returns a glideAggregate for reporting
- A function that returns all related childs of a child table
- A function that returns the only child record of a parent
- A function that returns a cmdb-relationship-path with all CIs from a starting CI all the way up to the Business Service
- A function that returns a generic Chart.js object (great for service portal reports where the oob ...

These functions can be coded in a similar fashion. Remember to build them as a reusable function library. That way GlideAjax won't be a "oh no not this again" matter.

Super secret reoccurring coding advice: Templates can be used for Client Scripts! If you know a client script uses something like this, have a template ready to save some time.

Regards

Fabian

 

ps.: if i find the time i will expand this blog with the examples stated.

14 Comments