- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-05-2023 11:29 PM
Hello everyone,
I appreciate your time in reading and responding to my query.
I am currently working on retrieving historical activities for each ticket in ServiceNow. I noticed that in the sys_audit table, changes to all fields are stored separately. However, in the activity stream, updates made to multiple fields at once are considered as a single update and hence, result in only one record. This discrepancy results in a difference in the count of changes - for instance, a ticket may show only 10 changes in the activity stream, but there could be 70 corresponding records in the sys_audit table.
My team is interested in retrieving these activities and ensuring that the count of activities they obtain matches the count displayed on the ticket.
Any guidance on how to achieve this would be greatly appreciated. Thank you in advance for your help!
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-05-2023 11:31 PM
HI @Bernie_BeAr ,
I trust you are doing great.
Here’s a high-level approach:
-
Query the
sys_audit
Table: Fetch records from thesys_audit
table for the specific ticket. You can filter based on thedocumentkey
field, which links to the ticket ID. -
Group Changes by Timestamp: The
sys_audit
table includes a timestamp for each change. Group changes that occur at the same timestamp. This grouping will mimic the activity stream's behavior of consolidating changes made in a single update. -
Count the Grouped Changes: Count each group of changes as one record, similar to how the activity stream counts a set of changes made together.
-
Compare with Activity Stream: Now that you have processed the
sys_audit
data, compare the count with the activity stream's count to ensure they match.
var ticketSysId = 'YOUR_TICKET_SYS_ID'; // Replace with actual ticket sys_id
var auditGr = new GlideRecord('sys_audit');
auditGr.addQuery('documentkey', ticketSysId);
auditGr.orderBy('sys_created_on'); // Order by timestamp
auditGr.query();
var changesByTimestamp = {};
while (auditGr.next()) {
var timestamp = auditGr.sys_created_on.toString();
if (!changesByTimestamp[timestamp]) {
changesByTimestamp[timestamp] = [];
}
changesByTimestamp[timestamp].push(auditGr.getValue('fieldname'));
}
var consolidatedCount = Object.keys(changesByTimestamp).length;
gs.info('Consolidated Change Count: ' + consolidatedCount);
Was this answer helpful?
Please consider marking it correct or helpful.
Your feedback helps us improve!
Thank you!
Regards,
Amit Gujrathi
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-05-2023 11:31 PM
HI @Bernie_BeAr ,
I trust you are doing great.
Here’s a high-level approach:
-
Query the
sys_audit
Table: Fetch records from thesys_audit
table for the specific ticket. You can filter based on thedocumentkey
field, which links to the ticket ID. -
Group Changes by Timestamp: The
sys_audit
table includes a timestamp for each change. Group changes that occur at the same timestamp. This grouping will mimic the activity stream's behavior of consolidating changes made in a single update. -
Count the Grouped Changes: Count each group of changes as one record, similar to how the activity stream counts a set of changes made together.
-
Compare with Activity Stream: Now that you have processed the
sys_audit
data, compare the count with the activity stream's count to ensure they match.
var ticketSysId = 'YOUR_TICKET_SYS_ID'; // Replace with actual ticket sys_id
var auditGr = new GlideRecord('sys_audit');
auditGr.addQuery('documentkey', ticketSysId);
auditGr.orderBy('sys_created_on'); // Order by timestamp
auditGr.query();
var changesByTimestamp = {};
while (auditGr.next()) {
var timestamp = auditGr.sys_created_on.toString();
if (!changesByTimestamp[timestamp]) {
changesByTimestamp[timestamp] = [];
}
changesByTimestamp[timestamp].push(auditGr.getValue('fieldname'));
}
var consolidatedCount = Object.keys(changesByTimestamp).length;
gs.info('Consolidated Change Count: ' + consolidatedCount);
Was this answer helpful?
Please consider marking it correct or helpful.
Your feedback helps us improve!
Thank you!
Regards,
Amit Gujrathi