How to Retrieve Caller Name in Fix Script (GlideRecord)
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Friday - last edited Friday
How to Retrieve Caller Name in Fix Script (GlideRecord)
Hi everyone,
I’m working with a Fix Script where I’m querying records from the incident table using GlideRecord.
I want to retrieve the caller’s name (display value) from the caller_id reference field, but I’m not getting the expected result.
What I Tried
var gr = new GlideRecord('incident');
gr.query();
if (gr.next()) {
gs.info('Caller Name: ' + gr.caller_id.name);
}Issue
The above approach is not working as expected in the Fix Script.
My Questions
What is the correct way to retrieve the caller name from
caller_idin a Fix Script?Should I use
getDisplayValue()orgetRefRecord()in this case?Are there any differences when accessing reference fields in Fix Scripts vs Business Rules?
Any guidance or best practices would be really helpful.
Thanks in advance!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Friday
It would be extremely helpful and beneficial to you to have shared what results you are getting, and what you are expecting. The code inherently works fine. You'll get the name of a Caller on a (random) incident record since you haven't specified any query criteria.
*** Script: Caller Name: Jim Ranzetti
[0:00:00.357] Total Time
Far more helpful would be to at least specify an incident number or sys_id, so that you know the record retrieved by the GlideRecord is the same one you're looking at for the test record. So likely your unexpected results are due to insufficient querying, with the GlideRecord returning an incident record that does not have a Caller.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Friday
Hello @themadhankumar ,
Although your code is right and working fine,
but if you are stuck or confused in which is the best practice so in short : the answer is "getDisplayValue()" because In most cases, you don't actually need to dot-walk to the name field on the User table. If you just want the string name (e.g., "Abel Tuter"), use getDisplayValue().
var gr = new GlideRecord('incident');
gr.query();
if (gr.next()) {
gs.info('Caller Name: ' + gr.getDisplayValue('caller_id'));
}
Also talking about "getRefRecord()",
Use getRefRecord() only if you need multiple fields from the User record (like their email, department, and manager). It is essentially a shorthand for performing a second GlideRecord query on the referenced table.
var gr = new GlideRecord('incident');
gr.query();
if (gr.next()) {
var userGR = gr.caller_id.getRefRecord();
if (userGR.isValidRecord()) {
gs.info('Name: ' + userGR.getValue('name'));
gs.info('Email: ' + userGR.getValue('email'));
}
}
If my response helped mark as helpful and accept the solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
yesterday
Answer:
1. It is safer to use getDisplayValue() to avoid errors if caller_id is empty
2. Usecases for getRefRecord(): If you need to query multiple fields from the sys_user table (like phone, email, and name),getRefRecord() is efficient because it fetches the user record once.
Usecases for getDisplayValue(): If you need specific reference field (like caller's name ) from the sys_user table ,Then dot-walking is the most efficient way to get the caller's name since a Fix Script operates directly on the record object.
3. Fix script in your usecase: To get the caller's name from an incident's caller_id (a reference field) in a Fix Script, you must query the Incident table, then access the dot-walked field or use getRefRecord()
Business Rule : No separate query is needed on table for dot walking.
- Use the current object.
- Dot-walking is highly efficient: current.caller_id.name directly retrieves the caller's name.
- Can use current.caller_id.getRefRecord() to get the entire GlideRecord object of the referenced user for further manipulation.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
yesterday
Hello ,
Your code is working fine . Please once check result in logs . If you want to retrieve all callers name then you need to use while(gr.next()) then all query records caller name will you get .
2) Mostly we use getDisplayValue() to fetch visible value instead of sys_id . By using displayValue you only fetch its displaValue and when you use getRefRecord() you can access whole record by using that reference field .Using getRefRecord() when you need more fields not only single field.
3) There is no any difference in acessing reference field in fixed script and business rule . But you need to run fixed script explicitly & Business rule will get triggered automatically on basis of crud operation(insert,update,delete,query)
Best practice while using getRefRecord() :
getRefRecord() in ServiceNow is used to retrieve a GlideRecord object from a reference field and is commonly used in server-side scripts like Business Rules and Fix Scripts. However, improper usage can lead to issues such as values not printing, “undefined” errors, or even unintended record updates. A key point to understand is that if the reference field is empty, getRefRecord() still returns an object, but it is not a valid record. Therefore, it is a best practice to always follow it with an isValidRecord() check before accessing or updating any fields. Additionally, developers should ensure that the reference field points to the correct table, especially when working with child tables, as referencing a parent table may result in missing fields and errors. Following these practices helps avoid bugs and ensures safe and reliable use of getRefRecord().
If this helps you then mark it as helpful and accept as solution.
Regards,
Aditya
