Auto-Populating Email Addresses in ServiceNow
- kelly.ryu
- 16 hours ago
- 3 min read

Are you a ServiceNow developer or administrator looking to streamline your forms and enhance user experience? One common requirement is automatically populating an email field when a user selects a name from a reference field (like the sys_user table). This article will guide you through a simple and effective solution using client-side scripting, making your ServiceNow instance even more efficient.
An onChange Client Script
The most straightforward way to achieve this auto-population is by using an onChange client script. This type of script executes whenever the value of a specific field changes. In this case, we want the script to run when the "Name" field is modified. Here's how you can implement it:
Step-by-Step Instructions
Navigate to Client Scripts
In your ServiceNow instance, navigate to System Client Scripts and click New.
Configure the Client Script
Name: Provide a descriptive name (e.g., "Populate Email on Name Change").
Table: Select the table where your form resides (e.g., sc_req_item for service catalog requests).
Type: Choose onChange.
Field Name: Select the name of your reference field (e.g., u_name).
UI Type: Select All if this needs to work in both the platform view and the service portal, otherwise select the appropriate one.
Active: Ensure this box is checked.
Inherit: Leave unchecked in most cases.
Paste the Following Script
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
// Get the user's email address
var caller = g_form.getReference('USER_VARIABLE_NAME', getEmail); // Replace USER_VARIABLE_NAME with your "Name" field's variable name
function getEmail(caller) {
// Set the "Email" field's value
g_form.setValue('EMAIL_VARIABLE_NAME', caller.email); // Replace EMAIL_VARIABLE_NAME with your "Email" field's variable name
}
}
Important: Replace Placeholders: Carefully replace USER_VARIABLE_NAME with the actual variable name of your "Name" reference field and EMAIL_VARIABLE_NAME with the variable name of your "Email" field. You can find these names by inspecting the element in your browser's developer tools or by looking at the field's properties in the ServiceNow form designer.
Save the Script: Click Submit to save your client script.
Explanation of the Script:
The onChange function is triggered when the "Name" field changes.
g_form.getReference() retrieves the GlideRecord object for the selected user from the sys_user table. This is an asynchronous call, which means it doesn't happen instantly. The getEmail function is the callback function that executes once the GlideRecord is retrieved.
The getEmail function then sets the value of the "Email" field using g_form.setValue() to the email address retrieved from the GlideRecord (caller.email).
Practical Example
Let's say you have a catalog item for requesting new software. The form has a "Requestor" field (variable name: requestor) and an "Requestor Email" field (variable name: requestor_email). The script would look like this:
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
var caller = g_form.getReference('requestor', getEmail);
function getEmail(caller) {
g_form.setValue('requestor_email', caller.email);
}
}
Alternative Solutions
While the client script is generally the best approach, consider these alternatives:
Dot-Walking (Declarative Approach): If you only need to display the email and not actually store it in a separate field, you can use dot-walking in a read-only field. This avoids scripting altogether but is limited to display purposes. This is configured in the dictionary entry by using the 'display' attribute.
Business Rule: While possible, using a Business Rule is generally not recommended for this task. Client scripts provide a more responsive user experience as they execute directly in the browser.
Empowering Your ServiceNow Forms
By implementing an onChange client script, you can effortlessly auto-populate email addresses in your ServiceNow forms, saving users time and reducing the risk of errors. Remember to double-check your variable names and test your script thoroughly. This small improvement can significantly enhance the usability and efficiency of your ServiceNow instance.
Next Steps
Implement the script in a development environment first to avoid disrupting your production instance.
Test the script with different users to ensure it works correctly in all scenarios.
Consider adding error handling to the script to gracefully handle cases where the user's email address is not available.