- 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
‎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
‎08-29-2016 12:23 PM
i rewrite my server-side script into this one:
var GetUserData = Class.create();
GetUserData.prototype = Object.extendsObject(AbstractAjaxProcessor, {
getUserInfo: function() {
var obj = {};
var usr = this.getParameter('sysparm_user');
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);
glide.query();
if(glide.next()){
obj.company = glide.name; }
else {
obj.company = '';
}
}
var json = new JSON();
var data = json.encode(obj);
return data;
},
type: 'GetUserData'
});
and client-side into this , i don't get the result populated, instead i receive message - object Object. Did i miss smth?
var usr = g_form.getValue('open_on_behalf_of');
var ga = new GlideAjax('GetUserData');
ga.addParam('sysparm_name', 'getUserInfo');
ga.addParam('sysparm_user', usr);
ga.getXML(AnswerParse);
function AnswerParse(response) {
var answer = response.responseXML.documentElement.getAttribute("answer");
answer = answer.evalJSON(); //Transform the JSON string to an object
g_form.setValue("company",answer.company); // Display Company
alert(answer.company)// here i receive object Object message
}

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-29-2016 12:54 PM
You should not need to do a second query to get the company if you already have the user. You can get the name from the user record by dot-walking.
Reference:
var GetUserData = Class.create();
GetUserData.prototype = Object.extendsObject(AbstractAjaxProcessor, {
getUserInfo: function() {
var obj = {};
var usr = this.getParameter('sysparm_user');
var cp = new GlideRecord('sys_user');
if(cp.get(usr)) {
var obj.company = {};
obj.company.name = usr.company.getDisplayValue();
obj.company.sys_id = usr.company;
}
var data = JSON.stringify(obj);
return data;
},
type: 'GetUserData'
---------
var usr = g_form.getValue('open_on_behalf_of');
var ga = new GlideAjax('GetUserData');
ga.addParam('sysparm_name', 'getUserInfo');
ga.addParam('sysparm_user', usr);
ga.getXML(AnswerParse);
function AnswerParse(response) {
var answer = response.responseXML.documentElement.getAttribute("answer");
var myObj = answer.evalJSON(); //Transform the JSON string to an object
g_form.setValue("company",answer.company.sys_id, answer.company.name);
}

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-16-2017 08:50 AM
Hi ctomasi,
I am using following scripts into scoped application, but it is not working:
Client Script:
var ga = new GlideAjax('getUserData');
ga.addParam('sysparm_name','getUserDetails');
ga.addParam('sysparm_sysid', newValue);
ga.getXML(HelloWorldParse);
function HelloWorldParse(response) {
var answer = response.responseXML.documentElement.getAttribute('answer');
var myObj = answer.evalJSON();
g_form.setValue('v_location', myObj.loc);
g_form.setValue('v_business_unit', myObj.bu);
}
Script Include:
var getUserData = Class.create();
getUserData.prototype = Object.extendsObject(global.AbstractAjaxProcessor, {
getUserDetails: function() {
var usr = this.getParameter('sysparm_sysid');
var obj = {};
var gr = new GlideRecord('sys_user');
gr.addQuery('sys_id', usr);
gr.query();
if(gr.next()) {
obj.loc = usr.location;
obj.bu = usr.department.business_unit;
}
var data = JSON.stringify(obj);
return data;
},
type: 'getUserData'
});
Is JSON supported in scoped application? If not, what's the workaround here?

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-16-2017 01:17 PM
evalJSON() may not be available in scoped applications. Try using:
var myObj = JSON.parse(answer);
instead of
var myObj = answer.evalJSON();
Cheers,
Tim