- 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:08 PM
Hi,
Refer below link:
Basically, when you don't have direct access to some table or its data in that case you use GlideRecord to make a bridge and to access both record of some table and fields.
First thing is to access the required table and the method is
var gr = new GlideRecord("table_name");
So now we have complete table access in "gr" object. You can access table records, fields almost everything.
next step is to filter the records, most probably you don't want millions of record from the table you might need only one specific record. So now the 2nd part is to apply a filter. This filter will be basically a pair of field name and its value i.e Number = 1234, Name = Andrei, sys_id = er864396623462544wer
gr.addQuery("name", 'Andrei');
in the above line I am saying "give me only those records from the table where name field has value Andei". Now that I set the filter next step is to execute that filter so that we can get what we required.
gr.query();
query method will execute the filter and return me only those record that fulfilled the filter criteria i.e name = Andrei
Last step will be to iterate over all the data and implement business logic. Let say I want all the records that table returned will have name changed from Andrei to Sharjeel. So the script will be like below
while(gr.next()){
gr.setValue('name','Sharjeel');
gr.update();
}
gr.update - force to update the record with new value we set. i.e Sharjeel as the name.
=========================================================
Current object
current is GlideRecord object just like gr in the above example above. The only different is gr has access to All the records on the GlideRecord table but current only have access to the record on which you triggered your script. Most commonly we use current in business rules. As business rule typically run on insertion, update, delete, query of record. So, in that case current will represent the record on which that Business rule executed. It can access everything related to that particular record.
=========================================================
Dynamic Queries (how to compare sys_id)
For the sake of example, you have Business rule written on incident table. Now on incident table you have Caller and Department field. Now I want when user insert the record Department should be filled in by Caller's Department that is stored on the User table in the Department field. Our query will be like below.
I know reference field holds the sys_id of record. Caller is reference field
Caller - Reference field, referenced from sys_user table having sys_id of user selected in the field.
if I do current.caller_id it will return me sys_id of user.
Now my query will be like below.
var gr = new GlideRecord('sys_user'); // I need record from user table.
// I need record but wait I don't need all the records. Please return me only 1 record that has sys_id of what is stored in the caller_id field.
gr.addQuery('sys_id', current.caller_id);
gr.query();
if(gr.next()){
// business logic here.
}
You observed, in the above example I got user's sys_id from caller_id field and that sys_id exist in the sys_user table so I GlideRecord that and then I apply filter to match sys_id field on the sys_user table and the value I got from caller id.
Likewise you can compare any value, you just need to know which value you have and on the target table which field you want to compare with. That's all!
I hope this helps. Happy Learning 🙂
Please mark this correct & helpful if it answered your question.
Regards,
Vishakha
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-27-2021 06:15 PM

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-16-2023 05:13 AM
This was the most simple, concise and clear explanation that I've ever read! Thanks a lot! 😁

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-02-2024 01:07 PM
Agreed! I've struggled with the GlideRecord concept and you made it so easy to understand, something that I've not been able to accomplish through other resources. Thank you!