From 6c01e7be47db9937bdc24887c8ae7e1f584ed599 Mon Sep 17 00:00:00 2001 From: Ashvin Tiwari Date: Mon, 20 Oct 2025 04:14:28 +0530 Subject: [PATCH 1/5] Add User Activity Logger script for ServiceNow --- server-scripts/README-user-activity-logger.md | 54 +++++++++++++++++++ server-scripts/user-activity-logger.js | 45 ++++++++++++++++ 2 files changed, 99 insertions(+) create mode 100644 server-scripts/README-user-activity-logger.md create mode 100644 server-scripts/user-activity-logger.js diff --git a/server-scripts/README-user-activity-logger.md b/server-scripts/README-user-activity-logger.md new file mode 100644 index 0000000000..3ce3a880b6 --- /dev/null +++ b/server-scripts/README-user-activity-logger.md @@ -0,0 +1,54 @@ +# User Activity Logger + +A ServiceNow server-side script to log user activity with timestamps and session details. + +## Description + +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. + +## Features + +- ✅ Captures current user information +- ✅ Logs session ID for tracking +- ✅ Creates audit trail records +- ✅ Error handling included +- ✅ Easy to integrate into Business Rules or Scheduled Jobs + +## Usage + +### In Business Rules + +```javascript +// When: before insert +// Table: incident +// Script: +(function executeRule(current, previous /*null when async*/) { + // Call the activity logger + userActivityLogger(); +})(current, previous); +``` + +### In Scheduled Jobs + +```javascript +// Run this as a scheduled job to periodically log user sessions +userActivityLogger(); +``` + +## Author + +**Ashvin** +- GitHub: [@ashvin2005](https://github.com/ashvin2005) +- LinkedIn: [ashvin-tiwari](https://linkedin.com/in/ashvin-tiwari) + +## Category + +Server Scripts / Business Rules + +## Hacktoberfest 2025 + +Created for ServiceNow Hacktoberfest 2025 🎃 + +## License + +MIT License \ No newline at end of file diff --git a/server-scripts/user-activity-logger.js b/server-scripts/user-activity-logger.js new file mode 100644 index 0000000000..c46fedfdac --- /dev/null +++ b/server-scripts/user-activity-logger.js @@ -0,0 +1,45 @@ +/** + * @name User Activity Logger + * @description Logs user activity with timestamp and session details + * @author Ashvin (@ashvin2005) + * @category Server Scripts + */ + +(function userActivityLogger() { + 'use strict'; + + // Get current user information + var currentUser = gs.getUser(); + var userName = currentUser.getName(); + var userID = currentUser.getID(); + var sessionID = gs.getSessionID(); + + // Create activity log record + var activityLog = new GlideRecord('sys_audit'); + activityLog.initialize(); + activityLog.setValue('user', userID); + activityLog.setValue('reason', 'User Activity - Session: ' + sessionID); + activityLog.setValue('description', 'User ' + userName + ' activity logged at ' + new GlideDateTime()); + + // Insert the record + var sysID = activityLog.insert(); + + if (sysID) { + gs.info('Activity logged successfully for user: ' + userName); + return sysID; + } else { + gs.error('Failed to log activity for user: ' + userName); + return null; + } +})(); + +/** + * Usage: + * This script can be used in Business Rules, Scheduled Jobs, or Script Includes + * to track user activities within ServiceNow. + * + * Example in Business Rule: + * - When: before insert + * - Table: incident + * - Script: Call this function to log when users create incidents + */ \ No newline at end of file From ab3e2a5816e723d047292609797ac8e53264e61f Mon Sep 17 00:00:00 2001 From: Ashvin Tiwari Date: Mon, 20 Oct 2025 04:18:09 +0530 Subject: [PATCH 2/5] Add User Activity Logger script for ServiceNow --- .../user-activity-logger/README.md | 83 +++++++++++++++++++ .../user-activity-logger.js | 45 ++++++++++ 2 files changed, 128 insertions(+) create mode 100644 Server-Side Components/user-activity-logger/README.md create mode 100644 Server-Side Components/user-activity-logger/user-activity-logger.js diff --git a/Server-Side Components/user-activity-logger/README.md b/Server-Side Components/user-activity-logger/README.md new file mode 100644 index 0000000000..e04d4bbc07 --- /dev/null +++ b/Server-Side Components/user-activity-logger/README.md @@ -0,0 +1,83 @@ +# User Activity Logger + +A ServiceNow server-side script to log user activity with timestamps and session details. + +## Description + +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. + +## Functionality + +The User Activity Logger provides the following capabilities: +- Captures current user information (name and user ID) +- Records session ID for tracking purposes +- Creates timestamped audit trail records +- Provides success/error logging with descriptive messages +- Returns the system ID of the created log entry for further processing + +## Usage Instructions + +### In Business Rules + +```javascript +// When: before insert +// Table: incident +// Script: +(function executeRule(current, previous /*null when async*/) { + // Call the activity logger + userActivityLogger(); +})(current, previous); +``` + +### In Scheduled Jobs + +```javascript +// Run this as a scheduled job to periodically log user sessions +userActivityLogger(); +``` + +### In Script Includes + +```javascript +var ActivityLogger = Class.create(); +ActivityLogger.prototype = { + initialize: function() { + }, + + logActivity: function() { + return userActivityLogger(); + }, + + type: 'ActivityLogger' +}; +``` + +## Prerequisites + +- Access to `sys_audit` table +- `gs.getUser()` API access +- Appropriate permissions to insert records into audit tables + +## Dependencies + +- GlideRecord API +- GlideDateTime API +- gs (GlideSystem) API + +## Author + +**Ashvin** +- GitHub: [@ashvin2005](https://github.com/ashvin2005) +- LinkedIn: [ashvin-tiwari](https://linkedin.com/in/ashvin-tiwari) + +## Category + +Server-Side Components / Business Rules / Script Includes + +## Hacktoberfest 2025 + +Created for ServiceNow Hacktoberfest 2025 🎃 + +## License + +MIT License \ No newline at end of file diff --git a/Server-Side Components/user-activity-logger/user-activity-logger.js b/Server-Side Components/user-activity-logger/user-activity-logger.js new file mode 100644 index 0000000000..c46fedfdac --- /dev/null +++ b/Server-Side Components/user-activity-logger/user-activity-logger.js @@ -0,0 +1,45 @@ +/** + * @name User Activity Logger + * @description Logs user activity with timestamp and session details + * @author Ashvin (@ashvin2005) + * @category Server Scripts + */ + +(function userActivityLogger() { + 'use strict'; + + // Get current user information + var currentUser = gs.getUser(); + var userName = currentUser.getName(); + var userID = currentUser.getID(); + var sessionID = gs.getSessionID(); + + // Create activity log record + var activityLog = new GlideRecord('sys_audit'); + activityLog.initialize(); + activityLog.setValue('user', userID); + activityLog.setValue('reason', 'User Activity - Session: ' + sessionID); + activityLog.setValue('description', 'User ' + userName + ' activity logged at ' + new GlideDateTime()); + + // Insert the record + var sysID = activityLog.insert(); + + if (sysID) { + gs.info('Activity logged successfully for user: ' + userName); + return sysID; + } else { + gs.error('Failed to log activity for user: ' + userName); + return null; + } +})(); + +/** + * Usage: + * This script can be used in Business Rules, Scheduled Jobs, or Script Includes + * to track user activities within ServiceNow. + * + * Example in Business Rule: + * - When: before insert + * - Table: incident + * - Script: Call this function to log when users create incidents + */ \ No newline at end of file From c777396d8315049892d079c8808e7b9f6fd1f6be Mon Sep 17 00:00:00 2001 From: Ashvin Tiwari Date: Mon, 20 Oct 2025 04:21:00 +0530 Subject: [PATCH 3/5] Add User Activity Logger script in correct folder structure --- .../user-activity-logger/README.md | 83 +++++++++++++++++++ .../user-activity-logger.js | 45 ++++++++++ 2 files changed, 128 insertions(+) create mode 100644 Server-Side Components/Business Rules/user-activity-logger/README.md create mode 100644 Server-Side Components/Business Rules/user-activity-logger/user-activity-logger.js diff --git a/Server-Side Components/Business Rules/user-activity-logger/README.md b/Server-Side Components/Business Rules/user-activity-logger/README.md new file mode 100644 index 0000000000..3274674ffc --- /dev/null +++ b/Server-Side Components/Business Rules/user-activity-logger/README.md @@ -0,0 +1,83 @@ +# User Activity Logger + +A ServiceNow server-side script to log user activity with timestamps and session details. + +## Description + +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. + +## Functionality + +The User Activity Logger provides the following capabilities: +- Captures current user information (name and user ID) +- Records session ID for tracking purposes +- Creates timestamped audit trail records +- Provides success/error logging with descriptive messages +- Returns the system ID of the created log entry for further processing + +## Usage Instructions + +### In Business Rules + +```javascript +// When: before insert +// Table: incident +// Script: +(function executeRule(current, previous /*null when async*/) { + // Call the activity logger + userActivityLogger(); +})(current, previous); +``` + +### In Scheduled Jobs + +```javascript +// Run this as a scheduled job to periodically log user sessions +userActivityLogger(); +``` + +### In Script Includes + +```javascript +var ActivityLogger = Class.create(); +ActivityLogger.prototype = { + initialize: function() { + }, + + logActivity: function() { + return userActivityLogger(); + }, + + type: 'ActivityLogger' +}; +``` + +## Prerequisites + +- Access to `sys_audit` table +- `gs.getUser()` API access +- Appropriate permissions to insert records into audit tables + +## Dependencies + +- GlideRecord API +- GlideDateTime API +- gs (GlideSystem) API + +## Author + +**Ashvin** +- GitHub: [@ashvin2005](https://github.com/ashvin2005) +- LinkedIn: [ashvin-tiwari](https://linkedin.com/in/ashvin-tiwari) + +## Category + +Server-Side Components / Business Rules / User Activity Logger + +## Hacktoberfest 2025 + +Created for ServiceNow Hacktoberfest 2025 🎃 + +## License + +MIT License \ No newline at end of file diff --git a/Server-Side Components/Business Rules/user-activity-logger/user-activity-logger.js b/Server-Side Components/Business Rules/user-activity-logger/user-activity-logger.js new file mode 100644 index 0000000000..c46fedfdac --- /dev/null +++ b/Server-Side Components/Business Rules/user-activity-logger/user-activity-logger.js @@ -0,0 +1,45 @@ +/** + * @name User Activity Logger + * @description Logs user activity with timestamp and session details + * @author Ashvin (@ashvin2005) + * @category Server Scripts + */ + +(function userActivityLogger() { + 'use strict'; + + // Get current user information + var currentUser = gs.getUser(); + var userName = currentUser.getName(); + var userID = currentUser.getID(); + var sessionID = gs.getSessionID(); + + // Create activity log record + var activityLog = new GlideRecord('sys_audit'); + activityLog.initialize(); + activityLog.setValue('user', userID); + activityLog.setValue('reason', 'User Activity - Session: ' + sessionID); + activityLog.setValue('description', 'User ' + userName + ' activity logged at ' + new GlideDateTime()); + + // Insert the record + var sysID = activityLog.insert(); + + if (sysID) { + gs.info('Activity logged successfully for user: ' + userName); + return sysID; + } else { + gs.error('Failed to log activity for user: ' + userName); + return null; + } +})(); + +/** + * Usage: + * This script can be used in Business Rules, Scheduled Jobs, or Script Includes + * to track user activities within ServiceNow. + * + * Example in Business Rule: + * - When: before insert + * - Table: incident + * - Script: Call this function to log when users create incidents + */ \ No newline at end of file From 132ad4d09c92478cb118aa9b0bf88ca1515a26a9 Mon Sep 17 00:00:00 2001 From: Ashvin Tiwari Date: Mon, 20 Oct 2025 04:23:51 +0530 Subject: [PATCH 4/5] Add User Activity Logger script in correct folder structure --- .../user-activity-logger/README.md | 83 ------------------- .../user-activity-logger.js | 45 ---------- server-scripts/README-user-activity-logger.md | 54 ------------ server-scripts/user-activity-logger.js | 45 ---------- 4 files changed, 227 deletions(-) delete mode 100644 Server-Side Components/user-activity-logger/README.md delete mode 100644 Server-Side Components/user-activity-logger/user-activity-logger.js delete mode 100644 server-scripts/README-user-activity-logger.md delete mode 100644 server-scripts/user-activity-logger.js diff --git a/Server-Side Components/user-activity-logger/README.md b/Server-Side Components/user-activity-logger/README.md deleted file mode 100644 index e04d4bbc07..0000000000 --- a/Server-Side Components/user-activity-logger/README.md +++ /dev/null @@ -1,83 +0,0 @@ -# User Activity Logger - -A ServiceNow server-side script to log user activity with timestamps and session details. - -## Description - -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. - -## Functionality - -The User Activity Logger provides the following capabilities: -- Captures current user information (name and user ID) -- Records session ID for tracking purposes -- Creates timestamped audit trail records -- Provides success/error logging with descriptive messages -- Returns the system ID of the created log entry for further processing - -## Usage Instructions - -### In Business Rules - -```javascript -// When: before insert -// Table: incident -// Script: -(function executeRule(current, previous /*null when async*/) { - // Call the activity logger - userActivityLogger(); -})(current, previous); -``` - -### In Scheduled Jobs - -```javascript -// Run this as a scheduled job to periodically log user sessions -userActivityLogger(); -``` - -### In Script Includes - -```javascript -var ActivityLogger = Class.create(); -ActivityLogger.prototype = { - initialize: function() { - }, - - logActivity: function() { - return userActivityLogger(); - }, - - type: 'ActivityLogger' -}; -``` - -## Prerequisites - -- Access to `sys_audit` table -- `gs.getUser()` API access -- Appropriate permissions to insert records into audit tables - -## Dependencies - -- GlideRecord API -- GlideDateTime API -- gs (GlideSystem) API - -## Author - -**Ashvin** -- GitHub: [@ashvin2005](https://github.com/ashvin2005) -- LinkedIn: [ashvin-tiwari](https://linkedin.com/in/ashvin-tiwari) - -## Category - -Server-Side Components / Business Rules / Script Includes - -## Hacktoberfest 2025 - -Created for ServiceNow Hacktoberfest 2025 🎃 - -## License - -MIT License \ No newline at end of file diff --git a/Server-Side Components/user-activity-logger/user-activity-logger.js b/Server-Side Components/user-activity-logger/user-activity-logger.js deleted file mode 100644 index c46fedfdac..0000000000 --- a/Server-Side Components/user-activity-logger/user-activity-logger.js +++ /dev/null @@ -1,45 +0,0 @@ -/** - * @name User Activity Logger - * @description Logs user activity with timestamp and session details - * @author Ashvin (@ashvin2005) - * @category Server Scripts - */ - -(function userActivityLogger() { - 'use strict'; - - // Get current user information - var currentUser = gs.getUser(); - var userName = currentUser.getName(); - var userID = currentUser.getID(); - var sessionID = gs.getSessionID(); - - // Create activity log record - var activityLog = new GlideRecord('sys_audit'); - activityLog.initialize(); - activityLog.setValue('user', userID); - activityLog.setValue('reason', 'User Activity - Session: ' + sessionID); - activityLog.setValue('description', 'User ' + userName + ' activity logged at ' + new GlideDateTime()); - - // Insert the record - var sysID = activityLog.insert(); - - if (sysID) { - gs.info('Activity logged successfully for user: ' + userName); - return sysID; - } else { - gs.error('Failed to log activity for user: ' + userName); - return null; - } -})(); - -/** - * Usage: - * This script can be used in Business Rules, Scheduled Jobs, or Script Includes - * to track user activities within ServiceNow. - * - * Example in Business Rule: - * - When: before insert - * - Table: incident - * - Script: Call this function to log when users create incidents - */ \ No newline at end of file diff --git a/server-scripts/README-user-activity-logger.md b/server-scripts/README-user-activity-logger.md deleted file mode 100644 index 3ce3a880b6..0000000000 --- a/server-scripts/README-user-activity-logger.md +++ /dev/null @@ -1,54 +0,0 @@ -# User Activity Logger - -A ServiceNow server-side script to log user activity with timestamps and session details. - -## Description - -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. - -## Features - -- ✅ Captures current user information -- ✅ Logs session ID for tracking -- ✅ Creates audit trail records -- ✅ Error handling included -- ✅ Easy to integrate into Business Rules or Scheduled Jobs - -## Usage - -### In Business Rules - -```javascript -// When: before insert -// Table: incident -// Script: -(function executeRule(current, previous /*null when async*/) { - // Call the activity logger - userActivityLogger(); -})(current, previous); -``` - -### In Scheduled Jobs - -```javascript -// Run this as a scheduled job to periodically log user sessions -userActivityLogger(); -``` - -## Author - -**Ashvin** -- GitHub: [@ashvin2005](https://github.com/ashvin2005) -- LinkedIn: [ashvin-tiwari](https://linkedin.com/in/ashvin-tiwari) - -## Category - -Server Scripts / Business Rules - -## Hacktoberfest 2025 - -Created for ServiceNow Hacktoberfest 2025 🎃 - -## License - -MIT License \ No newline at end of file diff --git a/server-scripts/user-activity-logger.js b/server-scripts/user-activity-logger.js deleted file mode 100644 index c46fedfdac..0000000000 --- a/server-scripts/user-activity-logger.js +++ /dev/null @@ -1,45 +0,0 @@ -/** - * @name User Activity Logger - * @description Logs user activity with timestamp and session details - * @author Ashvin (@ashvin2005) - * @category Server Scripts - */ - -(function userActivityLogger() { - 'use strict'; - - // Get current user information - var currentUser = gs.getUser(); - var userName = currentUser.getName(); - var userID = currentUser.getID(); - var sessionID = gs.getSessionID(); - - // Create activity log record - var activityLog = new GlideRecord('sys_audit'); - activityLog.initialize(); - activityLog.setValue('user', userID); - activityLog.setValue('reason', 'User Activity - Session: ' + sessionID); - activityLog.setValue('description', 'User ' + userName + ' activity logged at ' + new GlideDateTime()); - - // Insert the record - var sysID = activityLog.insert(); - - if (sysID) { - gs.info('Activity logged successfully for user: ' + userName); - return sysID; - } else { - gs.error('Failed to log activity for user: ' + userName); - return null; - } -})(); - -/** - * Usage: - * This script can be used in Business Rules, Scheduled Jobs, or Script Includes - * to track user activities within ServiceNow. - * - * Example in Business Rule: - * - When: before insert - * - Table: incident - * - Script: Call this function to log when users create incidents - */ \ No newline at end of file From 18ac4db9ef858dab3c0a7898328af8fa8c9b6abe Mon Sep 17 00:00:00 2001 From: Ashvin Tiwari Date: Mon, 20 Oct 2025 06:21:05 +0530 Subject: [PATCH 5/5] Remove author section from README Removed author information from the README. --- .../Business Rules/user-activity-logger/README.md | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/Server-Side Components/Business Rules/user-activity-logger/README.md b/Server-Side Components/Business Rules/user-activity-logger/README.md index 3274674ffc..128742eb50 100644 --- a/Server-Side Components/Business Rules/user-activity-logger/README.md +++ b/Server-Side Components/Business Rules/user-activity-logger/README.md @@ -64,11 +64,7 @@ ActivityLogger.prototype = { - GlideDateTime API - gs (GlideSystem) API -## Author -**Ashvin** -- GitHub: [@ashvin2005](https://github.com/ashvin2005) -- LinkedIn: [ashvin-tiwari](https://linkedin.com/in/ashvin-tiwari) ## Category @@ -80,4 +76,4 @@ Created for ServiceNow Hacktoberfest 2025 🎃 ## License -MIT License \ No newline at end of file +MIT License