validation of addEncodedQuery()

Community Alums
Not applicable

Hi ,

i had requirement in email inbound script.

i need to check whether my encoded query is valid or not 

var gr=new GlideRecord('sys_user');

gr=gr.addEncodedQuery(query);

gr.query();

 i need to check if this is not going to an error when query is not valid

when query is 'user=abel.tutor' there 'll not be generation of result

suggest me here how can i validate addEncodedQuery(query)..

5 REPLIES 5

Voona Rohila
Mega Patron
Mega Patron

Hi Akash

To verify if query has records,You can use hasNext() or next() with your gr object

 

Example:

 

var gr=new GlideRecord('sys_user');
gr=gr.addEncodedQuery(query);
gr.query();
while(gr.next())
{
// we can use gr to access the value of each record queried.
//do something
}

or

var gr=new GlideRecord('sys_user');
gr=gr.addEncodedQuery(query);
gr.query();
if(gr.hasNext()) //verifying if your gliderecord has any records. 
{
//do something
}

hasNext() : hasNext() method returns true if iterator have more elements. 

next() : next() method returns the next element and also moves to the next element.


Mark it helpful if this helps you to understand. Accept solution if this give you the answer you're looking for
Kind Regards,
Rohila V
2022-25 ServiceNow Community MVP

Community Alums
Not applicable

Hi Voona,

Sorry ,I'm not checking for this..after querying only you'll get to know if there's any records present in your answer,but what i need is before going to next step say gr.query() and so on..I need to check whatever i'm giving in "addEncodedQuery(query);" is valid or not,because if we give invalid query for addEncodedQuery(query); ,the next step wont be executed.

 

Shakeel Shaik
Giga Sage
Giga Sage

Hi @Akash 

 

If you want to check whether the query is valid or not, then better tot use GlideQuery instead of GlideRecord.

once check this blog - 

GLIDEQUERY PART 1

 

If my response is helpful, then Please mark as Correct Answer/Helpful.

Please check and let us know.

Thanks 🙂

Thanks,
Shakeel Shaik 🙂

Shakeel Shaik
Giga Sage
Giga Sage

Hi @Akash 

 

once check this link - 

 

ServiceNow Encoded Query validation

 

 

If my response is helpful, then Please mark as Correct Answer/Helpful.

Please check and let us know.

Thanks 🙂

Thanks,
Shakeel Shaik 🙂