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

Chuck Tomasi
Tera Patron

You need to change the userGR.sys_id to this:

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.getValue('sys_id')); 
  }

  return JSON.stringify(customIncLocUsers);
},

Can you please advise how to use the array "customIncLocUsers" as a string argument for my second query?

var incidentGR = new GlideRecord('incident');
incidentGR.addQuery('caller_id', 'IN', '02826bf03710200044e0bfc8bcbe5d6d, 02826bf03710200044e0bfc8bcbe5d76');
incidentGR.query();
while (incidentGR.next()) {
gs.print(incidentGR.number);
}

What I need is to supply the query above with a stringified "customIncLocUsers" array arguments instead of hardcoded sys_ids as shown above.

 

I think the output made by JSON.stringify is not the right one, basicallly I need to put the sys_ids in between ' ' after the 'IN' argument.

 

Appreciate your help,

Milan

Before I got any further on the technical ideas, I need to understand what you are trying to do. What is the business requirement of this? It could be that a GlideAjax request isn't even the right way to go. If you can give a breakdown of what's required and how you expect it to work, I may be able to steer you to the proper solution.

Thanks

Don't worry, I have already done this...thanks a lot.

Milan