- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-24-2023 11:25 AM
I have a reference field on the cmn_department table in a Catalog Client Script. when someone changes the department, I want to populate a field with the name of the department head. It does not look like my dot walking is working.
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
var dept = g_form.getReference('change_supervisory_organization_department_id', populateOtherFields);
function populateOtherFields(dept) {
alert(JSON.stringify(dept.dept_head.name));
}
//Type appropriate comment here, and begin script below
}
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-24-2023 04:38 PM
@Harold Felder Using get reference function you can only dot walk one level not further.
The below line is not valid as you dot walking twice.
alert(JSON.stringify(dept.dept_head.name));
Change the line as below to get the sys id of department head
alert(dept.dept_head);
So create the field department_head on catalog form which refers to user table and use below line to populate it.
g_form.setValue('<DEPARTMENT HEAD FIELD NAME HERE>',dept.dept_head);
this should solve your issue.
Please mark as correct answer if this solves your issue.
ServiceNow Community Rising Star, Class of 2023
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-25-2023 02:19 PM
That's good, you can use the sys_id to get the dept head name with a script include and glide ajax or Efficient Glide Record.
I'm not sure if dept.getDisplayValue('dept_head') will work but worth a try...
The best way though would be to make the field "change_manager_department_owner" a reference (or lookup select) to the user table and set the value to the sys_id with dept.dept_head.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-24-2023 03:36 PM
Doesn't getReference return a GlideRecord? Try removing JSON.stringify.
Instead of having an extra field to try and keep in sync why not show the referenced field on the form?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-25-2023 05:47 AM
When I try to populate the field with dept.dept_head I get the sys_id. When I try using dept.dept_head.name I get undefined.
var dept = g_form.getReference('change_supervisory_organization_department_id', populateOtherFields);
if (newValue == '') {
g_form.setValue('change_manager_department_owner', '');
}
function populateOtherFields(dept) {
// alert(JSON.stringify(dept.dept_head.name));
g_form.setValue('change_manager_department_owner', dept.dept_head);
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-25-2023 02:19 PM
That's good, you can use the sys_id to get the dept head name with a script include and glide ajax or Efficient Glide Record.
I'm not sure if dept.getDisplayValue('dept_head') will work but worth a try...
The best way though would be to make the field "change_manager_department_owner" a reference (or lookup select) to the user table and set the value to the sys_id with dept.dept_head.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-24-2023 04:38 PM
@Harold Felder Using get reference function you can only dot walk one level not further.
The below line is not valid as you dot walking twice.
alert(JSON.stringify(dept.dept_head.name));
Change the line as below to get the sys id of department head
alert(dept.dept_head);
So create the field department_head on catalog form which refers to user table and use below line to populate it.
g_form.setValue('<DEPARTMENT HEAD FIELD NAME HERE>',dept.dept_head);
this should solve your issue.
Please mark as correct answer if this solves your issue.
ServiceNow Community Rising Star, Class of 2023