- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-29-2016 11:25 AM
Using GlideAjax single call I expect to return multiple data about user and set it on the request form. However, I'm slightly confused with the returning of multiple values. Below script was a try, but not successful. Any suggestions on how to improve and optimize the below scripts?
client-side script
function onLoad() {
var usr = g_form.getValue('open_on_behalf_of');//getting user sys_id
var ga = new GlideAjax('GetUserData'); // with this single call i want to receive various user data like user company, division, location and etc.
ga.addParam('sysparm_name', 'getUserInfo');
ga.addParam('sysparm_user', usr);
ga.getXML(AnswerParse);
function AnswerParse(response) {
var answers = response.responseXML.documentElement.getElementsByTagName('usr_comp'); // not sure what to receive here
for (var i = 0; i < answers.length; i++) {
g_form.setValue(company,answers[i].getAttribute('result'));
g_form.setValue(company,answers[i].getAttribute('result'));
} }
}
server side script include
var GetUserData = Class.create();
GetUserData.prototype = Object.extendsObject(AbstractAjaxProcessor, {
getUserInfo: function() {
var usr = this.getParameter('sysparm_user');
this._getComp(usr);
},
//query user company
_getComp: function(usr) {
var cp = new GlideRecord('sys_user');
cp.addQuery('sys_id',usr);
cp.query();
if(cp.next()) {
var core_comp = cp.company;
var glide = new GlideRecord('core_company');
glide.addQuery('sys_id',core_comp); // query user company
glide.query();
if(glide.next()){
var usr_comp = glide.name;
var company = this.newItem('usr_comp');
company.setAttribute('result',usr_comp);
}
else {
return ''; }
}
else { return '';} },
_getDiv: function(usr) {
var gr = new GlideRecord('sys_user');
gr.addQuery('sys_id',usr);
gr.query();
if(gr.next()) {
var division = gr.u_division;
var glide = new GlideRecord('u_division');
glide.addQuery('sys_id',u_division); // query user division
glide.query();
if(glide.next()){
glide.query();
if(glide.next()){
var usr_div = glide.name;
var div = this.newItem('usr_div');
division.setAttribute('result',usr_div);
}
else { return ''; }
},
type: 'GetUserData'
});
Solved! Go to Solution.
- Labels:
-
Scripting and Coding

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-29-2016 11:40 AM
I do this all the time. I create an object or array of objects on the server.
var myObj = {};
// Get your stuff here
// populate myObj with stuff
return JSON.stringify(myObj);
Then on the client side, you retrieve your answer variable like you normally would (using the document XML parsing), then decode the JSON string.
var myObj = JSON.parse(answer);
All the object bits are right where you need them for processing in the client script.
Docs: Client Scripts
Docs: GlideForm
Docs: GlideAjax
Client Script Best Practices - ServiceNow Wiki
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-10-2017 08:18 AM
Hi Chuck,
I need to call two functions which are defined in same script include.
Can you please suggest me how to do it?
Regards,
Shamma

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-10-2017 12:38 PM
Hi Shamma,
Your script include can have multiple functions. When you make a GlideAjax call, the sysparm_name parameter determines which function gets called.
var ga = new GlideAjax('MyScriptIncludeAjax');
ga.addParam('sysparm_name', 'myFunction');
ga.addParam('sysparm_anotherparm', g_form.getValue('field'));
ga.getXML(myCallBack);
In this example, MyScriptInclude is the name of the script include, myFunction is one of the functions in that script include, and I pass an optional additional parameter that myFunction can use with this.getParameter('sysparm_anotherparm').
If you have another function in your script include, just change the name on the line that sets up the sysparm_name attribute. That's the only 'hard-coded' piece to GlideAjax.
Docs: Client Scripts
Docs: GlideForm
Docs: GlideAjax
Client Script Best Practices - ServiceNow Wiki
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-10-2017 10:23 PM
Hi Chuck,
This is my onSubmit Client script which is calling two GLIDEAJAX function:
function onSubmit() {
var ga = new GlideAjax('AjaxFunctions');
ga.addParam('sysparm_name','checkServerApplications');
ga.addParam('sysparm_servers',g_form.getValue('affected_servers'));
ga.getXMLWait();
if (ga.getAnswer() == 'noapp'){
return true;
}
else{
g_form.clearMessages();
g_form.addErrorMessage(ga.getAnswer());
return false;
}
var cdt = g_form.getValue('end_date');
var ajax = new GlideAjax('AjaxFunctions');
ajax.addParam('sysparm_name', 'addDateTimeAmount');
ajax.addParam('sysparm_end_date', cdt);
ajax.getXMLWait();
var res = ajax.getAnswer();
if(res == 'true')
{
g_form.showErrorBox('end_date', 'Please select an end date at least three days from now.', true);
return false;
}
else
{
return true;
}
}
This script is not working however if I comment anyone GLIDEAJAX call then it works fine.However both of them are not working together.
Please suggest how to proceed further?
Regards.,
Shamma

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-11-2017 05:42 AM
Hi Shamma,
Take a look at episode 23 of TechNow we did last month. It talks about this almost exactly.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-26-2017 10:01 AM
Hi Chuck,
Thank you so much!!
It is resolved now. I mentioned some conditions in Client scripts and it worked.
Thank alot!
Code mentioned below:
function onSubmit() {
var datetime = addDateTimeAmount();
var checkserver = checkServerApplications();
if(datetime == false || checkserver == false)
{
return false;
}
else if(datetime == true && checkserver == true)
{
return true;
}
}
function addDateTimeAmount() {
var cdt = g_form.getValue('end_date');
var ajax = new GlideAjax('AjaxFunctions');
ajax.addParam('sysparm_name', 'addDateTimeAmount');
ajax.addParam('sysparm_end_date', cdt);
ajax.getXMLWait();
var res = ajax.getAnswer();
if(res == 'true')
{
g_form.showErrorBox('end_date', 'Please select an end date at least three days from now.', true);
return false;
}
else
{
return true;
}
}
function checkServerApplications() {
var ga = new GlideAjax('AjaxFunctions');
ga.addParam('sysparm_name','checkServerApplications');
ga.addParam('sysparm_servers',g_form.getValue('affected_servers'));
ga.getXMLWait();
if (ga.getAnswer() == 'noapp'){
return true;
}
else{
g_form.clearMessages();
g_form.addErrorMessage(ga.getAnswer());
return false;
}
}
Regards,
Shamma