GlideRecord.get() and GlideRecord.addQuery() to get records.

ggg
Giga Guru
Scoped GlideRecord - addQuery(String name, Object value)  The first parameter is the table
field name.
Scoped GlideRecord - get(Object name, Object value)The first parameter is the Name of the
instantiated GlideRecord column to search for the specified value parameter. 
When I need to process records in a table I always use addQuery() to filter.
Is this Best Practice?
when should i use the get() method?
1 ACCEPTED SOLUTION

Yes you can use two parameters in the get method i.e get('sys_id', gs.getUserID()) but the get method will only return one record so if your result set will have more than one record user addQuery otherwise if you only need one record returned you can use get().

 

As I stated in my previous response use get() method when you only need a single record returned. That is when it would be the preferable method.

 

View solution in original post

6 REPLIES 6

DScroggins
Kilo Sage

Use get() method when you only need a single record returned and you know the sys_id. It is a bit quicker at retrieving the record from the database.

 

i.e.

var gr = new GlideRecord('sys_user');
if(gr.get(gs.getUserID())){

//Do something with returned glideRecord

gr.name = 'New Name'
gr.update();
}



 

vs

var gr = new GlideRecord('sys_user');
gr.addQuery('sys_id',gs.getUserID());
gr.query();
if(gr.next()){


//Do Something
gr.name = 'New Name';
gr.update();

}

that I know ... but you can also use get() with two parameters to get a result set as denoted above.

that is my question, which is preferable.

Yes you can use two parameters in the get method i.e get('sys_id', gs.getUserID()) but the get method will only return one record so if your result set will have more than one record user addQuery otherwise if you only need one record returned you can use get().

 

As I stated in my previous response use get() method when you only need a single record returned. That is when it would be the preferable method.

 

It is mentioned in the following docs that .get() can be used to retrieve mutiple records from a table.
link: https://developer.servicenow.com/dev.do#!/reference/api/tokyo/server/no-namespace/c_GlideRecordScope...

It mentions that "If multiple records are found, use next() to access the additional records."

So, in terms of performance, is get() method faster than addQuery/query when used to retrieve mutiple records?