The g_form.setValue() method is how you programmatically change field values in the browser without triggering a server round-trip. The critical gotcha most developers miss: reference fields require both the sys_id as the value AND the display value as a second parameter β otherwise the field shows the GUID to users. Choice fields need the actual choice value (like '2' for state), not the display label. This is different from server-side scripting where you can often be more flexible with value types.
When to use this
- In onChange client scripts when one field change should automatically populate other fields
- In onLoad client scripts to set default values based on user roles or URL parameters
- In UI Action client scripts before form submission to populate hidden calculation fields
- When you need immediate visual feedback without waiting for server processing
When NOT to use this
- Don't use in onSubmit client scripts for validation β the values won't be saved to the database, use Business Rules instead
- Don't use to set values that require server-side data lookups β use GlideAjax to fetch the data first
- Don't use for fields that should trigger their own onChange events if you want those events to fire β use
g_form.setValue()with the third parametertrueinstead - Don't use on read-only fields or fields the current user doesn't have write access to β it will silently fail
Key behaviors and gotchas
- Reference fields require
sys_idas first parameter and display value as second:g_form.setValue('assigned_to', sysId, displayName) - Choice fields need the actual choice value (
'2'), not the display label ('In Progress') - Boolean fields accept both actual booleans and string values (
trueor'true') - Date fields expect YYYY-MM-DD format, datetime fields expect full ISO format or ServiceNow internal format
- Setting a field to empty string (
'') clears the field completely - The method bypasses field validation and onChange scripts by default β add
trueas third parameter to trigger onChange events - Values set in client scripts don't persist unless the form is saved β they're only visual changes in the browser
Never call setValue() in an onChange script for the same field that triggered the onChange β you'll create an infinite loop that crashes the browser tab.
Enjoying this? Get one deep-dive per week.
Join 1,000+ ServiceNow pros β scripts, GlideRecord patterns, Flow Designer techniques, and career moves. Free.