What It Does
The getUserID() method returns the sys_id of the user who triggered the current server-side script execution. ServiceNow maintains user context throughout the request lifecycle, allowing scripts to identify which user initiated the action even when processing occurs in background threads or workflow activities.
Internally, ServiceNow stores the user context in the session object and maintains it across all server-side script executions within that request. The method retrieves the sys_id field from the sys_user record of the authenticated user, not their username or display name.
The method returns a 32-character hexadecimal string when a user context exists. During system operations like scheduled jobs, data imports, or certain workflow activities, no user context exists and the method returns an empty string. Web service calls maintain user context if properly authenticated, but system-generated events may not.
User impersonation affects the return value. When an administrator impersonates another user, getUserID() returns the impersonated user's sys_id, not the administrator's. This behavior ensures security rules and user-scoped queries work correctly during impersonation sessions.
The method works identically to gs.getUser().getID() but with better performance since it doesn't instantiate a GlideUser object. Use gs.getUserName() to get the username instead of the sys_id.
When to Use This
Use getUserID() whenever you need to scope database queries to the current user, enforce security rules, or track user-specific actions. This includes filtering records by assigned_to, requested_by, or any reference field that should match the current user's sys_id.
Use gs.getUser() instead when you need additional user properties like roles, groups, or preferences. Use gs.getUserName() when you need the username for display or logging purposes. Avoid using this method in client-side scripts where g_user.userID is the correct approach.
Return Value
Returns a 32-character string containing the hexadecimal sys_id of the current user (e.g., "5137153cc611227c000bbd1bd8cd2005"). The return value is always a string type, never null or undefined. When no user context exists, such as during system operations or scheduled jobs, the method returns an empty string ("").
Always check for empty string before using the return value in queries. Use if (gs.getUserID()) to verify user context exists.
Enjoying this? Get one deep-dive per week.
Join 1,000+ ServiceNow pros — scripts, GlideRecord patterns, Flow Designer techniques, and career moves. Free.
Platform Behavior & Side Effects
- No database queries are executed - the user ID is cached in memory for the request duration
- No Business Rules, ACLs, or notifications are triggered by calling this method
- Performance is excellent - sub-millisecond execution time
- Returns the same value throughout a single request lifecycle, even across multiple script executions
- User context persists through workflow activities and async script execution within the same request
- Returns impersonated user's ID during admin impersonation sessions, not the admin's ID
- Behaves identically in before/after Business Rules, Script Includes, and UI Actions