gs.addQuery('sys_id',current.caller_id)

shalinichiluka
Kilo Contributor

Can anyone explain me what does this query pull or mean in the below scripts mean?


Script :

var gr=new GlideRecord('sys_user');

gr.addQuery('sys_id', current.caller_id);

gr.query();

Q1) I dont understand how sys_id (Alpha-numerical value) is compared to caller_id(If i'm not wrong its the name of the caller on a table like incident).

I understand gr.addQuery('Active', true);

Pulls the records that are active, but querying with sys_id is very confusing.

1 ACCEPTED SOLUTION

Q1) The dictionary name of the caller field is caller_id?


Yes. The label says "Caller", but if you right click on the label and choose "Show caller_id" it will clearly indicate that the underlying database field name is "caller_id". Labels can differ, and often do when multiple languages are involved, from the database field name. No harm in that. Just the flexibility and power of the platform.



Q2) And it is a reference type field referring to user name?


Close. It is a reference field type referring the user table (which in the database is called sys_user).



But i guess ServiceNow recognizes, caller_id as reference to sys_id of the caller/user not the name of the caller/user.


Internally, the value of the caller_id field is a sys_id, a GUID as some system call it. It's a unique record identifier to a record in the sys_user table. This means that if you have five people with the same name (John Smith), they are all separate because they have different sys_ids on each record. All records in ServiceNow tables have unique sys_ids for each record.



Q3) The confusion is caller_id gives name of the user (string) or the sys_id of the user(alphanumeric)


When you use a sys_id to refer to a record in another table, the other table (in this case sys_user) has one field that is defined as the "display" field. In this case, the 'name' field is displayed for humans to read when you fill in the field value for "Caller" (caller_id). Because let's face it, few humans would be able to read a 32-character hex string and say "That's Shalini!". It's easier to read the value of the name field. In ServiceNow speak, you may hear this referred to as the "display value". It's one of the fields to show to humans instead of the sys_id. If you start seeing sys_ids in reference fields you know something has gone wrong.


Q4) Every record in all the tables have a unique ID which sys_id? If this is true, why look for matching sys_IDs?


Yes, that's true. All records have a unique sys_id associated with each record they contain as explained above. See my response below for the second part of your question.



For example:


var gr = new GlideRecord('sys_attachment');


gr.addQuery('table_sys_id', current.sys_id);


gr.query();



So if the attachment record with sys_id (table_sys_id) is a unique ID and current incident sys_ID is unique ID. Then why/how matching?


This might help... the sys_attachment table has a number of records. Each has their own sys_id. Unfortunately, you don't know the sys_id for the attachments that show up on your incident (or whatever.) Let's say you are looking at Incident INC001005 (sys_id 1A2B3C). You want to find incidents related to this record and know the table name (incident) and sys_id of the incident (1A2B3C), so you can find all attachments like this:



var gr = new GlideRecord('sys_attachment');


gr.addQuery('table_sys_id', current.sys_id);


gr.addQuery('table', current.getTableName());


gr.query();



That says "Find me all the attachments that are related to this record on and this table. It's a bit like saying "Find all the users in the user table with first name John and last name Smith."



I hope that helps.


View solution in original post

13 REPLIES 13

Chuck Tomasi
Tera Patron

The GlideRecord API turns your call in to something like this in SQL:



select * from sys_user where sys_id=(value from the caller_id field)



You are simply specifying the field name and value you want to filter on in the addQuery() call.



GlideRecord - ServiceNow Wiki


Pradeep Sharma
ServiceNow Employee
ServiceNow Employee

Hello Shalini,



To add, please refer below link for sample example.


https://www.servicenowguru.com/scripting/gliderecord-query-cheat-sheet/



Let me know if you have any questions.


guruprasad1
Kilo Guru

Hi Shalini,



var gr=new GlideRecord('sys_user');


gr.addQuery('sys_id', current.caller_id);


gr.query();



I believe this script was written for Incident table.



Above script will result one row from user Table, i.e., record of caller from User Table.


On Incident Table Caller field is a Reference type. So current.Caller_id will give Sys_id (Alphanumeric), current.caller_id.getDisplayValue() will result name of Caller (if Display value is set to true for Name column in User table).



If can also use above script like below



var gr=new GlideRecord('sys_user');


gr.addQuery('name', current.caller_id.getDisplayValue());


gr.query();



Note: addQuery with Name may result multiple records. Because, there may be many users with same name.



Regards


Guru


Pratiksha
Mega Sage
Mega Sage

Hi in simple way if you directly write the caller field name(which is a reference feild) it will show you the alphanumric value for the caller. so intend of that point to the primary key of the table which is sys_id which will do the "name" part for the addquery function and to get the value of the vargr we will do current.caller_id . check the screenshot i am trying to print the value of caller as well as callers manager.find_real_file.png