"How Does getReference() Function Work in ServiceNow Client Scripts?"

saikumar2612
Tera Contributor

Hello Community,

I'm trying to understand how the getReference() function works in ServiceNow, especially when used in client scripts. I would appreciate any guidance or examples that can help clarify its functionality

 

  1. I'm working with a reference field (e.g., caller_id on the Incident form).
  2. My goal is to retrieve additional fields from the referenced record (e.g., email or department) when the reference field is populated.

What I've Tried:

I’ve implemented the getReference() function in an onChange client script, but I'm not entirely sure if I'm using it correctly or how to handle cases where the referenced record is not found.

2 ACCEPTED SOLUTIONS

Sid_Takali
Kilo Patron
Kilo Patron

Hi @saikumar2612  The getReference() method is used in client scripts to fetch a GlideRecord object from a reference field on a form. Reference fields are fields that point to records in other tables. The getReference()  method allows you to retrieve the referenced record and access its properties.

 

Have a look at this thread https://www.servicenow.com/community/developer-forum/what-is-use-of-getreference/m-p/1791072 

https://servicenowguru.com/client-scripts-scripting/gform-getreference-callback/  

View solution in original post

Amit Verma
Kilo Patron
Kilo Patron

Hi @saikumar2612 

 

Below link will help you to get a better understanding of getReference() :

https://www.servicenow.com/community/developer-articles/servicenow-introduction-to-getreference-serv...

 

Please note that using getReference is not considered a best practice, even with a callback function, as it retrieves the complete record while usually you only need one field or two to work with. The recommended best practice option is GlideAjax.

 

Thanks and Regards

Amit Verma


Please mark this response as correct and helpful if it assisted you with your question.

View solution in original post

4 REPLIES 4

Sid_Takali
Kilo Patron
Kilo Patron

Hi @saikumar2612  The getReference() method is used in client scripts to fetch a GlideRecord object from a reference field on a form. Reference fields are fields that point to records in other tables. The getReference()  method allows you to retrieve the referenced record and access its properties.

 

Have a look at this thread https://www.servicenow.com/community/developer-forum/what-is-use-of-getreference/m-p/1791072 

https://servicenowguru.com/client-scripts-scripting/gform-getreference-callback/  

Amit Verma
Kilo Patron
Kilo Patron

Hi @saikumar2612 

 

Below link will help you to get a better understanding of getReference() :

https://www.servicenow.com/community/developer-articles/servicenow-introduction-to-getreference-serv...

 

Please note that using getReference is not considered a best practice, even with a callback function, as it retrieves the complete record while usually you only need one field or two to work with. The recommended best practice option is GlideAjax.

 

Thanks and Regards

Amit Verma


Please mark this response as correct and helpful if it assisted you with your question.

Thank You

Mark Roethof
Tera Patron
Tera Patron

Hi there,

 

Why asking about getReference? This is a poor method. Go for superior methods, like GlideAjax with getXMLAnswer.

 

- 2020-01-10 - Article - Client Side Scripting: Go for GlideAjax (with getXMLAnswer)!

 

Kind regards,

 

Mark Roethof

Independent ServiceNow Consultant

10x ServiceNow MVP

---

 

~444 Articles, Blogs, Videos, Podcasts, Share projects - Experiences from the field

LinkedIn