sys_id and current in addQuery in GlideRecord

Andrei16
Kilo Contributor

Hi folks,

So far I am self-learner in SNow and I still don't get it some part of GlideRecord query.

I assume that I can't understand deeply how to use sys_id and query on that and what is the current.

I can build a basic query on GlideRecord but if it comes to compare some values from different tables then I failed.

Could anyone explain it to me like I’m a six-year-old and provide some real use cases/scenarios?

I know that my question could be silly for some of you but I want to understand it.

1 ACCEPTED SOLUTION

MrMuhammad
Giga Sage

Hi Andrei,

 

Let me try to simplify it for you.

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.

Thanks & Regards,
Sharjeel

 

 

 

Regards,
Muhammad

View solution in original post

4 REPLIES 4

MrMuhammad
Giga Sage

Hi Andrei,

 

Let me try to simplify it for you.

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.

Thanks & Regards,
Sharjeel

 

 

 

Regards,
Muhammad

Thanks for clarifying!

Happy to Help!

 

if you question has been answered please be kind to mark my response as CORRECT so that this thread can be added to answered list and others can be benefited by this. Happy Learning 🙂

 

Thanks & Regards,

Sharjeel

Regards,
Muhammad

Vishakha
Mega Guru

Hi Andrei,

Refer docs for better understanding of GlideRecord, refer below link:

1) https://docs.servicenow.com/bundle/orlando-application-development/page/script/server-scripting/conc...

 

2) Using GlideRecord to query tables

 

Regards,

Vishakha