How to push glideRecord values to an array in a script include and get array of GlideRecord values retrieved in a client script

Milan13
Giga Expert

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

 

 

 

 

1 ACCEPTED SOLUTION

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.

 

View solution in original post

16 REPLIES 16

Excellent. I'm glad you got your solution. Care to share your solution so the rest of the community can benefit?

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);

},

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.

// 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
}
}

Imran Makandar
ServiceNow Employee
ServiceNow Employee

You can do below change -

while (userGR.next()) {
customIncLocUsers.push(userGR.sys_id.toString()); 
}