Development · LessonBy Praveen T, ServiceNow Trainer, 9 yrs · Published · ServiceNow · all levels
g_form API
The client side form API: reading and setting values, mandatory and read only states, messages and the calls that quietly hurt performance.
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 spaceMessages 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
Practice challenge
+0 XPStreak ×0
Question 1 of 2
Which call attaches a message under one field?
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.