Query All Incidents That A User Has Touched
Management is interested to find a list of Incidents that a specific user (analyst) has touched, even if the Incident was not assigned to them.
Is it possible to do this via a SQL script against the Data Warehouse?
"Is there any way to see what tickets were modified by a specific user within SCSM? Not just ones they created, but any they made edits to, either moved them queues, or added comments to tickets that weren't in their name? They are an analyst."
Best Answers
-
Conner_Wood Customer Ninja IT Monkey ✭✭✭✭Wubba Lubba Dub Dub!
DECLARE @UserToAudit nvarchar(255) = 'DOMAIN\SUPERCOOLUSER'; WITH USER_TRANSACTIONS AS ( SELECT [EntityTransactionLogId] ,[ContextGenerated] ,[TimeAdded] FROM [ServiceManager].[dbo].[EntityTransactionLog] (nolock) WHERE ContextGenerated LIKE @UserToAudit ) ,HistoryToAudit AS ( --Incident Class Properties. You may want to add your own extended incident classes as well SELECT [EntityChangeLogId] ,[ChangeType] ,[EntityTransactionLogId] ,[EntityId] FROM [ServiceManager].[dbo].[EntityChangeLog] ECL (nolock) JOIN [ServiceManager].[dbo].[ManagedType] MT (nolock) ON ECL.[EntityTypeId] = MT.[ManagedTypeId] AND MT.[TypeName] IN ('System.WorkItem.Incident') AND ECL.RelationshipId IS NULL UNION ALL --WorkItem/Incident Relationships, the ones you'd want to know on an incident if touched (Affected User, Assigned To, Primary Owner, etc.) SELECT [EntityChangeLogId] ,[ChangeType] ,[EntityTransactionLogId] ,[EntityId] FROM [ServiceManager].[dbo].[EntityChangeLog] ECL (nolock) JOIN [ServiceManager].[dbo].[BaseManagedEntity] BME (nolock) ON BME.[BaseManagedEntityId] = ECL.[EntityId] JOIN [ServiceManager].[dbo].[ManagedType] MT (nolock) ON BME.[BaseManagedTypeId] = MT.[ManagedTypeId] AND MT.[TypeName] IN ('System.WorkItem.Incident') JOIN [ServiceManager].[dbo].[RelationshipType] RT (nolock) ON ECL.[RelationshipId] IS NOT NULL AND RT.[RelationshipTypeId] = ECL.[EntityTypeId] AND RT.[RelationshipTypeName] IN ('System.WorkItemHasCommentLog' ,'System.WorkItem.TroubleTicketHasAnalystComment' ,'System.WorkItem.TroubleTicketHasUserComment' ,'System.WorkItemHasActionLog' ,'System.WorkItem.TroubleTicketHasActionLog' ,'System.WorkItemAffectedUser' ,'System.WorkItemAssignedToUser' ,'System.WorkItemCreatedByUser' ,'System.WorkItemHasParentWorkItem' ,'System.WorkItemClosedByUser' ,'System.WorkItemAboutConfigItem' -- This is used by both Affected Services and Affected Items ,'System.WorkItemRelatesToConfigItem' ,'System.WorkItemRelatesToWorkItem' ,'System.WorkItemContainsActivity' ,'System.WorkItemHasFileAttachment' ,'System.WorkItem.TroubleTicketResolvedByUser' ,'System.WorkItem.TroubleTicketClosedByUser' ,'System.WorkItem.IncidentPrimaryOwner') ) SELECT DISTINCT HTA.[ChangeType] ,IR.[Id_9A505725_E2F2_447F_271B_9B9F4F0D190C] AS 'ID' ,IR.[DisplayName] AS 'Incident Display Name' ,UT.[ContextGenerated] AS 'Audited User' ,FORMAT(DATEADD(mi, DATEDIFF(mi, GETUTCDATE(), GETDATE()), UT.[TimeAdded]), 'MM/dd/yyyy h:mm:ss tt') AS 'Touched Date (Local Time)' FROM HistoryToAudit HTA JOIN USER_TRANSACTIONS UT ON UT.[EntityTransactionLogId] = HTA.[EntityTransactionLogId] JOIN [ServiceManager].[dbo].[MTV_System$WorkItem$Incident] IR (nolock) ON IR.[BaseManagedEntityId] = HTA.[EntityId] ORDER BY [Touched Date (Local Time)] DESC
It's messy but it gets the general idea across.5 -
Conner_Wood Customer Ninja IT Monkey ✭✭✭✭Well, I admit I was using SQL 2012, so didn't think about you not having that functionality..... in that case you might need to find another way to get UTC Date Time to Local Time in a preferable format.
Maybe try using CONVERT on DATETIME.
See if you can replace that line with the following instead., REPLACE(convert(varchar, DATEADD(mi, DATEDIFF(mi, GETUTCDATE(), GETDATE()), UT.[TimeAdded]), 101) + SUBSTRING(convert(varchar, DATEADD(mi, DATEDIFF(mi, GETUTCDATE(), GETDATE()), UT.[TimeAdded]), 22), 9 , 20) , ' ',' ') AS 'Touched Date (Local Time)'
Glad I helped, Good Luck.
5
Answers
I mine as well post the code snippets, it's more to show how MS structured the history logs.....
Jeez..... ok, enough of that, luckily there are some posts out there:
Entity Change Log The Service Manager CMDB does a great job of tracking all of the changes made to it
So yes, you could get the history and as long as it hasn't been groomed yet you will be able to track any changes made to a ticket!
select * from EntityTransactionLog where ????
But I'm clueless as to how I would search that log for a particular user in AD. Any help here?
Although I did just find an interesting bit of C# code of mine I forgot about that has some SQL.....
The main purpose was "Get a list of certain tickets with the most recent user who modified it along with the date they modified it"
You best start reading about The Log Tables section, it goes over what to look for and explains what certain values mean...
Good news @Chris_Keander it looks like I might be making a similar query afterall.....
It's messy but it gets the general idea across.
I entered the domain\username at the top of the script and executed it but immediately got this error:
Msg 195, Level 15, State 10, Line 67
'FORMAT' is not a recognized built-in function name.
Line 67 is: ,FORMAT(DATEADD(mi, DATEDIFF(mi, GETUTCDATE(), GETDATE()), UT.[TimeAdded]), 'MM/dd/yyyy h:mm:ss tt') AS 'Touched Date (Local Time)'
Any suggestions?
That's strange as FORMAT was introduced in SQL Server 2012. What version of SQL Server are you using and what compatibility level is the database at (right-click on the Service Manager and choose Properties and then Options) ? If the compatibility level is 110 or higher then FORMAT should be available.
Product Name: SQL Server 2008 R2
Product Level: SP3
Product Edition: Standard Edition (64-bit)
Maybe try using CONVERT on DATETIME.
See if you can replace that line with the following instead.
Glad I helped, Good Luck.