The GlideSystem (gs) object provides access to system methods and information. It's available in all server-side scripts and offers utilities for logging, user management, properties, and more.
The gs object is ONLY available in server-side scripts (Business Rules, Script Includes, etc.). Not available in client scripts.
// Log levels (shown in System Log)
gs.log('This is a log message'); // INFO level
gs.info('Information message'); // INFO level
gs.warn('Warning message'); // WARNING level
gs.error('Error message'); // ERROR level
gs.debug('Debug message'); // DEBUG level
// Log with source (helps identify where log came from)
gs.log('Processing incident', 'MyScriptInclude');
gs.error('Failed to update record', 'BusinessRule.UpdateIncident');
// Example in Business Rule
if (current.priority == '1' && current.assignment_group.nil()) {
gs.error('Critical incident without assignment group: ' + current.number);
}
Use appropriate log levels. Use gs.info() for normal operations, gs.warn() for potential issues, gs.error() for failures. View logs at System Logs > System Log.
// Get current user's sys_id
var userID = gs.getUserID();
// Get current user's username
var userName = gs.getUserName(); // Returns 'john.doe'
// Get user's display name
var displayName = gs.getUserDisplayName(); // Returns 'John Doe'
// Check if user has a role
if (gs.hasRole('admin')) {
// User has admin role
}
if (gs.hasRole('itil,admin')) {
// User has EITHER itil OR admin role (OR logic)
}
// Check if user is impersonating
if (gs.isImpersonating()) {
gs.log('User is impersonating');
}
// Example: Auto-assign to current user
if (current.assigned_to.nil()) {
current.assigned_to = gs.getUserID();
}
// Info message (blue banner)
gs.addInfoMessage('Record has been updated successfully');
// Error message (red banner)
gs.addErrorMessage('Failed to update record: Missing required field');
// Example in Business Rule
if (current.priority == '1' && current.assignment_group.nil()) {
gs.addErrorMessage('Critical incidents must have an assignment group');
current.setAbortAction(true); // Prevent save
}
// Success message
gs.addInfoMessage('Incident ' + current.number + ' has been created');
// Get system property value
var maxAttachmentSize = gs.getProperty('glide.attachment.max_size');
var instanceName = gs.getProperty('instance_name');
// Get property with default value if not found
var defaultPriority = gs.getProperty('custom.default.priority', '3');
// Set property value (requires admin role)
gs.setProperty('custom.my.property', 'value');
// Example: Use property for configuration
var autoAssignEnabled = gs.getProperty('custom.incident.auto_assign', 'false');
if (autoAssignEnabled == 'true') {
// Auto-assign logic
}
// Get current date/time
var now = gs.now(); // Returns: '2024-10-24 10:30:00'
var nowDateTime = gs.nowDateTime(); // Returns GlideDateTime object
var nowNoTZ = gs.nowNoTZ(); // Without timezone conversion
// Days ago
var sevenDaysAgo = gs.daysAgo(7); // Date 7 days ago
var startOfToday = gs.daysAgoStart(0); // Start of today
var endOfToday = gs.daysAgoEnd(0); // End of today
// Months ago
var oneMonthAgo = gs.monthsAgo(1);
var startOfMonth = gs.monthsAgoStart(0);
var endOfMonth = gs.monthsAgoEnd(0);
// Begin/End of specific periods
var startOfWeek = gs.beginningOfThisWeek();
var endOfWeek = gs.endOfThisWeek();
var startOfMonth = gs.beginningOfThisMonth();
var endOfMonth = gs.endOfThisMonth();
var startOfYear = gs.beginningOfThisYear();
// Date/Time calculations
var yesterday = gs.daysAgo(1);
var nextWeek = gs.daysAgo(-7); // Negative for future
// Example: Query records from last 30 days
var gr = new GlideRecord('incident');
gr.addQuery('sys_created_on', '>', gs.daysAgoStart(30));
gr.query();
// Generate GUID
var newGUID = gs.generateGUID();
// Get message from sys_ui_message table
var msg = gs.getMessage('Record not found');
var msgWithParams = gs.getMessage('Hello {0}, you have {1} tasks', [userName, taskCount]);
// Nil check (check if value is null or empty)
if (gs.nil(current.assignment_group)) {
// Field is empty
}
// Get session info
var sessionID = gs.getSessionID();
var clientIP = gs.getClientIP();
// URL utilities
var currentURL = gs.getProperty('glide.servlet.uri');
var baseURL = gs.getProperty('glide.servlet.uri') + '/nav_to.do?uri=' + current.getLink();
// Execute business rule
gs.eventQueue('incident.assigned', current);
// Sleep (use sparingly)
gs.sleep(1000); // Sleep for 1 second (1000 ms)
// Example: Generate unique number
var uniqueID = gs.generateGUID();
gs.log('Generated ID: ' + uniqueID);
// Evaluate script from string (use carefully!)
var result = gs.eval('2 + 2'); // Returns 4
// Check if running in background
if (gs.isInteractive()) {
// Running interactively (from UI)
} else {
// Running in background (scheduled job, etc.)
}
// Get current scope
var currentScope = gs.getCurrentScopeName(); // Returns 'global' or app scope
// Include script
gs.include('UtilityScriptInclude');
gs.getUserID() - User sys_idgs.getUserName() - Usernamegs.hasRole('role') - Check rolegs.addInfoMessage() - Info bannergs.addErrorMessage() - Error bannergs.log() - Write to logsgs.now() - Current timegs.daysAgo(n) - Date n days agogs.getProperty() - System propertygs.nil(value) - Check null/empty