Skip to content

Developer track

Client-side scripting

g_form, g_user, GlideAjax and friends.

2 sections · 15 entries

g_form quick reference

Works in client scripts and UI policies scripts on the classic form and in Service Portal.

Value (sys_id for references)
Set a value; pass the display value for references to avoid an extra server callHidden gem
Make a field mandatory
Make a field read-only
Hide a field and reclaim its space (setVisible leaves a gap)
Message under a field (info or error)
Banner at the top of the form
Rebuild a choice list on the fly
True on an unsaved record
The record’s sys_id
Fetch the referenced record. Always pass a callbackHidden gem
Role check on the client (UI only, never security)
Read values a display business rule put there

GlideAjax

The right way to get server data from a client script.

GlideAjax (client → server)

Asynchronous, so the form never freezes. The Script Include must be client callable.

// Script Include "UserInfoAjax" (client callable)
var UserInfoAjax = Class.create();
UserInfoAjax.prototype = Object.extendsObject(AbstractAjaxProcessor, {
  getEmail: function () {
    var user = new GlideRecord('sys_user');
    return user.get(this.getParameter('sysparm_user')) ? user.getValue('email') : '';
  },
  type: 'UserInfoAjax',
});

// Client script
var ga = new GlideAjax('UserInfoAjax');
ga.addParam('sysparm_name', 'getEmail');
ga.addParam('sysparm_user', g_form.getValue('caller_id'));
ga.getXMLAnswer(function (email) {
  g_form.setValue('u_contact_email', email);
});

Return several values as JSONHidden gem

One round trip instead of three.

// Script Include method
getUserCard: function () {
  var u = new GlideRecord('sys_user');
  if (!u.get(this.getParameter('sysparm_user'))) return '{}';
  return JSON.stringify({
    email: u.getValue('email'),
    phone: u.getValue('phone'),
    dept: u.getDisplayValue('department'),
  });
},

// Client script
ga.getXMLAnswer(function (answer) {
  var card = JSON.parse(answer || '{}');
  g_form.setValue('u_email', card.email || '');
});

Snippets are provided as-is under the MIT licence, without warranty. Test in a sub-production instance first. ServiceNow is a trademark of ServiceNow, Inc.; this page is independent and not affiliated with or endorsed by ServiceNow.