Logging methods
Always pass a source identifier as the second argument — it makes filtering the system log fast and reliable. See the debugging guide for how to use these effectively.
gs.log('General message'); // Info level — System Log
gs.log('Processing record', 'MyScriptName'); // With source — filterable
gs.info('Processing started', 'MyScript'); // Info level
gs.warn('Queue depth high', 'JobMonitor'); // Warning level
gs.error('Failed to process', 'MyScript'); // Error level
gs.debug('Step complete', 'MyScript'); // Debug — visible when session debug enabled
User context
gs.getUserID(); // sys_id of currently logged-in user
gs.getUserName(); // Username (login name) e.g. "john.smith"
gs.getUser().getFullName(); // Display name e.g. "John Smith"
gs.getUser().getEmail(); // User's email address
gs.hasRole('itil'); // Boolean — does current user have this role?
gs.hasRole('itil,admin'); // Boolean — has either role (OR, not AND)
gs.isInteractive(); // True if user is in browser session (not API/background)
gs.getSession().isInteractive(); // Same as above
gs.getSession().getClientIP(); // Client IP address
For more on roles and how they work, see Roles vs Groups in ServiceNow. For getUserID patterns in scripts and ACLs, see gs.getUserID() complete reference.
Date and time utilities
gs.now(); // Current datetime as UTC string: "2026-05-13 14:30:00"
gs.nowDateTime(); // GlideDateTime of current time
gs.beginningOfToday(); // Start of today in local time
gs.endOfToday(); // End of today in local time
gs.beginningOfThisWeek(); // Monday 00:00 of this week
gs.endOfThisWeek(); // Sunday 23:59 of this week
gs.beginningOfThisMonth(); // First day of this month 00:00
gs.endOfThisMonth(); // Last day of this month 23:59
gs.beginningOfLastMonth(); // First day of last month
gs.endOfLastMonth(); // Last day of last month
gs.beginningOfThisQuarter(); // Start of current quarter
gs.beginningOfThisYear(); // Jan 1 of current year
gs.daysAgo(7); // datetime 7 days ago
gs.daysAgoStart(7); // start of day 7 days ago (for range queries)
gs.daysAgoEnd(0); // end of today (for range queries)
gs.hoursAgo(24); // datetime 24 hours ago
gs.minutesAgo(30); // datetime 30 minutes ago
System properties
// Read a system property
gs.getProperty('glide.ui.home'); // Returns value or null
gs.getProperty('my.setting', 'default_value'); // Returns default if not found
// Set a system property (requires admin role)
gs.setProperty('my.setting', 'new_value');
gs.setProperty('my.setting', 'new_value', 'Description'); // With description
// Check before using
var endpoint = gs.getProperty('my.api.endpoint');
if (!endpoint) {
gs.error('System property my.api.endpoint is not configured', 'MyIntegration');
return;
}
For the full gs.getProperty() reference including type handling and caching, see gs.getProperty() complete reference.
Event management
// Queue an event that fires a notification or Script Action
gs.eventQueue(
'incident.priority.changed', // Event name (must exist in Event Registry)
current, // GlideRecord object (event source)
current.getValue('assigned_to'), // param1
current.getValue('priority') // param2
);
// Schedule event for future execution
gs.eventQueueScheduled(
'event.name',
current,
param1,
param2,
'tomorrow' // Run time: 'tomorrow', 'next week', or GlideDateTime string
);
// Check if an event is queued
var isQueued = gs.eventQueuedAlready('incident.priority.changed', current);
String and encoding utilities
gs.generateGUID(); // Generate a new 32-char sys_id
gs.urlEncode('hello world'); // "hello+world"
gs.urlDecode('hello+world'); // "hello world"
gs.base64Encode('my string'); // Base64 encoded string
gs.base64Decode('bXkgc3RyaW5n'); // Decoded string
gs.xmlToJSON(xmlString); // Convert XML string to JavaScript object
gs.jsonToXML(jsonObject); // Convert object to XML string
gs.htmlDecode('<b>'); // Decode HTML entities
Number formatting
gs.getProperty('glide.currency.code', 'USD'); // Instance currency code
// Format a number with locale settings
// Note: for currency/number display, GlideLocale is typically used
// gs provides basic utilities
Session and context
gs.getSession(); // GlideSession object
gs.getSession().putClientData('key', 'value'); // Store value in session
gs.getSession().getClientData('key'); // Retrieve from session (string)
gs.getCurrentApplicationID(); // Current application scope sys_id
gs.getCurrentApplicationName(); // Current application scope name
gs.getMaxSchemaNameLength(); // Max table/column name length
gs.getNodeName(); // Name of current node in cluster
Impersonation utilities
// Check if currently running as background/system
if (!gs.isInteractive()) {
// Running via API, Scheduled Job, or background script
gs.log('Non-interactive session — skipping UI checks', 'MyScript');
}
// Get the real user when impersonating
gs.getImpersonatingUserName(); // Returns username being impersonated, or null
gs.getImpersonatingUserDisplayName(); // Display name of impersonated user
Table and schema utilities
// Check if a table exists
var tableExists = gs.tableExists('my_custom_table');
// Check if a record exists by sys_id
var recExists = gs.recordExists('incident', sys_id);
// Get the display name of a table
gs.getTableDisplayName('incident'); // Returns "Incident"
Sending emails programmatically
// Send a simple email from a script
var email = new GlideEmailOutbound();
email.setSubject('Incident Alert: ' + current.getValue('number'));
email.setBody('A new P1 incident has been created: ' + current.getValue('short_description'));
email.addRecipient(current.assigned_to.getDisplayValue());
email.addAddress('to', 'manager@company.com');
email.save();
Password and security utilities
gs.generateGUID(); // Use for generating sys_ids or unique keys
// For generating passwords:
var chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
var password = '';
for (var i = 0; i < 12; i++) {
password += chars.charAt(Math.floor(Math.random() * chars.length));
}
Related scripting guides:
- gs.getUserID() — complete reference with patterns
- gs.getProperty() — system properties in scripts
- Debugging scripts — using gs.log() effectively
- Script Includes — gs usage in server-side libraries
- Business Rule types — gs context in each rule type