Interested in a ServiceNow event built for developers? Registration for now[dev]26 is officially open!

GlideAjax

SRIRAMSANKAR007
Tera Expert

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

2 ACCEPTED SOLUTIONS

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.

 

 

View solution in original post

SohamTipnis
Mega Sage

Hi @SRIRAMSANKAR007,

 

Here is your answer:

 

Example 1: Auto-populate a User's Email
When you change the "Caller" field on an incident form, you want to automatically pull their email address from the user table.
1. Client Script (The Request)
 
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
    if (isLoading || newValue === '') {
        return;
    }

    // 1. Initialize GlideAjax and name the Script Include
    var ga = new GlideAjax('UserInformationUtils'); 
    
    // 2. Specify the exact function (method) to run on the server    ga.addParam('sysparm_name', 'getUserEmail'); 
    
    // 3. Pass the User's Sys ID to the server    ga.addParam('sysparm_user_id', newValue); 
    
    // 4. Send the request and wait for the response in the background    ga.getXMLAnswer(populateEmailField); 
}

// 5. This function runs automatically when the server answers
function populateEmailField(response) {
    g_form.setValue('u_caller_email', response);
}
 
2. Script Include (The Server Action)
Must be marked as "Client callable."
 
var UserInformationUtils = Class.create();
UserInformationUtils.prototype = Object.extendsObject(AbstractScriptProcessor, {

    getUserEmail: function() {
        // Grab the parameter passed from the client script
        var userId = this.getParameter('sysparm_user_id'); 
        
        var userGR = new GlideRecord('sys_user');
        if (userGR.get(userId)) {
            return userGR.getValue('email'); // Return email back to the client        }
        return '';
    },

    type: 'UserInformationUtils'});
 
Example 2: Check if a User is a VIP
When a form loads, you want to check if the current user is a VIP. If they are, you display a special warning message on the screen.
1. Client Script (The Request)
 
 
function onLoad() {
    var ga = new GlideAjax('UserInformationUtils');
    ga.addParam('sysparm_name', 'checkVipStatus');
    ga.addParam('sysparm_user_id', g_user.userID); // Gets current logged-in user    ga.getXMLAnswer(showAlertForVIP);
}

function showAlertForVIP(response) {
    if (response === 'true') {
        g_form.addInfoMessage('You are creating a ticket for a VIP user!');
    }
}
 
 
2. Script Include (The Server Action)
Add this function inside your existing UserInformationUtils Script Include.
 
checkVipStatus: function() {
    var userId = this.getParameter('sysparm_user_id');
    var userGR = new GlideRecord('sys_user');
    
    if (userGR.get(userId)) {
        // Returns 'true' or 'false' as a string
        return userGR.getValue('vip').toString(); 
    }
    return 'false';
},



If you find my answer useful, please mark it as helpful and correct. ‌😊


Regards,
Soham Tipnis
ServiceNow Developer || Technical Consultant
LinkedIn: www.linkedin.com/in/sohamtipnis10

 

View solution in original post

4 REPLIES 4

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

Dinesh 89
Tera Contributor

The important point is:

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

dinesh kumar

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.

 

 

SohamTipnis
Mega Sage

Hi @SRIRAMSANKAR007,

 

Here is your answer:

 

Example 1: Auto-populate a User's Email
When you change the "Caller" field on an incident form, you want to automatically pull their email address from the user table.
1. Client Script (The Request)
 
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
    if (isLoading || newValue === '') {
        return;
    }

    // 1. Initialize GlideAjax and name the Script Include
    var ga = new GlideAjax('UserInformationUtils'); 
    
    // 2. Specify the exact function (method) to run on the server    ga.addParam('sysparm_name', 'getUserEmail'); 
    
    // 3. Pass the User's Sys ID to the server    ga.addParam('sysparm_user_id', newValue); 
    
    // 4. Send the request and wait for the response in the background    ga.getXMLAnswer(populateEmailField); 
}

// 5. This function runs automatically when the server answers
function populateEmailField(response) {
    g_form.setValue('u_caller_email', response);
}
 
2. Script Include (The Server Action)
Must be marked as "Client callable."
 
var UserInformationUtils = Class.create();
UserInformationUtils.prototype = Object.extendsObject(AbstractScriptProcessor, {

    getUserEmail: function() {
        // Grab the parameter passed from the client script
        var userId = this.getParameter('sysparm_user_id'); 
        
        var userGR = new GlideRecord('sys_user');
        if (userGR.get(userId)) {
            return userGR.getValue('email'); // Return email back to the client        }
        return '';
    },

    type: 'UserInformationUtils'});
 
Example 2: Check if a User is a VIP
When a form loads, you want to check if the current user is a VIP. If they are, you display a special warning message on the screen.
1. Client Script (The Request)
 
 
function onLoad() {
    var ga = new GlideAjax('UserInformationUtils');
    ga.addParam('sysparm_name', 'checkVipStatus');
    ga.addParam('sysparm_user_id', g_user.userID); // Gets current logged-in user    ga.getXMLAnswer(showAlertForVIP);
}

function showAlertForVIP(response) {
    if (response === 'true') {
        g_form.addInfoMessage('You are creating a ticket for a VIP user!');
    }
}
 
 
2. Script Include (The Server Action)
Add this function inside your existing UserInformationUtils Script Include.
 
checkVipStatus: function() {
    var userId = this.getParameter('sysparm_user_id');
    var userGR = new GlideRecord('sys_user');
    
    if (userGR.get(userId)) {
        // Returns 'true' or 'false' as a string
        return userGR.getValue('vip').toString(); 
    }
    return 'false';
},



If you find my answer useful, please mark it as helpful and correct. ‌😊


Regards,
Soham Tipnis
ServiceNow Developer || Technical Consultant
LinkedIn: www.linkedin.com/in/sohamtipnis10