Creating a data audit for CRM is easy. But there's one thing I still haven't figured out. How do you know who deleted a record?
The ModifiedBy attribute is not updated on a delete, so you can't use that. Within a custom workflow, the WorkflowContext.UserId is the owner of the workflow, not the person who did whatever triggered the workflow.
I just want to log who it was that deleted a record. Sounds easy, but I'm stumped. Can anyone enlighten me?
*This post is locked for comments
You may want to take a look on an ISV product called Data Audit for Microsoft Dynamics CRM 4.0.
http://www.solomon.com.hk/products/data-audit-for-dynamics-crm-4-0
Thanks, Yaniv. That was just the nudge I needed. My mistake was trying to do this within a workflow, when a plug-in provided the information I needed. Well, sort of.
With plug-ins, the context.UserId is the user who did whatever triggered the event that the plug-in is handling (unlike workflows, where the context.UserId is the owner of the workflow). This is exactly what I needed. But, as usual, the solution to one problem caused yet another....
Unlike Create and Update events, which pass the entitity being created/updated in the context parameter, Delete events pass only a moniker--basically the ID of the entity being deleted. So, to get any information out of the record being deleted, you have to first retrieve that record, using the ID contained in the moniker. For this to work, the plug-in must be registered as pre-stage; if registered with the default post-stage, the record is already gone and no information is available.
Here's the final code for simple logging of deleted leads:
Public Class LogDeletedLead
Implements IPlugin
Public Sub Execute(ByVal context As Microsoft.Crm.Sdk.IPluginExecutionContext) _
Implements Microsoft.Crm.Sdk.IPlugin.Execute
Try
Dim service As ICrmService = context.CreateCrmService(True)
' Retrieve the lead entity that is to be deleted
Dim mon As Moniker = CType(context.InputParameters.Properties("Target"), Moniker)
Dim cols As New ColumnSet(New String() {"firstname", "lastname"})
Dim targ As New TargetRetrieveLead()
targ.EntityId = mon.Id
Dim req As New RetrieveRequest
req.Target = targ
req.ColumnSet = cols
Dim resp As RetrieveResponse = CType(service.Execute(req), RetrieveResponse)
Dim ld As lead = CType(resp.BusinessEntity, lead)
' Set up the lookup variable for the User Id
Dim lookup As New Lookup
lookup.Value = context.UserId
lookup.type = EntityName.systemuser.ToString
' Set up a new dynamic entity with the information to be logged
Dim debuglog As New DynamicEntity()
debuglog.Name = "new_debuglog"
debuglog.Properties = New PropertyCollection()
debuglog.Properties.Add(New StringProperty("new_category", "Lead Deleted"))
debuglog.Properties.Add(New StringProperty("new_firstname", ld.firstname))
debuglog.Properties.Add(New StringProperty("new_lastname", ld.lastname))
debuglog.Properties.Add(New CrmDateTimeProperty("new_deletedon", _
CrmTypes.CreateCrmDateTime(Now.ToString)))
debuglog.Properties.Add(New LookupProperty("new_deletedbyid", lookup))
' Set up the create request
Dim targ2 As New TargetCreateDynamic
targ2.Entity = debuglog
Dim create As New CreateRequest
create.Target = targ2
' Create the new log entry
Dim created As CreateResponse = CType(service.Execute(create), _
CreateResponse)
Catch ex As System.Web.Services.Protocols.SoapException
Throw New InvalidPluginExecutionException( _
"An error occurred in the LogDeletedLead plug-in.", ex)
End Try
End Sub
End Class
Hi Jim,
I don't see a way in which the workflow can help you in this case.
I would use a plug-in mapped to the entity pre-delete event. It will give you more context related detailed, such as the user who initiated the request.
Hope this helps,
Yaniv
André Arnaud de Cal...
292,160
Super User 2025 Season 1
Martin Dráb
230,962
Most Valuable Professional
nmaenpaa
101,156