How to pass results from one query to use in another query

Nelson Ma
Tera Contributor

Hello Developers,

 

I am trying to retrieve the sysid from some user generated events from the syslog table. Get the message from the syslog table, slice the string and only return the value I need which is the sysid value. Use the sysid values and query it in the sys_user table to retrieve the displayvalues.


I am getting the results I want from the first query but the second query is fetching all the data from the sys_user table which I don't want. I just want to pass the results from the first query and iterate through result. I think the second query does not know about the variable from the first query. How can I achieve this?

 

grlog = new GlideRecord('syslog');
grlog.addEncodedQuery("customcopiedquery");
grlog.query(); //query to go through the logs table.

while(grlog.next()){

var messages = grlog.message;
var msgstr = messages.toString();
var sysid = msgstr.slice(57,89); //slice the string and only return what I need.

gs.print(sysid); //I am able to get the proper string value I need by verifying the output.

grSys = new GlideRecord('sys_user');
grSys.addquery('sys_id',sysid); //query the sys_id and the value from the first query. this is not working.
grSys.query();
while(grSys.next()){
gs.print(grSys.uniqueValue);
gs.print(grSys.first_name);
gs.print(gr.Sys.last_name);
}
}

 

 

2 ACCEPTED SOLUTIONS

Mike_R
Kilo Patron
Kilo Patron

is sysid a single value or comma separated?

If its a single value, can you try sysid.toString()

View solution in original post

mdash
Giga Guru

Like Mike said, you can use sysid.toString() and change the 'while' to 'if'. 
Also there are couple of typos/syntax errors. (addquery, uniqueValue)

Fixing all these, your second function can be:

 

 

grSys = new GlideRecord('sys_user');
grSys.addQuery('sys_id',sysid.toString()); //query the sys_id and the value from the first query. this is not working.
grSys.query();
if(grSys.next()){
gs.print(grSys.getUniqueValue());
gs.print(grSys.first_name);
gs.print(grSys.last_name);
}

 

 

View solution in original post

3 REPLIES 3

Mike_R
Kilo Patron
Kilo Patron

is sysid a single value or comma separated?

If its a single value, can you try sysid.toString()

mdash
Giga Guru

Like Mike said, you can use sysid.toString() and change the 'while' to 'if'. 
Also there are couple of typos/syntax errors. (addquery, uniqueValue)

Fixing all these, your second function can be:

 

 

grSys = new GlideRecord('sys_user');
grSys.addQuery('sys_id',sysid.toString()); //query the sys_id and the value from the first query. this is not working.
grSys.query();
if(grSys.next()){
gs.print(grSys.getUniqueValue());
gs.print(grSys.first_name);
gs.print(grSys.last_name);
}

 

 

Nelson Ma
Tera Contributor

Thank you @mdash and @Mike_R for the help! I am new to all of this.


Now I will try to add this to an action step in Flow Designer.

var sysarray = [];
grlog = new GlideRecord('syslog');
grlog.addEncodedQuery("customcopiedquery;
grlog.query();

while(grlog.next()){

var messages = grlog.message;
var msgstr = messages.toString();
var sysid = msgstr.slice(57,89);

sysarray.push(sysid.toString());

}


for (var i =0; i <sysarray.length; i++){
var user = sysarray[i];
grSys = new GlideRecord('sys_user');
grSys.addQuery('sys_id',user.toString()); //query the sys_id and the value from the first query. this is not working.
grSys.query();
if(grSys.next()){
gs.print(grSys.getUniqueValue());
gs.print(grSys.first_name);
gs.print(grSys.last_name);
}}