gs.getUserID() in ServiceNow — Complete Reference with Examples

Everything you need to know about gs.getUserID() in ServiceNow — how it works, when to use it, common patterns, and the difference between getUserID, getUser, and getUserName.

gs.getUserID() is one of the most frequently used GlideSystem methods in ServiceNow scripting. This reference covers how it works, when to use it, and the patterns that come up most in production scripts.

What gs.getUserID() Returns

gs.getUserID() returns the sys_id of the currently logged-in user as a string. The sys_id is a 32-character GUID — the unique identifier of the user's record in the sys_user table.

// Returns the sys_id of the current user
var currentUserID = gs.getUserID();
gs.log(currentUserID);
// Output: "6816f79cc0a8016401c5a33be04be441"

gs.getUserID() vs gs.getUserName() vs gs.getUser()

These three methods are frequently confused. Here is the exact difference:

MethodReturnsExample output
gs.getUserID()sys_id of current user6816f79cc0a8016401c5...
gs.getUserName()username (login name)john.smith
gs.getUser()GlideUser objectObject with all user fields
gs.getUser().getFullName()Display nameJohn Smith

Use gs.getUserID() when you need to compare against a reference field, query for records belonging to the current user, or set a reference field to the current user.

Common Usage Patterns

1. Check if current user owns a record

// Business Rule — check if current user is the caller
if (current.caller_id == gs.getUserID()) {
    gs.log("User is the caller on this incident");
}

2. Query records belonging to the current user

var gr = new GlideRecord('incident');
gr.addQuery('assigned_to', gs.getUserID());
gr.addQuery('active', true);
gr.query();
while (gr.next()) {
    gs.log(gr.number + ' is assigned to current user');
}

3. Set a reference field to the current user

var gr = new GlideRecord('task');
gr.initialize();
gr.short_description = 'New task';
gr.assigned_to = gs.getUserID();
gr.insert();

4. ACL script — allow owner access

// Returns true if current user is the caller or has itil role
(function() {
    if (current.caller_id == gs.getUserID()) {
        return true;
    }
    return gs.hasRole('itil');
})()

5. Get user details from the sys_id

// If you need more than just the sys_id
var user = gs.getUser();
var fullName  = user.getFullName();   // "John Smith"
var email     = user.getEmail();      // "john.smith@company.com"
var userID    = user.getID();         // Same as gs.getUserID()
var userName  = user.getName();       // "john.smith"

// Or query the sys_user table directly
var userGR = new GlideRecord('sys_user');
userGR.get(gs.getUserID());
var dept = userGR.getValue('department');

Client-Side Equivalent

In Client Scripts and UI Policies, gs.getUserID() is not available. Use g_user.userID instead:

// Client Script — get current user sys_id
var currentUserID = g_user.userID;
var currentUserName = g_user.userName;
var currentUserFullName = g_user.fullName;

Important: getUserID() in Scheduled Jobs

When a script runs in a Scheduled Job, gs.getUserID() returns the sys_id of the user the job runs as — typically the system user (System Administrator) unless the job is configured to run as a specific user. Do not use gs.getUserID() in scheduled jobs to represent a real user — use a stored sys_id from the record you are processing instead.

Performance Note

gs.getUserID() is a lightweight call — it reads from the current session context, not the database. You can call it freely without performance concerns. gs.getUser() also reads from session context. Only querying sys_user directly via GlideRecord hits the database.

Want the complete reference?

This article is part of the NowSpectrum knowledge library. Browse all products for cheat sheets, interview prep, and deep-dive reference guides.

Browse All Products →
← Back to all posts