โ† Home
๐Ÿ”ฅ0 DAY
โšก0 XP
Client Scripts ยท Step-by-step

Dot-walk in a client script

Short answer

g_form.getValue('caller_id.department') returns nothing โ€” g_form only knows fields that are on the form, so there is no client-side dot-walk. Get the related value one of three ways: add the dot-walked field to the form (fastest, no code), g_form.getReference() with a callback for several fields on the referenced record, or GlideAjax to a client-callable Script Include for anything deeper than one hop.

Where: Form Designer / form layout for dot-walked fields; System Definition โ†’ Client Scripts and Script Includes for the scripted routes

Steps

  1. 1.Understand why the dot-walk fails

    The client only has the fields rendered on the form. A dotted string is treated as a field name, no such field exists, and g_form returns an empty string โ€” silently, with no error in the console.

    // Returns '' โ€” there is no such field on the form
    var dept = g_form.getValue('caller_id.department');
  2. 2.Try the no-code route first: put the field on the form

    In Form Designer, expand the reference field and drag the related field onto the layout. The platform loads it with the record, so g_form.getValue('caller_id.department') then works. Set it read-only, and hide it with a UI Policy if users should not see it.

    // After adding the dot-walked field to the form layout
    var dept = g_form.getValue('caller_id.department');
  3. 3.Use getReference for one hop with several fields

    For multiple fields on the directly referenced record, one asynchronous getReference() call is enough. The callback receives a record object whose properties you read directly.

    g_form.getReference('caller_id', function (caller) {
      g_form.setValue('location', caller.location);
      g_form.setValue('department', caller.department);
    });
  4. 4.Use GlideAjax for two or more hops

    getReference() only reaches the first record. Anything like caller_id.manager.email must be dot-walked on the server, where GlideRecord can walk as deep as you need, and returned as a plain value.

    // Script Include: CallerUtils (Client callable = true)
    getManagerEmail: function () {
      var gr = new GlideRecord('sys_user');
      if (!gr.get(this.getParameter('sysparm_user'))) return '';
      return gr.manager.email ? gr.manager.email.toString() : '';
    },
  5. 5.Return several values as JSON

    One round trip beats three. Build an object on the server, JSON.stringify it, and parse it in the callback.

    var ga = new GlideAjax('CallerUtils');
    ga.addParam('sysparm_name', 'getCallerProfile');
    ga.addParam('sysparm_user', g_form.getValue('caller_id'));
    ga.getXMLAnswer(function (json) {
      var p = JSON.parse(json || '{}');
      g_form.setValue('u_manager_email', p.managerEmail || '');
      g_form.setValue('department', p.department || '');
    });
  6. 6.Verify the value is not empty before you use it

    Every one of these paths can legitimately return an empty string โ€” no caller, no manager, no read access. Branch on empty and show a field message rather than writing blanks onto the form.

Common mistakes

  • Assuming a dotted field name works in g_form โ€” it returns an empty string with no error, which looks like a data problem.
  • Chaining two hops through getReference callbacks, which serialises two round trips instead of doing the walk on the server.
  • Dot-walking on the server without null-checking each hop; gr.manager.email throws nothing but yields an empty GlideElement when the manager is empty.
  • Adding dot-walked fields to a form on a high-volume table without setting them read-only, letting users edit the referenced record by accident.
  • Returning a GlideElement from a Script Include instead of calling toString() or getValue().

FAQ

Can you dot-walk with g_form in a client script?

No. g_form only exposes fields present on the form, so a dotted name returns an empty string. Add the dot-walked field to the form, or fetch the value with getReference or GlideAjax.

How do I get a field two levels deep, like caller_id.manager.email?

Do the dot-walk on the server in a client-callable Script Include and return the string through GlideAjax. getReference only reaches the first referenced record.

Does adding a dot-walked field to the form slow it down?

Marginally โ€” the platform joins the referenced table when loading the form. One or two fields are fine; a dozen on a wide table is where you switch to GlideAjax.