Skip to content

Commit 6c01e7b

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

File tree

2 files changed

+99
-0
lines changed

2 files changed

+99
-0
lines changed
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
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.
8+
9+
## Features
10+
11+
- ✅ Captures current user information
12+
- ✅ Logs session ID for tracking
13+
- ✅ Creates audit trail records
14+
- ✅ Error handling included
15+
- ✅ Easy to integrate into Business Rules or Scheduled Jobs
16+
17+
## Usage
18+
19+
### In Business Rules
20+
21+
```javascript
22+
// When: before insert
23+
// Table: incident
24+
// Script:
25+
(function executeRule(current, previous /*null when async*/) {
26+
// Call the activity logger
27+
userActivityLogger();
28+
})(current, previous);
29+
```
30+
31+
### In Scheduled Jobs
32+
33+
```javascript
34+
// Run this as a scheduled job to periodically log user sessions
35+
userActivityLogger();
36+
```
37+
38+
## Author
39+
40+
**Ashvin**
41+
- GitHub: [@ashvin2005](https://github.com/ashvin2005)
42+
- LinkedIn: [ashvin-tiwari](https://linkedin.com/in/ashvin-tiwari)
43+
44+
## Category
45+
46+
Server Scripts / Business Rules
47+
48+
## Hacktoberfest 2025
49+
50+
Created for ServiceNow Hacktoberfest 2025 🎃
51+
52+
## License
53+
54+
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)