- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago - last edited 3 weeks ago
Hi @SRIRAMSANKAR007,
Here is your answer:
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); }
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'});
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!'); } }
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
This is the best explanation
https://www.servicenow.com/community/developer-articles/glideajax-example-cheat-sheet/ta-p/2312430
This helps other users find accurate and useful information more easily
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
The important point is:
Client Script → runs in the browser
Script Include → runs on the server
GlideAjax → connects them
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago - last edited 3 weeks ago
Hi @SRIRAMSANKAR007,
Here is your answer:
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); }
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'});
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!'); } }
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
