How to Query a Table Multiple Times?

Raj64
Tera Guru

Hello everyone,

If I need to run several queries on a table, can I use one GlideRecord object?

For example, if I wanted to run a query to find information in sys_user_group for two different records ... do I need two GlideRecord objects, or can I create one like below:

var myObj = new GlideRecord('sys_user_group');
var sys_id;

myObj.addQuery('name', 'GROUP1');
myObj.query();

while (myObj.next()) {
   sys_id = myObj.sys_id;
}

Now what if I wanted the sys_id of GROUP2?

Thanks!

1 ACCEPTED SOLUTION

Justin Loftas
Tera Guru

You can re-use the same GlideRecord variable to run your query.

var firstGroupId = null;
var secondGroupId = null;
var grGroup = new GlideRecord('sys_user_group');
//You can use .get() as the name on the sys_user_group table is unique
if (grGroup.get('name', 'GROUP1')){
  firstGroupId = grGroup.getUniqueValue();
} 
if (grGroup.get('name', 'GROUP2')){
  secondGroupId = grGroup.getUniqueValue();
} 

//Or you can use the .initialize() function to clear an existing query record set
grGroup.initialize();
grGroup.addQuery('name', 'GROUP1');
grGroup.query();
//Can use if rather than while as only one record should be returned
if (grGroup.next()){
   firstGroupId = grGroup.getUniqueValue();
}
grGroup.initialize();
grGroup.addQuery('name', 'GROUP2');
grGroup.query();
if (grGroup.next()){
   secondGroupId = grGroup.getUniqueValue();
}

 

Sometimes your use case does require you to use two different variables but this is another option. I would always use the API's getter and setter methods rather than dot notation to access/set properties.

View solution in original post

4 REPLIES 4

Filipe Cruz
Kilo Sage
Kilo Sage

Hi Raj,

You need to declare a new GlideRecord object, since your initial myObj object already contains the query "name = GROUP1". 
In order for you to properly use that object, you had to "erase" it.

Hope this makes sense to you.

 

Please, don't forget to mark my answer as correct if it solves your issue or mark it as helpful if it is relevant for you!

Best Regards,

Filipe Cruz

Raj64
Tera Guru

Thanks Filipe.

Chris D
Kilo Sage
Kilo Sage

Just to add an additional note, a GlideRecord isn't limited to one record. So another option for you would be to have one query that returns both records, something like:

var myObj = new GlideRecord('sys_user_group');
var sys_id = [];

myObj.addQuery('name', 'GROUP1').addOrCondition('name', 'GROUP2');
myObj.query();

while (myObj.next()) {
   sys_id.push() = myObj.sys_id;
}

 

Note that this example wouldn't help you decipher which is which so for the sake of simplicity, you could just do two GR queries, but down the line when you have an indefinite number of results, keep this in mind.

Justin Loftas
Tera Guru

You can re-use the same GlideRecord variable to run your query.

var firstGroupId = null;
var secondGroupId = null;
var grGroup = new GlideRecord('sys_user_group');
//You can use .get() as the name on the sys_user_group table is unique
if (grGroup.get('name', 'GROUP1')){
  firstGroupId = grGroup.getUniqueValue();
} 
if (grGroup.get('name', 'GROUP2')){
  secondGroupId = grGroup.getUniqueValue();
} 

//Or you can use the .initialize() function to clear an existing query record set
grGroup.initialize();
grGroup.addQuery('name', 'GROUP1');
grGroup.query();
//Can use if rather than while as only one record should be returned
if (grGroup.next()){
   firstGroupId = grGroup.getUniqueValue();
}
grGroup.initialize();
grGroup.addQuery('name', 'GROUP2');
grGroup.query();
if (grGroup.next()){
   secondGroupId = grGroup.getUniqueValue();
}

 

Sometimes your use case does require you to use two different variables but this is another option. I would always use the API's getter and setter methods rather than dot notation to access/set properties.