- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-04-2019 07:59 AM
Hi,
I need assistance with querying one table and using the results to query on another table.
The 1st query is on the hardware table and it working and it returns multiple users.
Then I need to query the sc_req_item to check if they got active request item open , but its only returning one results but im expecting a lot more.
Code:
var asset_query = "*****";
var ast = new GlideAggregate('alm_hardware');
ast.addEncodedQuery(asset_query);
ast.groupBy('assigned_to');
ast.query();
while(ast.next()){
var gr = new GlideRecord('alm_hardware');
gr.get('assigned_to', ast.assigned_to);
}
var users_query = 'active=true^cat_item=be1dae433768ef793d35616043990e88^ORcat_item=1610da8b37a4ef793d35616043990e2f';
var user = new GlideRecord('sc_req_item');
user.addEncodedQuery(users_query);
user.addQuery('u_requested_for', gr.assigned_to);
gr.query();
while(gr.next())
{
gs.log('The asset: '+ gr.variable_pool.asset.getDisplayValue() + ' ' + gr.number + ' ' + gr.u_requested_for.getDisplayValue() );
gs.log('Requests: ' + gr.getRowCount());
}
Thanks,
L
Solved! Go to Solution.
- Labels:
-
Scripting and Coding

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-04-2019 08:11 AM
Hi,
Looks like your loop is terminating too soon and you swapped the "gr" query with the "user" query. Try the following the extra alm_hardware query was removed:
var asset_query = "*****";
var ast = new GlideAggregate('alm_hardware');
ast.addEncodedQuery(asset_query);
ast.groupBy('assigned_to');
ast.query();
while(ast.next()){
var users_query = 'active=true^cat_item=be1dae433768ef793d35616043990e88^ORcat_item=1610da8b37a4ef793d35616043990e2f';
var item = new GlideRecord('sc_req_item');
item.addEncodedQuery(users_query);
item.addQuery('u_requested_for', ast.assigned_to);
item.query();
while(item.next()){
gs.log('The asset: '+ item.variables.asset.getDisplayValue() + ' ' + item.number + ' ' + item.u_requested_for.getDisplayValue() );
gs.log('Requests: ' + item.getRowCount());
}
}

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-05-2019 08:05 AM
Try this instead:
var asset_query = "*****";
var ast = new GlideAggregate('alm_hardware');
ast.addEncodedQuery(asset_query);
ast.groupBy('assigned_to');
ast.query();
while(ast.next()){
var users_query = 'active=true^cat_item=be1dae433768ef793d35616043990e88^ORcat_item=1610da8b37a4ef793d35616043990e2f';
var item = new GlideRecord('sc_req_item');
item.addEncodedQuery(users_query);
item.addQuery('u_requested_for', ast.assigned_to);
item.query();
if(!item.hasNext()){
gs.eventQueue(****);
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-14-2019 02:05 PM
Thanks for your assistance but I still cant get it to work.
if(!item.hasNext()){
gs.eventQueue(****);
}
This part of the script is still triggering the event that fires the emails. Users who have an open active request item shouldn’t be receiving the email.
Thanks,
L

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-14-2019 02:39 PM
That tells me the following script is not returning results when you are expecting it to i.e. when it finds no open RITM's for the specified user:
var users_query = 'active=true^cat_item=be1dae433768ef793d35616043990e88^ORcat_item=1610da8b37a4ef793d35616043990e2f';
var item = new GlideRecord('sc_req_item');
item.addEncodedQuery(users_query);
item.addQuery('u_requested_for', ast.assigned_to);
item.query();
Are you able to run that script through a background script or Xplore and see what the output is? Try using a user which should have an active open RITM. From there you can dig down into what the underlying issue is.