Skip to content

User Activity Log Tracking #1594

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
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
16 changes: 16 additions & 0 deletions Business Rules/User Activity Log Tracking/README
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# Overview
This script logs specific user actions (e.g: record updates and approvals) in ServiceNow into a custom table `u_user_activity_log`.

This provides audit capabilities and allowing developers to track user actions for compliance or analytics.

# How It Works
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:
- `u_user`: User ID
- `u_action`: Type of action performed
- `u_record_id`: ID of the updated record
- `u_record_table`: Name of the table where the change occurred
- `u_description`: Brief description of the action

# Implementation
- 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.
- Configure Business Rule: Set the Business Rule to run on update and add conditions for monitoring fields (`state`, `approval`).
17 changes: 17 additions & 0 deletions Business Rules/User Activity Log Tracking/userActivityLog.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// Script to log user actions (updates, approvals) into a custom table
(function executeRule(current, previous /*null when async*/) {
// Check if key fields are modified (customize as needed)
if (current.state.changes() || current.approval.changes()) {
var logEntry = new GlideRecord('u_user_activity_log');
logEntry.initialize();

// Populate log details
logEntry.u_user = gs.getUserID();
logEntry.u_action = current.state.changes() ? "State Change" : "Approval Change";
logEntry.u_record_id = current.sys_id;
logEntry.u_record_table = current.getTableName();
logEntry.u_description = "User " + gs.getUserDisplayName() + " updated " + logEntry.u_action;

logEntry.insert();
}
})(current, previous);
Loading