Built something you're proud of? Tell the story. A quick G2 review of App Engine or Build Agent helps other developers see what's possible on ServiceNow. Share your experience.

Dot walking through multiple tables in client script

Kyle Wiley
Mega 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

17 REPLIES 17

Hi,

I totally agree with @Göran Lundqvist . Avoid GlideRecord on client side. Go for GlideAjax call, and return a JSON with all the values you want.

  • Best Practice
  • Better Performance
  • Better User Experience

Regards,

Higa

Mani23
Tera Guru

Please use GlideAjax option as suggested by one of the experts.
Please check the below docs link

GlideAjax – Client

jon421553
Tera Contributor

You’re absolutely right—dot-walking through multiple tables in a client script can get tricky because it’s limited on the client side. The best approach is usually to use a GlideAjax call with a Script Include for deeper relationships, as it keeps logic server-side and avoids performance issues. Are you trying to pull data from more than two linked tables or just a single reference chain? with VipRide4U

jon421553
Tera Contributor

You’re absolutely right—dot-walking through multiple tables in a client script can get tricky because it’s limited on the client side. The best approach used by VipRide4U limousine is usually to use a GlideAjax call with a Script Include for deeper relationships, as it keeps logic server-side and avoids performance issues. Are you trying to pull data from more than two linked tables or just a single reference chain?

jon421553
Tera Contributor

Great discussion — you’ve definitely hit on a common trap with client-side scripting in ServiceNow. It’s important to know that true “multi-table dot walking” in a client script often doesn’t work as expected because you’re restricted by client-side access and performance with mesolyft .