What is the difference Query or addQuery?

SS64
Tera Contributor

What is the difference Query or addQuery?

1 ACCEPTED SOLUTION

Namrata Khabale
Giga Guru

Hey SS,

Query: Runs the query against the table,Issue the query to the database to get relevant records.

In order to query a table, first create a ServiceNow object for the table. This object is called a GlideRecord.

query() is a method of class 'GlideRecord'.

 

addQuery: Adds a filter to return records where the field meets the specified condition

 Syntax: addQuery(String name, Object operator, Object value)  //(field, operator, value).

 

Mark correct and helpful if it resolves your issue!!!

 

Best Regards,

Namrata.

View solution in original post

10 REPLIES 10

Priyanka Chandr
Mega Guru

Hi,

Query: Runs the query against the table based on the specified filters by addQuery and addEncodedQuery. This will query the GlideRecord table as well as any references of the table. it issues the query to the database to get relevant records.

eg:

var gr= new GlideRecord('incident');

 gr.addQuery('priority', 1);

   gr.query();     // Issue the query to the database to get relevant records

     while (gr.next()) {

 

    // add code here

 

  }

 

addQuery()

The addQuery() method of the GlideRecord class can be called in three different ways, depending on whether one, two, or three arguments are supplied to it. If one argument is passed into the addQuery() method, then it'll assume that the argument is an encoded query. Encoded queries are a single string that represents all of the conditions in a single query (or even multiple queries)!

 

Please mark it correct and helpful.

Thanks,

Dhananjay Pawar
Kilo Sage

Hi,

Lets understand with example.

1)Query()

var gr=new GlideRecord('incident');

gr.query();

while(gr.next()){

gs.addInfoMessage(gr.number);

}

If you check above example, I have used only query() so it will give you all records of table.

 

2)addQuery()

 

var gr=new GlideRecord('incident');

gr.addQuery('state','closed');

gr.query();

while(gr.next()){

gs.addInfoMessage(gr.number);

}

 

If you check above example, it will give those incident records who is having closed state only.

So that means you can add more appropriate filters to fetch data using addQuery().

Thanks,

Dhananjay. 

Namrata Khabale
Giga Guru

Hey SS,

Query: Runs the query against the table,Issue the query to the database to get relevant records.

In order to query a table, first create a ServiceNow object for the table. This object is called a GlideRecord.

query() is a method of class 'GlideRecord'.

 

addQuery: Adds a filter to return records where the field meets the specified condition

 Syntax: addQuery(String name, Object operator, Object value)  //(field, operator, value).

 

Mark correct and helpful if it resolves your issue!!!

 

Best Regards,

Namrata.

Hi Namrata,

I read your answer, I want to ask, how addQuery() will work if won't use Object operartor?

Syntax: addQuery(String name, Object operator, Object value)

 

Thanks

AMIT SHISHODIA2
Giga Guru

gr.Query() is used to query all data.
gr.Addquery() is used to query some selected data(It's like that we are applying a condition).

We must use gr.Query() after gr.addQuery() to make it work.

Please mark this correct & helpful if it answered your question.