What It Does

The addErrorMessage() method queues an error message to display as a red banner at the top of the user's browser window. The message appears after the current server-side script completes and the response reaches the client. ServiceNow stores these messages in the user's session and displays them on the next page load or form refresh.

Internally, ServiceNow adds the message to a session-based queue that gets processed by the client-side framework. The platform automatically HTML-encodes the message content and strips out potentially dangerous tags to prevent cross-site scripting attacks. Multiple calls to addErrorMessage() within the same transaction will stack multiple error banners on the user's screen.

The method returns void and never fails or throws exceptions, even if passed null or undefined values. When given non-string parameters, ServiceNow converts them using JavaScript's standard string coercion rules. Empty strings display as blank error banners, which can confuse users.

⚠️

addErrorMessage() does NOT prevent saves, updates, or other operations. The transaction continues normally unless you also call setAbortAction(true).

This method works closely with gs.addInfoMessage() and gs.addMessage() to provide different message severity levels. Unlike client-side g_form.addErrorMessage(), the server-side version cannot target specific form fields and always appears at the page level.

When to Use This

Use addErrorMessage() when you need to notify users about validation failures, business rule violations, or critical errors that occurred during server-side processing. This is particularly useful in before Business Rules where you want to explain why a record cannot be saved, or in Script Includes called by AJAX requests where you need to communicate errors back to the user interface.

For field-specific validation errors, use current.fieldName.setError() instead, which highlights the problematic field and positions the error message near the input. For informational messages that aren't errors, use gs.addInfoMessage() which displays blue banners instead of red ones.

Avoid using this method for debugging or logging purposes — error messages appear to end users and can cause confusion. Never use it to display sensitive information like passwords, API keys, or internal system details that users shouldn't see.

Return Value

This method returns void (undefined in JavaScript). It provides no feedback about whether the message was successfully queued or will be displayed. The method always succeeds from a technical perspective, even when given invalid input parameters.

Since there's no return value to check, you cannot determine if the user will actually see the message. Messages may not appear if the user navigates away immediately, if their session expires, or if client-side errors prevent the page from rendering properly.

Free Newsletter

Enjoying this? Get one deep-dive per week.

Join 1,000+ ServiceNow pros — scripts, GlideRecord patterns, Flow Designer techniques, and career moves. Free.

No spam · Unsubscribe anytime

Platform Behavior & Side Effects

  • No Business Rules, ACLs, or notifications are triggered by this method — it only affects the user interface
  • Messages are stored temporarily in the user's server-side session and cleared after being displayed once
  • Performance impact is minimal — the method executes in microseconds and doesn't query the database
  • Works identically in before/after Business Rules, Script Includes, and Scheduled Jobs, but messages from background jobs are never seen by users
  • Messages persist across redirects within the same session but are lost if the user opens a new tab or browser window
  • Multiple error messages stack vertically on the page, with the most recent message appearing at the bottom of the stack
  • Error messages are automatically logged to the system log with INFO severity, not ERROR, which can complicate troubleshooting