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

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.

 

Gotcha. Thanks Chuck.

This works as expected. Thanks a lot.

Milan

I recommend not using item.toString() since it can throw an exception/error (can't convert null to an object) if the item is null. You should at least verify that the item is not null before using item.toString(). I prefer to use the String constructor such as String(userGR.manager.manager). This tells you first off that you are creating a String. It works for everything. The only issue is that the string that is created could be 'null' or 'undefined'. But if you test it first then it won't be a problem. For example:
if(userGR.manager.manager) var manager = String(userGR.manager.manager);
If item can be zero then you can add that to the if statement:
if(item || 0 === item) var itemString = String(item);

Another good reason to use the String constructor is that it will always create a JavaScript String. Java objects also have a toString() function but will create a Java String. The functions and parameters for these different strings are different so if you think you are handling a JavaScript String but actually it is a Java String you may get unexpected results, especially with special characters as the separator. Consider the following:
JavaScript String: str.split([separator[, limit]]) which returns a JavaScript array
Java String: split(String regex) or split(String regex, int limit) which returns a Java array

So if you use var itemString = String(item); the itemString will always be a JavaScript String which should make coding easier.