Run two queries and do something based on whether either query returns a result

Andrew Bettcher
Kilo Sage

Hi,

I want to run two queries and then, if either query returns any result, do something. I can't post my code here but it would be something like this:

var gr1 = new GlideRecord('table1');
gr1.query();

var gr2 = new GlideRecord('table2');
gr2.query();

if (gr1 || gr2){
gs.info('Result found');
}

 

This will be run as a fix script and the table is huge so it's taking hours to run and, depending on what else is happening on the instance, may self cancel with the error message "Available memory is almost depleted". Therefore, I want to make sure that I've got it more or less right before running it.

 

Can anyone confirm if this is the right approach?

 

Also, is there a way for me to output from which table the result came from?

1 ACCEPTED SOLUTION

Thank you Andrew,

I think the script should work just fine. One small adjustment on this line :

if (mrvsCheck.hasNext || itemOptionCheck.hasNext) { //updated following advice from Peter Bodelier

You forgot the () after hasNext()

For testing purposes I would still add within every query a setLimit of say 1 or 10 as I did in my example script. This way you can test safely without worrying that your instance is going to stop 😉


Help others to find a correct solution by marking the appropriate response as accepted solution and helpful.

View solution in original post

8 REPLIES 8

Thank you Peter. I've posted a slightly sanitised version of my script in response to the another answer posted here if you're interested.

Thank you Andrew,

I think the script should work just fine. One small adjustment on this line :

if (mrvsCheck.hasNext || itemOptionCheck.hasNext) { //updated following advice from Peter Bodelier

You forgot the () after hasNext()

For testing purposes I would still add within every query a setLimit of say 1 or 10 as I did in my example script. This way you can test safely without worrying that your instance is going to stop 😉


Help others to find a correct solution by marking the appropriate response as accepted solution and helpful.

Thank you Peter. The missing () is a standard thing for me. Over the years its become one of the first things i check when scripts don't work. 

Of course, for a script like this that can take hours to run, it's useful to have a 2nd pair of eyes scan it.

Thank you again.

Saurabh Gupta
Kilo Patron
Kilo Patron

Hi @Andrew Bettcher 

It should be something like below-

var gr1 = new GlideRecord('table1');
gr1.query();
if (gr1.next())
{
gs.info('Result 1 found');
}
else
{
var gr2 = new GlideRecord('table2');
gr2.query();
if (gr2.next())
{
gs.info('Result 2 found');
}
}

Thanks and Regards,

Saurabh Gupta