Skip to content
IT Canvass
Development · Lesson

g_form API

Quick answer

The client side form API: reading and setting values, mandatory and read only states, messages and the calls that quietly hurt performance.

Key takeaways

  • setValue on a reference field takes a display value to avoid a round trip
  • UI policies beat client scripts for simple visibility rules
  • GlideAjax, never client side GlideRecord
  • Clear field messages when the condition no longer applies

Core methods

g_form is available in client scripts, UI policies with scripts, UI actions marked client and catalog client scripts.

g_form.getValue('priority');
g_form.setValue('assignment_group', groupSysId, 'Network Support');
g_form.setMandatory('close_notes', true);
g_form.setReadOnly('category', true);
g_form.setDisplay('u_reason', false); // removes from layout
g_form.setVisible('u_reason', false); // hides but keeps space

Messages and validation

Use addErrorMessage for blocking problems, addInfoMessage for guidance, and showFieldMsg to attach a message to a specific field. Clear them with clearMessages and hideFieldMsg so old messages do not linger.

Performance habits

Client scripts run on every form load for every user.

  • Pass the third argument to setValue on reference fields to avoid a server lookup
  • Never call GlideRecord from a client script, use GlideAjax
  • Use g_scratchpad from a display business rule to send data with the form
  • Prefer UI policies to onChange scripts for simple show, hide and mandatory logic

Want to learn this properly?

Our live, instructor-led ServiceNow Training covers this hands-on, with real projects and a certification path.

Check your understanding

  1. Which call attaches a message under one field?

    • A. addInfoMessage
    • B. showFieldMsg
    • C. setLabel
    • D. addErrorMessage
    Show answer

    B. showFieldMsg

    showFieldMsg targets a single field.

  2. What should replace client side GlideRecord?

    • A. GlideAggregate
    • B. GlideAjax
    • C. GlideSystem
    • D. GlideForm
    Show answer

    B. GlideAjax

    GlideAjax calls a client callable script include asynchronously.

Frequently asked questions

What is the difference between setDisplay and setVisible?

setDisplay removes the field from the layout so the space closes up. setVisible hides it but leaves the gap.

Why does my onChange script slow the form?

It is probably making a synchronous server call. Move the lookup to GlideAjax with a callback, or send the data up front with g_scratchpad.
CallWhatsAppEnquire