Find your people. Pick a challenge. Ship something real. The CreatorCon Hackathon is coming to the Community Pavilion for one epic night. Every skill level, every role welcome. Join us on May 5th and learn more here.

How to Retrieve Caller Name in Fix Script (GlideRecord)

themadhankumar
Tera Contributor

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

  1. What is the correct way to retrieve the caller name from caller_id in a Fix Script?

  2. Should I use getDisplayValue() or getRefRecord() in this case?

  3. 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!

2 ACCEPTED SOLUTIONS

yashkamde
Mega Sage

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.

View solution in original post

Aditya_hublikar
Mega Sage

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

View solution in original post

5 REPLIES 5

Hello @themadhankumar ,

 

 I hope you are doing well . Does your query is resolved ?

 

If my response helps you then mark it as helpful and accept as solution .

Regards,

Aditya,

Technical Consultant