- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-28-2019 05:58 AM
Hello,
I am trying to get an array of values (sys_id) from sys_user table - these are queried based on location information.
My Client Script Ajax:
var ga = new GlideAjax('IncidentUtils1');
ga.addParam('sysparm_name', 'getLocationUsers');
ga.addParam('sysparm_cust_inc_loc', g_form.getValue('u_location'));
ga.getXML(AjaxParse);
function AjaxParse(response) {
var answer = response.responseXML.documentElement.getAttribute("answer");
//alert(answer);
//var userSysIds = answer.evalJSON();
var userSysIds = JSON.parse(answer);
for (var i=0; i< userSysIds.length; i++) {
alert(userSysIds[i]);
}
My Script Include method:
getLocationUsers: function () {
//variable for Incident custom field u_location
var customIncLocUsers = [];
var cust_inc_locSi = this.getParameter('sysparm_cust_inc_loc');
var userGR = new GlideRecord('sys_user');
userGR.addQuery('location', cust_inc_locSi);
userGR.query();
while (userGR.next()) {
customIncLocUsers.push(userGR.sys_id);
}
return JSON.stringify(customIncLocUsers);
},
What I am getting is "[object Object]" - not sure what I am doing wrong as I expect an array of sys_id values from sys_user table.
Can anyone please advise?
Much appreciated,
Milan
Solved! Go to Solution.
- Labels:
-
Scripting and Coding

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-28-2019 06:30 AM
I recommend only using toString() when a getter isn't available. In this case, you can use either getValue('sys_id') or getUniqueValue() to get the sys_id.
The only time you should use toString() is when you don't have access to a getter like getValue(). This would be when you dot-walk past the first level property. Ex: userGR.manager.manager.toString(); There's not getValue() on the end that so toString() makes the most sense.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-01-2019 05:50 AM
Excellent. I'm glad you got your solution. Care to share your solution so the rest of the community can benefit?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-01-2019 05:58 AM
Part of my script include code used:
getLocationUsers: function () {
//variable for Incident custom field u_location
//returns array of user sys IDs from sys_user table based on u_location field in incident form
var customIncLocUsers = [];
//returns incidents - those with caller_id - caller_id supplied with array of user sys IDs from array customIncLocUsers
var customIncLocIncs = [];
var cust_inc_locSi = this.getParameter('sysparm_cust_inc_loc');
var userGR = new GlideRecord('sys_user');
userGR.addQuery('location', cust_inc_locSi);
userGR.query();
while (userGR.next()) {
customIncLocUsers.push(userGR.getValue('sys_id').toString());
}
JSON.stringify(customIncLocUsers);
var incidentGR = new GlideRecord('incident');
incidentGR.addQuery('caller_id', 'IN', customIncLocUsers);
incidentGR.addQuery('state','NOT IN', '7,8');
incidentGR.query();
while (incidentGR.next()) {
customIncLocIncs.push(incidentGR.getValue('number'));
}
return JSON.stringify(customIncLocIncs);
},
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-19-2021 01:36 AM
Hi Milan, Chuck,
How can I pass this to callback function in glideajax in my Client Script, It's returning null every time,
I checked logging the value from SI and the value is present.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-19-2021 10:03 AM
// in my Client Script HandleGetLocationUsersResponse is the callback function
function isArray(a) {
if(isUndefined(a) ) return false;
return (a instanceof Array);
}
function isUndefined(a) {return (a === undefined);}
var ga = new GlideAjax('MyScriptInclude');
ga.addParam('sysparm_name', 'getLocationUsers');
ga.addParam('sysparm_cust_inc_loc', cust_inc_loc);
ga.getXMLAnswer(HandleGetLocationUsersResponse);
function HandleGetLocationUsersResponse (answer) {
if(DEBUG) console.log('%c my Client Script HandleGetLocationUsersResponse answer:' + answer, 'color:blue');
if( !answer ) throw( 'name:InvalidArgument, message:"answer is invalid. Expected a JSON encoded string", details:"answer:<' + answer + '>"' );
var customIncLocIncs = JSON.parse(answer);
if( !customIncLocIncs ) ) throw( 'name:InvalidArgument, message:"customIncLocIncs is invalid. Expected an array", details:"customIncLocIncs:<' + customIncLocIncs + '>"' );
if( !isArray(customIncLocIncs) ) throw( 'name:InvalidArgument, message:"customIncLocIncs is invalid. Expected an array", details:"customIncLocIncs:<' + JSON.stringify(customIncLocIncs) + '>"' );
for(var index in customIncLocIncs) {
var value = customIncLocIncs[index];
//your code here
}
}

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-28-2019 06:22 AM
You can do below change -
while (userGR.next()) {
customIncLocUsers.push(userGR.sys_id.toString());
}