logo

NJP

HowTo - Ajax Calls: Use GlideRecords on the client side (easy)

Import · Aug 23, 2019 · article

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):

image

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 ...

View original source

https://www.servicenow.com/community/developer-blog/howto-ajax-calls-use-gliderecords-on-the-client-side-easy/ba-p/2288390