Dot Walking on Catalog Client Script for Department Head

Harold Felder
Tera Contributor

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

}

2 ACCEPTED SOLUTIONS

jaheerhattiwale
Mega Sage
Mega Sage

@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.

Please mark the answer as correct or helpful based on impact
ServiceNow Community Rising Star, Class of 2023

View solution in original post

@Harold Felder,

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.

 

View solution in original post

4 REPLIES 4

ricker
Tera Guru

@Harold Felder,

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?

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);
}

@Harold Felder,

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.

 

jaheerhattiwale
Mega Sage
Mega Sage

@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.

Please mark the answer as correct or helpful based on impact
ServiceNow Community Rising Star, Class of 2023