this code is showing sys_id in servicenow, but when i put display value then only email is showing in info message, getDisplayValue is not working in client script. please correct the script.
function onLoad() {
//Type appropriate comment here, and begin script below
var caller = g_form.getReference('caller_id', doAlert);
code
function doAlert(caller) { //reference is passed into callback as first arguments
g_form.addInfoMessage('Caller Email is: ' + caller.email);
g_form.addInfoMessage('Caller Department is: ' + callerDepartment.getDisplayValue());
g_form.addInfoMessage('Caller Location is: ' + caller.location);
g_form.addInfoMessage('Caller Manager is: ' + caller.manager);
}
}
Assistant
User
first code not execute after email, only email is showing and in second it is showing undefined
Assistant
User
working but i need to do it via getreference only in client script
Assistant
In your client script, the main issue is that you are trying to call getDisplayValue() on variables (callerDepartment, caller.location, caller.manager) that are not defined or not retrieved correctly. The getReference function returns an object for the referenced record (e.g., caller), but if you want the display value of related fields like department, location, or manager, you must get their references separately.
In ServiceNow client scripts:
g_form.getReference(fieldName, callback) fetches the GlideRecord for the referenced field value.
To get display values for referenced fields within that record, you typically access the display value properties (which come from the server) or request them explicitly.
Unfortunately, getDisplayValue() cannot be called directly on client script objects returned by getReference, because those don't support server-side methods like getDisplayValue(). Instead, you can access properties on the returned object, such as display_value or use the display value field directly if it is present.
Corrected version of your code:
js
function onLoad() {
g_form.getReference('caller_id', function(caller) {
g_form.addInfoMessage('Caller Email is: ' + caller.email);
// For department, location, and manager, we need to get their display values from the 'caller' object
// Usually, the 'caller' object will have _display_value fields or related record properties
// 1. Caller Department
if (caller.department) {
g_form.getReference('caller_id.department', function(department) {
g_form.addInfoMessage('Caller Department is: ' + department.name); // assuming 'name' is the display value
});
} else {
g_form.addInfoMessage('Caller Department is: N/A');
}
// 2. Caller Location
if (caller.location) {
g_form.getReference('caller_id.location', function(location) {
g_form.addInfoMessage('Caller Location is: ' + location.name); // assuming 'name' is the display value
});
} else {
g_form.addInfoMessage('Caller Location is: N/A');
}
// 3. Caller Manager
if (caller.manager) {
g_form.getReference('caller_id.manager', function(manager) {
g_form.addInfoMessage('Caller Manager is: ' + manager.name); // assuming 'name' is the display value
});
} else {
g_form.addInfoMessage('Caller Manager is: N/A');
}
});
}
Explanation:
You first get a reference to caller_id to get the user record.
Then for each field (department, location, manager) which are themselves references, you call g_form.getReference() again, passing "caller_id.department", "caller_id.location", "caller_id.manager" so that ServiceNow knows to fetch those related records.
From those returned records, you can access fields such as name (which is typically the display value for these reference records).
Added checks to ensure no errors if those fields are empty.
Alternative (Simpler) Approach:
Sometimes, the initial caller object returned by g_form.getReference('caller_id', callback) already contains the display values as properties, usually with a _display_value suffix.
Check this by logging caller in console:
js
function onLoad() {
g_form.getReference('caller_id', function(caller) {
console.log(caller);
});
}
If you see properties like:
department (sys_id)
department_display_value
location (sys_id)
location_display_value
manager (sys_id)
manager_display_value
Then you can access them directly:
js
function onLoad() {
g_form.getReference('caller_id', function(caller) {
g_form.addInfoMessage('Caller Email is: ' + caller.email);
g_form.addInfoMessage('Caller Department is: ' + caller.department_display_value);
g_form.addInfoMessage('Caller Location is: ' + caller.location_display_value);
g_form.addInfoMessage('Caller Manager is: ' + caller.manager_display_value);
});
}
If these _display_value properties exist, this is the cleanest way.
Please check in your environment if caller.department_display_value etc exist and use accordingly.
Let me know if you want me to help modify your script further!
Thanks for the update! Means:
Your first approach is failing likely because the nested getReference calls on fields like 'caller_id.department' are incorrect usage — g_form.getReference() expects a field on the form, not a nested field.
The second approach is failing probably because the _display_value suffix fields aren't present on the client caller object returned from g_form.getReference('caller_id', fn).
Why g_form.getReference('caller_id.department', fn) does NOT work?
g_form.getReference expects the name of a field on the form, e.g. 'caller_id' or 'some_field'. It cannot resolve nested references like 'caller_id.department'.
Since caller is itself a GlideRecord object (but on the client side, a JS object with some properties), you need a different way to get display values of related fields like department, location, manager.
How to fix: Fetch related records inside the callback using new GlideAjax calls or do all info in one server call.
Option 1: Use a Script Include and GlideAjax to get all display values in one go
This is the most robust method:
Create a Script Include (Client Callable) that, given a caller_id sys_id, returns caller's email, and display values of department, location, manager.
Example Script Include:
js
var CallerInfo = Class.create();
CallerInfo.prototype = Object.extendsObject(AbstractAjaxProcessor, {
getCallerInfo: function() {
var callerId = this.getParameter('sysparm_caller_id');
var result = {};
if (!callerId)
return JSON.stringify(result);
var userGR = new GlideRecord('sys_user');
if (userGR.get(callerId)) {
result.email = userGR.email.toString();
result.department = userGR.getDisplayValue('department');
result.location = userGR.getDisplayValue('location');
result.manager = userGR.getDisplayValue('manager');
}
return JSON.stringify(result);
}
});
Make sure Client Callable checkbox is ON.
Client Script (onLoad):
js
function onLoad() {
var ga = new GlideAjax('CallerInfo');
ga.addParam('sysparm_name', 'getCallerInfo');
ga.addParam('sysparm_caller_id', g_form.getValue('caller_id'));
ga.getXMLAnswer(function(response){
var callerInfo = JSON.parse(response);
if (callerInfo.email)
g_form.addInfoMessage('Caller Email is: ' + callerInfo.email);
if (callerInfo.department)
g_form.addInfoMessage('Caller Department is: ' + callerInfo.department);
if (callerInfo.location)
g_form.addInfoMessage('Caller Location is: ' + callerInfo.location);
if (callerInfo.manager)
g_form.addInfoMessage('Caller Manager is: ' + callerInfo.manager);
});
}
Option 2: Client-only with multiple getReferences (working reliably)
You can do sequential getReference calls at the caller/user level to get user info, then for each referenced field like department, location, manager call separate getReference by sys_id.
But the problem is g_form.getReference only works on fields on the form.
If the caller_department is not a field on the form, g_form.getReference() won't help.
Alternative approach to get referenced record info on client (without Script Include):
Using GlideAjax with built-in Script Include _sys_user.has or sys_user.get is not available, so custom Script Include is best.
Summary:
Your usage of g_form.getReference('caller_id.department', ...) does not work.
getDisplayValue() only exists on server GlideRecord objects.
Client getReference(field, callback) only fetches the record pointed to by the field on the form.
To fetch info about related reference fields of the caller (department/location/manager), you must do separate server calls.
Best approach: Create a Script Include that returns all these in one call; call it from client using GlideAjax.
If you want, I can help you generate that Script Include and full client code, or I can help with another approach. Would you like that?
Thanks for the clarification — you want a client script-only solution using g_form.getReference()only (no GlideAjax, no Script Include).
Important points about g_form.getReference() on client:
g_form.getReference(field, callback) fetches the GlideRecord referenced by a field on the current form.
You cannot do g_form.getReference('caller_id.department', callback) — this won't work because 'caller_id.department' is not a field on the form.
However, you can:
First do g_form.getReference('caller_id', function(user){ ... }) to get the user record.
The user object will contain values of fields like department (sys_id), location (sys_id), manager (sys_id).
Then if those sys_id values exist, call g_form.getReference()for fields on the form that contain those sys_ids.
But usually, your form only has caller_id field, not fields like caller_id.department, or separate fields containing department sys_id — so you cannot call g_form.getReference() directly for department, location, or manager unless those fields exist on your form.
So: To continue only with g_form.getReference() on the client, your form must contain fields for:
caller_id (already present)
department (sys_id of caller's department)
location (sys_id of caller's location)
manager (sys_id of caller's manager)
A way to do this with your current form only having caller_id:
When caller.department contains the sys_id of the department, then **you cannot g_form.getReference('caller_id.department',...), but you can create temporary GlideRecord for department record by sys_id on the client — wait, you cannot create GlideRecord client-side.
So you cannot fetch department details from the client alone without those fields on your form.
So the only way to do this truly client-only with g_form.getReference() is:
Add additional hidden fields on your form:
caller_department (reference to cmn_department)
caller_location (reference to cmn_location)
caller_manager (reference to sys_user)
Then when the form loads or caller_id changes, write a client script like:
js
function onLoad() {
g_form.getReference('caller_id', function(caller) {
g_form.setValue('caller_department', caller.department);
g_form.setValue('caller_location', caller.location);
g_form.setValue('caller_manager', caller.manager);
g_form.addInfoMessage('Caller Email is: ' + caller.email);
// Now get department info
g_form.getReference('caller_department', function(dept) {
g_form.addInfoMessage('Caller Department is: ' + dept.name);
});
// Location info
g_form.getReference('caller_location', function(loc) {
g_form.addInfoMessage('Caller Location is: ' + loc.name);
});
// Manager info
g_form.getReference('caller_manager', function(mgr) {
g_form.addInfoMessage('Caller Manager is: ' + mgr.name);
});
});
}
Explanation:
g_form.getReference('caller_id', ...) gets the user record.
you put those department, location, manager sys_ids into fields on your form (hidden fields e.g.)
then you can call g_form.getReference() on those fields to get display values, because those fields exist on the form.
finally display those display values.
If you cannot add those fields onto the form, then this method will not work.
Summary:
What you want
Possible with g_form.getReference() client only?
Get caller email
Yes (direct from caller record)
Get display value of caller's fields
Only if those fields are on the form
Call g_form.getReference() on nested references
No
Example with new hidden fields:
Let's say fields on form:
Field name
Type
Notes
caller_id
Reference (sys_user)
main caller field
caller_department
Reference (cmn_department)
hidden, for department sys_id
caller_location
Reference (cmn_location)
hidden, for location sys_id
caller_manager
Reference (sys_user)
hidden, for manager sys_id
Full working script (Client Script - onLoad):
js
function onLoad() {
g_form.getReference('caller_id', function(caller) {
if (!caller) return;
g_form.addInfoMessage('Caller Email is: ' + caller.email);
// Set hidden fields with caller's referenced sys_ids
g_form.setValue('caller_department', caller.department);
g_form.setValue('caller_location', caller.location);
g_form.setValue('caller_manager', caller.manager);
// Now get department display value
g_form.getReference('caller_department', function(dept) {
if (dept)
g_form.addInfoMessage('Caller Department is: ' + dept.name);
else
g_form.addInfoMessage('Caller Department is: N/A');
});
// Location display value
g_form.getReference('caller_location', function(loc) {
if (loc)
g_form.addInfoMessage('Caller Location is: ' + loc.name);
else
g_form.addInfoMessage('Caller Location is: N/A');
});
// Manager display value
g_form.getReference('caller_manager', function(mgr) {
if (mgr)
g_form.addInfoMessage('Caller Manager is: ' + mgr.name);
else
g_form.addInfoMessage('Caller Manager is: N/A');
});
});
}
Important:
Make sure the hidden fields (caller_department, etc.) exist on the form and are reference fields pointing to the correct tables.
Make sure your users have permission to read those referenced records.
Adjust the display field (dept.name) if your department table uses a different "display" field.
This pattern avoids using extra Script Includes or GlideAjax.
If you'd like, I can help write the dictionary changes and client scripts to do this!
TL;DR:
You cannot get display values of related records by dynamically calling g_form.getReference() on nested fields, unless the referenced sys_ids are in actual reference fields on the form. So add hidden reference fields for department, location, and manager to the form, set those fields to the caller's department, location, and manager sys_ids, then use g_form.getReference() on these fields.
Let me know if you want help adding those fields or making the script!