The result object pattern exists because ServiceNow developers need a consistent way to handle both success and failure scenarios without relying on exceptions. Unlike languages with robust exception handling, server-side JavaScript in ServiceNow benefits from explicit success/failure communication. When your Script Include method throws an exception, it crashes the calling Business Rule or Scheduled Job with cryptic error messages. When it returns structured objects, calling code can gracefully handle failures and provide meaningful user feedback.
When to use this pattern
- When your Script Include performs operations that can fail (record creation, external API calls, data validation)
- When calling code needs to handle failures differently than successes (show error messages vs. redirect users)
- When you need to return both status information and data payload in a single response
- When your method is called from Business Rules, Scheduled Jobs, or other Script Includes that can't afford to crash
When NOT to use this pattern
- For simple getter methods that just return data — use direct return values instead
- When calling from client-side code — use GlideAjax with proper JSON serialization instead
- For utility functions that perform calculations — return the calculated value directly
- When the operation genuinely should crash the system on failure — let exceptions bubble up
Key behaviors and gotchas
Always return the same object structure — {success: boolean}with optionaldataorerrorproperties- Include relevant data in error responses when it helps calling code make decisions (like existing record IDs)
- Error messages should be specific enough for debugging but safe for end users to see
- Check for null/empty parameters at the method start — fail fast with clear validation errors
- Use
getUniqueValue()notgetValue('sys_id')when returning record IDs in the data payload - GlideRecord
insert()returns the sys_id string on success or null on failure — check explicitly
Never concatenate error messages with user input without sanitization. A malicious department name or email address can become an injection vector when displayed in the UI.
Enjoying this? Get one deep-dive per week.
Join 1,000+ ServiceNow pros — scripts, GlideRecord patterns, Flow Designer techniques, and career moves. Free.