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

Akhil Kumar1
Mega Sage

Caller is a field in your table and it's a reference field(reference to sys_user) table.



so current.caller_id will gives you the sys_id of the user in the caller field so if it's matches the code will execute and print the result.



As the shown script is a mail script running on some table(template is a variable of mail script).


So is 'caller' different from 'caller_id'?


Is 'caller_id' a alphanumeric?



Correct me if i'm wrong, so it looks for the records or record where the field sys_id of sys_user table matches with caller_id (alphanumeric)?


caller_id is a reference field type


so it looks for the records or record where the field sys_id of sys_user table matches with caller_id (reference)? PS: You are right.



Let me know if you have any questions.


Ok ...couple of tiny questions:


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


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



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



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


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



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?