diff --git a/Business Rules/User Activity Log Tracking/README b/Business Rules/User Activity Log Tracking/README new file mode 100644 index 0000000000..4cfa6b10ec --- /dev/null +++ b/Business Rules/User Activity Log Tracking/README @@ -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`). \ No newline at end of file diff --git a/Business Rules/User Activity Log Tracking/userActivityLog.js b/Business Rules/User Activity Log Tracking/userActivityLog.js new file mode 100644 index 0000000000..f0c97beba4 --- /dev/null +++ b/Business Rules/User Activity Log Tracking/userActivityLog.js @@ -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);