ServiceNow g_form cheat sheet g_form controls the form in client scripts, UI policies and client UI actions: read and set fields, show messages, and make fields mandatory, read-only or hidden.
All APIs, searchable GlideRecord GlideAggregate GlideSystem GlideDateTime Business rules GlideAjax Client scripts, g_user & g_scratchpad Client 13 methods · examples checked with the Script Checker · reviewed October 2026
Returns the field value as a string (a sys_id for reference fields).
Copy function onLoad () {
var category = g_form.getValue ('category' );
} Always a string: use getIntValue() or getDecimalValue() for numbers.
g_form.getIntValue(field) / getDecimalValue(field) # Returns the field as a number, ready for comparisons.
Copy function onLoad () {
var quantity = g_form.getIntValue ('u_quantity' );
if (quantity > 10 ) {
g_form.showFieldMsg ('u_quantity' , 'Needs approval' , 'info' );
}
} g_form.setValue(field, value, displayValue) # Sets a field. For reference fields, pass the display value as well to avoid an extra server call.
Copy function onLoad () {
g_form.setValue ('urgency' , 2 );
g_form.setValue ('assigned_to' , g_user.userID, g_user.getFullName ());
} g_form.clearValue(field) # Empties a field.
Copy function onLoad () {
g_form.clearValue ('u_resolution_code' );
} g_form.setMandatory(field, true|false) # Makes a field mandatory, or not.
Copy function onLoad () {
var isMajor = g_form.getValue ('priority' ) == '1' ;
g_form.setMandatory ('u_business_impact' , isMajor);
} A mandatory empty field cannot be hidden: call setMandatory(field, false) first.
g_form.setReadOnly(field, true|false) # Makes a field read-only, or editable.
Copy function onLoad () {
if (!g_form.isNewRecord ()) {
g_form.setReadOnly ('caller_id' , true );
}
} Read-only in the browser is not security; protect the field with an ACL too.
g_form.setDisplay(field, true|false) / setVisible() # Shows or hides a field. setDisplay() closes the gap; setVisible() leaves an empty space.
Copy function onLoad () {
var isHardware = g_form.getValue ('category' ) == 'hardware' ;
g_form.setDisplay ('u_asset_tag' , isHardware);
} g_form.setSectionDisplay(section, true|false) # Shows or hides a form section. The name is the section label in lowercase with underscores.
Copy function onLoad () {
var resolved = g_form.getValue ('state' ) == '6' ;
g_form.setSectionDisplay ('resolution_information' , resolved);
} g_form.addInfoMessage() / addErrorMessage() / clearMessages() # Shows a message at the top of the form.
Copy function onLoad () {
g_form.clearMessages ();
g_form.addInfoMessage ('This incident is linked to a major incident.' );
} g_form.showFieldMsg(field, message, 'info'|'error') / hideFieldMsg(field) # Shows a message under one field.
Copy function onLoad () {
g_form.hideFieldMsg ('short_description' );
g_form.showFieldMsg ('short_description' , 'Keep it under 80 characters' , 'error' );
} g_form.addOption() / removeOption() / clearOptions() # Changes the choices of a choice field.
Copy function onLoad () {
g_form.clearOptions ('u_size' );
g_form.addOption ('u_size' , '' , '-- None --' );
g_form.addOption ('u_size' , 'm' , 'Medium' );
g_form.addOption ('u_size' , 'l' , 'Large' );
} g_form.getReference(field, callback) # Loads the record behind a reference field and passes it to the callback.
Copy function onLoad () {
g_form.getReference ('caller_id' , function (caller) {
if (caller.vip == 'true' ) {
g_form.addInfoMessage ('VIP caller' );
}
});
} Always pass a callback. For one field, GlideAjax is lighter: getReference() returns the whole record.
g_form.getUniqueValue() / getTableName() / isNewRecord() # The record's sys_id, its table, and whether it has been saved yet.
Copy function onLoad () {
if (g_form.isNewRecord ()) {
g_form.setValue ('contact_type' , 'self-service' );
}
} Check your own script Paste a business rule, client script or script include and get every mistake by line, with the fix.
Open the Script Checker Frequently asked questions What is the difference between setDisplay() and setVisible()? setDisplay(field, false) hides the field and closes the gap. setVisible(field, false) hides it but leaves an empty space where it was.
Why can I not hide a mandatory field? ServiceNow keeps an empty mandatory field visible so the form can still be saved. Call g_form.setMandatory(field, false) before hiding it.