Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

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

Thanks a lot, it has been resolved already.

Milan

Thanks a lot.