← Home
🔥0 DAY
0 XP
Client Scripts · Step-by-step

Get a reference field value in a client script

Short answer

g_form.getValue('caller_id') gives you the sys_id. To read fields on the referenced record, use g_form.getReference('caller_id', callback) with a callback so the lookup is asynchronous, or — better for one or two fields — call a client-callable Script Include with GlideAjax. Never call getReference() without a callback: it blocks the browser on a synchronous request.

Where: System Definition → Client Scripts, plus System Definition → Script Includes for the GlideAjax counterpart

Steps

  1. 1.Start with the sys_id

    If all you need is the reference itself — to compare it, to pass it to a server call, or to check for empty — getValue() is enough and costs nothing.

    var callerId = g_form.getValue('caller_id');
    if (!callerId) {
      g_form.showFieldMsg('caller_id', 'Pick a caller first', 'error');
      return;
    }
  2. 2.Use getReference with a callback for several fields

    getReference() returns a GlideRecord-like object for the referenced record. Always pass the callback function as the second argument — that makes the request asynchronous. Reading properties off the return value directly forces a synchronous call and freezes the form.

    function onChange(control, oldValue, newValue, isLoading, isTemplate) {
      if (isLoading || newValue === '') return;
    
      g_form.getReference('caller_id', function (caller) {
        g_form.setValue('location', caller.location);
        g_form.setValue('u_caller_email', caller.email);
      });
    }
  3. 3.Prefer GlideAjax when you need one or two fields

    getReference() pulls the entire record across the wire. For a single field, a client-callable Script Include returns only what you asked for and is measurably faster on wide tables.

    // Script Include: CallerUtils (Client callable = true)
    var CallerUtils = Class.create();
    CallerUtils.prototype = Object.extendsObject(global.AbstractAjaxProcessor, {
      getEmail: function () {
        var gr = new GlideRecord('sys_user');
        if (gr.get(this.getParameter('sysparm_user'))) return gr.getValue('email');
        return '';
      },
      type: 'CallerUtils'
    });
  4. 4.Call the Script Include from the client script

    Instantiate GlideAjax with the Script Include name, add the method and parameters, then read the answer inside getXMLAnswer(). Everything that depends on the value must live inside that callback.

    var ga = new GlideAjax('CallerUtils');
    ga.addParam('sysparm_name', 'getEmail');
    ga.addParam('sysparm_user', g_form.getValue('caller_id'));
    ga.getXMLAnswer(function (email) {
      if (email) g_form.setValue('u_caller_email', email);
    });
  5. 5.Handle the empty and cleared cases

    An onChange script fires when the reference is cleared too, with newValue as an empty string. Return early on empty, and clear the dependent fields instead of leaving stale data on the form.

    if (newValue === '') {
      g_form.setValue('u_caller_email', '');
      return;
    }
  6. 6.Confirm with the browser network tab

    Change the field and watch the requests. One xmlhttp call per change is expected; a blocked UI or a long pending request means a synchronous getReference() slipped through.

Common mistakes

  • Calling var caller = g_form.getReference('caller_id') with no callback — synchronous AJAX that freezes the form.
  • Reading g_form.setValue() results outside the callback, where the value has not arrived yet.
  • Using getReference() to fetch one field from a wide table instead of a targeted GlideAjax call.
  • Forgetting Client callable = true on the Script Include, which makes every GlideAjax call return an empty answer.
  • Leaving stale dependent field values when the reference is cleared.

FAQ

Is g_form.getReference synchronous?

It is synchronous unless you pass a callback function as the second argument. Always pass the callback — a synchronous call blocks the browser until the server responds.

GlideAjax or getReference — which is faster?

GlideAjax, when you need one or two fields, because it returns only those values. getReference is convenient when you need many fields from the same referenced record.

Why is my GlideAjax answer empty?

Usual causes: the Script Include is not Client callable, it does not extend AbstractAjaxProcessor, the sysparm_name does not match the method name, or an ACL blocks the user from reading the field.