- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-13-2022 08:29 AM
Hello all,
I am trying to populate the dept code with an onChange client script, but it keeps returning 'undefined' when i attempt to dot walk to the id field on the department table. When i use getDisplayValue() the field does not return anything at all. Would greatly appreciate any help.
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
if (newValue){
var caller = g_form.getReference('name', popDept);
}
function popDept(caller){
g_form.setValue('department_code', caller.department_id);
}
}
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-13-2022 10:02 AM - edited 10-13-2022 10:04 AM
Hello,
So it is basically you have to dot walk two times, please use the below code:-
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
var caller = g_form.getReference('name');
var dept = caller.department;
if (dept) {
var grDept = new GlideRecord('cmn_department');
if (grDept.get(dept)) {
g_form.setValue('department_code', grDept.department_id);
}
}
}
Please mark my answer as correct based on Impact.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-13-2022 09:20 AM
Is the reference field you are trying to dot-walk through the correct field? Looking at your screenshots, it looks like you should be doing g_form.getReference('department', popDept) instead. This line is saying, "get the referenced record in this field for me to use" and I'm not seeing a 'name' field shown unless the label is "Department" and the underlying field name is "name".
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-13-2022 09:39 AM - edited 10-13-2022 09:43 AM
'name' is the underlying field name to the requested for reference field. The requested for field is the reference field to the sys_user table that contains the department name, should i not be doing it this way?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-13-2022 09:50 AM
Since that is the case, getReference() is currently retrieving the sys_user record which you would want to dot-walk to the department and further to the department ID (i.e. caller.department.department_id in your script.)
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-13-2022 09:56 AM
Attempted that change, but its still returning an 'undefined' value in the department code field.