How To Use g_form.getReference() To Dot Walk In A Client Script

What is g_form.getReference(), Anyways? Need to get the value of a field on another table, but you’re in a client script? If you’ve got a reference field that points to that record where you need …

servicenow use g_form.getReference() in a client script to access fields on different tables, connected by reference field

What is g_form.getReference(), Anyways?

Need to get the value of a field on another table, but you’re in a client script?

If you’ve got a reference field that points to that record where you need the value from – then we’ve got you covered.

You can easily use one line of code, to access any field on a record, where you have a reference field available to access, by using:

g_form.getReference(referenceFieldName).otherFieldValue;

In ServiceNow, g_form.getReference() is a method of the client side g_form API that is used to get the display value of a reference field on a form. A reference field is a field that references another table in the ServiceNow database, and displays the display value of the referenced record. For example, the “Assigned To” field on an incident form might be a reference field that references the “sys_user” table, and displays the name of the user that the incident is assigned to.

The g_form.getReference() method allows a developer to retrieve the value of the referenced record for a specific reference field. This can be useful in situations where the developer needs a field value, that isn’t directly on the form – but instead – is referenced on that form.

To dot walk using g_form.getReference() in ServiceNow, you can use the . operator to access the field on the referenced record. Dot walking allows you to access fields on a referenced record, by specifying the field on the referenced record after the name of the reference field, separated by a . (dot).

So you’d do something like this, in a client script:

g_form.getReference('caller_id').email;

Need A Field, On A Non Referenced Table? Use GlideAjax

If you need to retrieve records from other tables, that are not directly referenced from the current record/form, then you’ll need to use GlideAjax to retrieve this data. GlideAjax is a little more complex regarding scripting it out, as you need to build a client script function and script include function to accomplish it.

To learn how to write a GlideAjax script, go here:

A g_form.getReference() Example

Here is an example of how you might use g_form.getReference() and dot walking to retrieve the value of a field on a reference field in a client script:

function onLoad() {
  // Get the value of the "Email" field on the referenced "sys_user"
// record for the "Caller ID" field
 var callerIdEmail = g_form.getReference('caller_id').email;
alert(callerIdEmail);
}

In this example, the onLoad() function is called when the form loads, and uses g_form.getReference() and dot walking to get the value of the “Email” field on the referenced “sys_user” record for the “Called ID” field. Maybe you need the users email address, or department on the client? The g_form.getReference() method is called with the reference field and the field on the referenced record separated by a . (dot). The callerIdEmail variable is then used in a calculation or other custom logic in the script.

Dot walking using g_form.getReference() can be useful in situations where you need to access fields on the referenced record in your script, but only have the reference field available.

It allows you to easily access the value of the field on the referenced record, without having to perform a separate GlideRecord query. This is the real value, and it only takes a line or 2 of code.

Note that you can use dot walking with g_form.getReference() to access fields on multiple levels of referenced records. For example, if the “Caller ID” field references the “sys_user” table, and the “Department” field on the “sys_user” table references the “cmn_department” table, you could use dot walking to access fields on the “cmn_department” record as well, by specifying the reference field and the field on the referenced record, separated by . (dots).

You can ultimately dot walk, via reference field, on the client as deeply as you’d like. We’ve never done more than 2 or 3 in the real world, so we’re not sure what performance impacts this would have, but you can try by testing it out.

Another example – Get A User’s Department In A Client Script

Let’s take a look at this example.

// Client Script
// Get the value of the "Department" field on the referenced "sys_user" record for the "Caller ID" field
var assignedToDepartment = g_form.getReference('caller_id').department;
alert(assignedToDepartment);

In this example, dot walking is used to access the value of the “Department” field on the “sys_user” record, which is a sys_id in this case. You can keep dot walking to get other values and fields.

You can take these examples and expand them to any use case you can think of.

You can then use this field value on the client to perform any operation with it.

Let us know below if you have any tips or tricks on how to better use g_form.getReference() in ServiceNow, we’d love to hear it.



What Do You Think About This Article?

5 1 vote
Article Rating
Subscribe
Notify of
guest
3 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Pat
Pat
6 months ago

Thanks for this!

Hrishabh Kumar
Hrishabh Kumar
6 months ago

I don’t think we can dot walk to multiple levels, dot walking only works upto 1 level.

g_form.getReference(‘caller_id’).department.name
//This code returns undefined

Hrishabh Kumar
Hrishabh Kumar
6 months ago

Please confirm the multiple level dot walking stuff.
Better if you can add a code snippet for that too.

3
0
Would love your thoughts, please comment.x
()
x