Skip to content

Commit ab3e2a5

Browse files
committed
Add User Activity Logger script for ServiceNow
1 parent 6c01e7b commit ab3e2a5

File tree

2 files changed

+128
-0
lines changed

2 files changed

+128
-0
lines changed
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
# User Activity Logger
2+
3+
A ServiceNow server-side script to log user activity with timestamps and session details.
4+
5+
## Description
6+
7+
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.
8+
9+
## Functionality
10+
11+
The User Activity Logger provides the following capabilities:
12+
- Captures current user information (name and user ID)
13+
- Records session ID for tracking purposes
14+
- Creates timestamped audit trail records
15+
- Provides success/error logging with descriptive messages
16+
- Returns the system ID of the created log entry for further processing
17+
18+
## Usage Instructions
19+
20+
### In Business Rules
21+
22+
```javascript
23+
// When: before insert
24+
// Table: incident
25+
// Script:
26+
(function executeRule(current, previous /*null when async*/) {
27+
// Call the activity logger
28+
userActivityLogger();
29+
})(current, previous);
30+
```
31+
32+
### In Scheduled Jobs
33+
34+
```javascript
35+
// Run this as a scheduled job to periodically log user sessions
36+
userActivityLogger();
37+
```
38+
39+
### In Script Includes
40+
41+
```javascript
42+
var ActivityLogger = Class.create();
43+
ActivityLogger.prototype = {
44+
initialize: function() {
45+
},
46+
47+
logActivity: function() {
48+
return userActivityLogger();
49+
},
50+
51+
type: 'ActivityLogger'
52+
};
53+
```
54+
55+
## Prerequisites
56+
57+
- Access to `sys_audit` table
58+
- `gs.getUser()` API access
59+
- Appropriate permissions to insert records into audit tables
60+
61+
## Dependencies
62+
63+
- GlideRecord API
64+
- GlideDateTime API
65+
- gs (GlideSystem) API
66+
67+
## Author
68+
69+
**Ashvin**
70+
- GitHub: [@ashvin2005](https://github.com/ashvin2005)
71+
- LinkedIn: [ashvin-tiwari](https://linkedin.com/in/ashvin-tiwari)
72+
73+
## Category
74+
75+
Server-Side Components / Business Rules / Script Includes
76+
77+
## Hacktoberfest 2025
78+
79+
Created for ServiceNow Hacktoberfest 2025 🎃
80+
81+
## License
82+
83+
MIT License
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/**
2+
* @name User Activity Logger
3+
* @description Logs user activity with timestamp and session details
4+
* @author Ashvin (@ashvin2005)
5+
* @category Server Scripts
6+
*/
7+
8+
(function userActivityLogger() {
9+
'use strict';
10+
11+
// Get current user information
12+
var currentUser = gs.getUser();
13+
var userName = currentUser.getName();
14+
var userID = currentUser.getID();
15+
var sessionID = gs.getSessionID();
16+
17+
// Create activity log record
18+
var activityLog = new GlideRecord('sys_audit');
19+
activityLog.initialize();
20+
activityLog.setValue('user', userID);
21+
activityLog.setValue('reason', 'User Activity - Session: ' + sessionID);
22+
activityLog.setValue('description', 'User ' + userName + ' activity logged at ' + new GlideDateTime());
23+
24+
// Insert the record
25+
var sysID = activityLog.insert();
26+
27+
if (sysID) {
28+
gs.info('Activity logged successfully for user: ' + userName);
29+
return sysID;
30+
} else {
31+
gs.error('Failed to log activity for user: ' + userName);
32+
return null;
33+
}
34+
})();
35+
36+
/**
37+
* Usage:
38+
* This script can be used in Business Rules, Scheduled Jobs, or Script Includes
39+
* to track user activities within ServiceNow.
40+
*
41+
* Example in Business Rule:
42+
* - When: before insert
43+
* - Table: incident
44+
* - Script: Call this function to log when users create incidents
45+
*/

0 commit comments

Comments
 (0)