How to Retrieve Field Values in ServiceNow Client Scripts Using g_form Methods
- nathanlee142
- Mar 19
- 2 min read
Updated: Mar 30

When creating client scripts in ServiceNow, accurately retrieving and setting field values is critical for ensuring seamless form interactions and user experiences. Developers commonly encounter situations where they need the actual value of a reference field, rather than its system ID (sys_id). This article explains how to correctly retrieve field values using various g_form methods and addresses common challenges developers face when dealing with reference fields and other specific data types.
Retrieving Field Values with g_form Methods
ServiceNow provides several helpful client-side methods through the g_form object for accessing form field values. However, understanding which method to use can sometimes be confusing. Let's clarify the usage of three effective methods:
1. Using g_form.getValue()
g_form.getValue('field_name') retrieves the actual stored value of the specified field. For reference fields, this typically returns the sys_id. This method is ideal when you need the sys_id for backend operations.
Example:
var userSysId = g_form.getValue('requested_by');
// returns sys_id of the selected user
2. Using g_form.getDisplayValue()
If you need the user-readable value (display value) rather than the sys_id, the method g_form.getDisplayValue('field_name') should be used.
Example:
var userName = g_form.getDisplayValue('requested_by');
// returns the user's full name instead of the sys_id
3. Using g_form.getReference()
The getReference() method asynchronously retrieves all field details of a referenced record, allowing access to specific fields within the referenced record.
Example:
function onLoad() {
g_form.getReference('requested_by', callback);
}
function callback(userRecord) {
var userName = userRecord.getValue('user_name');
alert(userName); // displays the user's username
}
This method is useful when you need more detailed data from a referenced record beyond just the display value.
Handling Special Field Types (Date/Time)
For fields like Date/Time, the straightforward method g_form.getValue() works effectively. However, remember:
g_form.getValue('u_date_field') directly fetches the stored date value.
Avoid using getDisplayBox() for non-reference fields as this method is primarily for reference fields to access their display value.
Example:
var reservationDate = g_form.getValue('u_reservation_from');
// correctly fetches the date/time value
Common Issues and How to Troubleshoot Them
If you're receiving a "match not found" or similar errors when setting reference fields, it's typically because you're attempting to set a field with a display value rather than the required sys_id.
Ensure that you're assigning sys_ids when setting reference fields:
g_form.setValue('u_reserved_by', userSysId);
To fetch the sys_id when you only have a display name, use GlideAjax to dynamically retrieve the sys_id from the server-side.
Conclusion
Accurately retrieving and handling field values in ServiceNow client scripts is essential for creating effective, error-free user interactions. Utilizing the correct g_form method (getValue(), getDisplayValue(), or getReference()) based on your specific use case ensures robust functionality and simplifies debugging.
Actionable Next Steps
Review your existing scripts for proper use of g_form methods.
Implement the appropriate method (getValue, getDisplayValue, or getReference) depending on your specific scenario.
Test thoroughly to ensure data consistency and accuracy.