Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
# User Activity Logger

A ServiceNow server-side script to log user activity with timestamps and session details.

## Description

This script creates an audit log entry whenever called, recording the current user's information, session ID, and timestamp. Useful for tracking user activities across the platform and maintaining an audit trail of user actions.

## Functionality

The User Activity Logger provides the following capabilities:
- Captures current user information (name and user ID)
- Records session ID for tracking purposes
- Creates timestamped audit trail records
- Provides success/error logging with descriptive messages
- Returns the system ID of the created log entry for further processing

## Usage Instructions

### In Business Rules

```javascript
// When: before insert
// Table: incident
// Script:
(function executeRule(current, previous /*null when async*/) {
// Call the activity logger
userActivityLogger();
})(current, previous);
```

### In Scheduled Jobs

```javascript
// Run this as a scheduled job to periodically log user sessions
userActivityLogger();
```

### In Script Includes

```javascript
var ActivityLogger = Class.create();
ActivityLogger.prototype = {
initialize: function() {
},

logActivity: function() {
return userActivityLogger();
},

type: 'ActivityLogger'
};
```

## Prerequisites

- Access to `sys_audit` table
- `gs.getUser()` API access
- Appropriate permissions to insert records into audit tables

## Dependencies

- GlideRecord API
- GlideDateTime API
- gs (GlideSystem) API



## Category

Server-Side Components / Business Rules / User Activity Logger

## Hacktoberfest 2025

Created for ServiceNow Hacktoberfest 2025 🎃

## License

MIT License
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/**
* @name User Activity Logger
* @description Logs user activity with timestamp and session details
* @author Ashvin (@ashvin2005)
* @category Server Scripts
*/

(function userActivityLogger() {
'use strict';

// Get current user information
var currentUser = gs.getUser();
var userName = currentUser.getName();
var userID = currentUser.getID();
var sessionID = gs.getSessionID();

// Create activity log record
var activityLog = new GlideRecord('sys_audit');
activityLog.initialize();
activityLog.setValue('user', userID);
activityLog.setValue('reason', 'User Activity - Session: ' + sessionID);
activityLog.setValue('description', 'User ' + userName + ' activity logged at ' + new GlideDateTime());

// Insert the record
var sysID = activityLog.insert();

if (sysID) {
gs.info('Activity logged successfully for user: ' + userName);
return sysID;
} else {
gs.error('Failed to log activity for user: ' + userName);
return null;
}
})();

/**
* Usage:
* This script can be used in Business Rules, Scheduled Jobs, or Script Includes
* to track user activities within ServiceNow.
*
* Example in Business Rule:
* - When: before insert
* - Table: incident
* - Script: Call this function to log when users create incidents
*/
Loading