- 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-27-2017 06:08 AM
I am glad you got it taken care of. Don't forget to mark the appropriate comment as correct so that others with the same question in the future can find it quickly and that it gets removed from the Unanswered list.
If you are viewing this from the community inbox you will not see the correct answer button. If so, please review How to Mark Answers Correct From Inbox View.
Thank you