- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-07-2020 10:01 PM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-07-2020 10:49 PM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-07-2020 10:15 PM
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,

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-07-2020 10:32 PM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-07-2020 10:49 PM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎12-29-2021 04:13 AM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-07-2020 10:53 PM
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.