- 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:28 AM
Hi Anna,
Check this out. This might help you. You can use that example to edit your code.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-29-2016 11:37 AM
Use JSON to pass multiple values from server to client. Go thru this link
https://fruitionpartners.eu/blog/2015/11/17/glideajax-return-multiple-values-using-json/
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-31-2016 06:01 AM
Thank you for reply, this article indeed was very helpful.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-31-2016 06:07 AM
You are very welcome. I have been meaning to make a video of how to do this since it comes up so many times. You've inspired me to get a little closer to completion.