Use PDIs? Take our 5-minute survey to help shape the PDI roadmap.

GlideAjax

SRIRAMSANKAR007
Tera Contributor

Explain GlideAjax in a simple way with two to three simple examples

3 REPLIES 3

Rafael Batistot
Kilo Patron

Hi @SRIRAMSANKAR007 

 

This is the best explanation 

 

https://www.servicenow.com/community/developer-articles/glideajax-example-cheat-sheet/ta-p/2312430

 

If this response was helpful, please mark it as Helpful and, if applicable, as Correct.
This helps other users find accurate and useful information more easily

dinesdh
Tera Contributor

The important point is:

Client Script → runs in the browser
Script Include → runs on the server
GlideAjax → connects them

yashkamde
Giga Sage

Hello @SRIRAMSANKAR007 ,

 

GlideAjax is a client side API in ServiceNow that enables client scripts to execute server side code without reloading the webpage. It acts as a performance friendly communication between the browser (Client Script) and the database (Script Include) to retrieve data or perform calculations dynamically.

 

For Examples :

1) Get today's date/server time :

var DateUtils = Class.create();
DateUtils.prototype = Object.extendsObject(AbstractAjaxProcessor, {
    getServerDate: function() {
        return new GlideDateTime().getDisplayValue();
    },
    type: 'DateUtils'
});

 

client script :

var ga = new GlideAjax('DateUtils');
ga.addParam('sysparm_name', 'getServerDate');
ga.getXML(function(response) {
    var date = response.responseXML.documentElement.getAttribute('answer');
    alert('Server date: ' + date);
});

 

2) Passing a parameter: Check if Incident has open Problem 

script include :

var IncidentUtils = Class.create();
IncidentUtils.prototype = Object.extendsObject(AbstractAjaxProcessor, {
    hasOpenProblem: function() {
        var incidentSysId = this.getParameter('sysparm_incident_id');
        var gr = new GlideRecord('problem');
        gr.addQuery('rfi', incidentSysId); // example query
        gr.addActiveQuery();
        gr.query();
        return gr.hasNext() ? 'true' : 'false';
    },
    type: 'IncidentUtils'
});

 

client script :

var ga = new GlideAjax('IncidentUtils');
ga.addParam('sysparm_name', 'hasOpenProblem');
ga.addParam('sysparm_incident_id', g_form.getUniqueValue());
ga.getXMLAnswer(function(answer) {
    if (answer === 'true') {
        g_form.showFieldMsg('short_description', 'This incident has a linked open problem', 'warning');
    }
});

 

If my response helped mark as helpful and accept the solution.