in servicenow, this code is working in client script, but showing sysid and if i use getDisplayValue() it was not working
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
code
var caller = g_form.getReference('caller_id', doAlert);
function doAlert(caller) { //reference is passed into callback as first arguments
alert('Caller Manager is: ' + caller.manager);
}
}
Assistant
User
no popup
Assistant
In ServiceNow client scripts, when you use g_form.getReference(), it returns a GlideRecord-like JavaScript object representing the referenced record. However, the fields you get may not be the "display values" in the way you expect, especially for reference fields like caller.manager.
Why are you seeing sys_id instead of the display value?
caller.manager might be a reference field itself. So when you access caller.manager, you get the sys_id of the manager, not the display value.
getDisplayValue() is a GlideRecord server-side method—it does not work in client scripts.
How to get the display value of a referenced field on the client side?
You have 2 main ways:
1. Access the display value returned in the callback (if returned)
Sometimes, when you use g_form.getReference(), the object returned already contains some display values for referenced fields, but not always for nested reference fields.
If caller.manager is a reference field, then caller.manager will be a sys_id string. To get the manager's display value, you need to fetch that record as well.
2. Use g_form.getReference() recursively
Example:
javascript
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
g_form.getReference('caller_id', function(caller) {
// caller.manager is the sys_id of the manager
if (caller.manager) {
g_form.getReference('caller.manager', function(manager) {
alert('Caller Manager is: ' + manager.getDisplayValue());
// OR use manager.name (the display field)
});
} else {
alert('Caller has no manager');
}
});
}
Here, you call g_form.getReference twice: first to get the caller record, then to get the manager record, and finally display the name.
Notes:
getDisplayValue()is a server-side GlideRecord method, NOT client-side on these callback objects. Usually, the returned JS object contains display values as plain properties, e.g., manager.name may give the name of the manager.
So in the manager callback, try:
javascript
alert('Caller Manager is: ' + manager.name);
or whatever the display field is.
Summary
g_form.getReference returns an object with fields from the referenced record.
If a field is a reference itself, you need to call g_form.getReference again on that field to get its display data.
No getDisplayValue on client scripts; instead, use the properties available on the returned object.
Let me know if you want me to help with making this asynchronous code cleaner or if you want to use a GlideAjax approach!
If you don't see any popup (alert) when running the code, some possibilities are:
Your onChange is not firing properly (maybe the field name or conditions are off)
The callback function is never called (maybe g_form.getReference is failing)
Or the alert is hidden by the browser (rare but possible)
Let's try a minimal, robust test script. Follow this example carefully:
javascript
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
g_form.getReference('caller_id', function(caller) {
if (!caller) {
alert('Caller not found.');
return;
}
if (caller.manager) {
g_form.getReference('caller.manager', function(manager) {
if (!manager) {
alert('Manager record not found.');
return;
}
// try manager.display_value or a common display field like 'name'
var managerName = manager.name || manager.getDisplayValue || manager.display_value;
alert('Caller Manager is: ' + managerName);
});
} else {
alert('Caller has no manager.');
}
});
}
Important checks:
Is this onChange script running on the field caller_id?
The first argument to g_form.getReference is 'caller_id' so your field must be named exactly caller_id. Otherwise, change that string to your referenced field's name.
Test alert right at the start
Add this to be sure the script triggers at all:
javascript
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
alert('onChange triggered. New value: ' + newValue);
if (isLoading || newValue === '') {
return;
}
// ...
}
If you get this alert but not the others, then the problem is in the async callback.
Alternative: Use console.log instead of alert
Sometimes alert boxes can be blocked by browsers. Instead try console.log() and open browser Developer Tools Console (F12).
Example:
javascript
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
console.log('onChange triggered. New value: ' + newValue);
if (isLoading || newValue === '') {
return;
}
g_form.getReference('caller_id', function(caller) {
if (!caller) {
console.log('Caller not found.');
return;
}
if (caller.manager) {
g_form.getReference('caller.manager', function(manager) {
if (!manager) {
console.log('Manager record not found.');
return;
}
console.log('Caller Manager is: ' + (manager.name || '(no name)'));
});
} else {
console.log('Caller has no manager.');
}
});
}
Summary checklist
Make sure script is onChange of the correct field (caller_id)
Confirm onChange fires using alert or console.log before async calls
Confirm the referenced records exist and fields are populated
Use nested g_form.getReference to get nested field display values
Avoid using getDisplayValue client-side (use property e.g. .name)
If you still get no popup or console output, double-check:
The field name your script is attached to
If client scripting is enabled and script is active
Browser console errors
If any UI policies or client scripts block the field or execution