
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-31-2016 06:26 AM
I am trying to dot walk through multiple tables in an onChange client script on the Incident table. When the Caller field on the INC form changes, I want to reference the sys_user table to see what department the Caller is in. The field that I am trying to use on the Department table is u_department_group.
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
var caller = g_form.getReference('caller_id');
var dept = g_form.getReference('caller_id').department;
if (newValue) {
confirm(caller.name);
confirm(dept);
}
}
caller.name prints out the user's full name as it displays in the sys_user table but when I try to print out dept, the only value I get is the sys_id. I cannot get the department name to display or any other field on the Department table to display anything.
Is this possible in a client script?
Solved! Go to Solution.
- Labels:
-
Workflow
- 6,482 Views
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-31-2016 07:51 AM
Here's the same script but using your custom field cmn_department.u_department_group:
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}var caller = g_form.getReference('caller_id');
var dept = caller.department;
if (dept) {
var grDept = new GlideRecord('cmn_department');
if (grDept.get(dept)) {
confirm(caller.name + ' works at ' + grDept.u_department_group);
}
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-31-2016 03:18 PM
One way to improve .getReference() is to make it asynchronous by providing a callback function as the second parameter as below, modified from the "answer":
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}var caller = g_form.getReference('caller_id', getDept);
function getDept(caller) {
var dept = caller.department;
if (dept) {
var grDept = new GlideRecord('cmn_department');
if (grDept.get(dept)) {
confirm(caller.name + ' works at ' + grDept.u_department_group);
}
}
}
}
Here, the getDept() function gets executed only when the server returns the caller reference; in the meantime, the browser continues doing other things. However, this will make yet another subsequent call to the server inside the getDept() function to get cmn_department.u_department_group via GlideRecord. All these can be done by making a single GlideAjax call to the server with some added scripts both on the client and server side.
Please see Client Script Best Practices - ServiceNow Wiki: Minimize Server Lookups for more details.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-07-2020 07:07 AM
Hi,
I totally agree with
- Best Practice
- Better Performance
- Better User Experience
Regards,
Higa
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-07-2025 07:38 AM
Please use GlideAjax option as suggested by one of the experts.
Please check the below docs link