Skip to content

Commit c5d5ce4

Browse files
authored
Code snippet for User Activity Log Tracking (#1594)
1 parent 9acf357 commit c5d5ce4

File tree

2 files changed

+33
-0
lines changed

2 files changed

+33
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# Overview
2+
This script logs specific user actions (e.g: record updates and approvals) in ServiceNow into a custom table `u_user_activity_log`.
3+
4+
This provides audit capabilities and allowing developers to track user actions for compliance or analytics.
5+
6+
# How It Works
7+
The script is triggered by a Business Rule on record updates and checks for changes in specified critical fields (e.g., `state`, `approval`). When a change occurs, it logs relevant details in the `u_user_activity_log` table, including:
8+
- `u_user`: User ID
9+
- `u_action`: Type of action performed
10+
- `u_record_id`: ID of the updated record
11+
- `u_record_table`: Name of the table where the change occurred
12+
- `u_description`: Brief description of the action
13+
14+
# Implementation
15+
- Create Custom Table: Ensure `u_user_activity_log` table exists with fields like `u_user`, `u_action`, `u_record_id`, `u_record_table`, `u_description`, etc.
16+
- Configure Business Rule: Set the Business Rule to run on update and add conditions for monitoring fields (`state`, `approval`).
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// Script to log user actions (updates, approvals) into a custom table
2+
(function executeRule(current, previous /*null when async*/) {
3+
// Check if key fields are modified (customize as needed)
4+
if (current.state.changes() || current.approval.changes()) {
5+
var logEntry = new GlideRecord('u_user_activity_log');
6+
logEntry.initialize();
7+
8+
// Populate log details
9+
logEntry.u_user = gs.getUserID();
10+
logEntry.u_action = current.state.changes() ? "State Change" : "Approval Change";
11+
logEntry.u_record_id = current.sys_id;
12+
logEntry.u_record_table = current.getTableName();
13+
logEntry.u_description = "User " + gs.getUserDisplayName() + " updated " + logEntry.u_action;
14+
15+
logEntry.insert();
16+
}
17+
})(current, previous);

0 commit comments

Comments
 (0)