Dot walking through multiple tables in client script

Kyle Wiley
Kilo Expert

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?  

1 ACCEPTED SOLUTION

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


View solution in original post

12 REPLIES 12

jamesjurden
Giga Guru

That is because department is a reference field on the user record, so you have to dot-walk further to ...department.name


I've tried that. It just returns undefined. Is that because the department field is a reference field as well?

 

If you just want to set the location field on the form then the sys_id should work fine the way you have it. The field should show the display value automatically.


drjohnchun
Tera Guru

Please try using a GlideRecord like below:



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



Hope this helps.



Please feel free to connect, follow, mark helpful / answer, like, endorse.


John Chun, PhD PMP see John's LinkedIn profile

visit snowaid


ServiceNow Advocate

Winner of November 2016 Members' Choice Award