web
You’re offline. This is a read only version of the page.
close
Skip to main content

Announcements

No record found.

News and Announcements icon
Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

Who Deleted That Record?

(0) ShareShare
ReportReport
Posted on by 65

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

I have the same question (0)
  • Leon Tribe Profile Picture
    7,034 on at
    Thought of a potential solution, posted and then tested it. It didn't work, sorry. Leon Tribe Want to hear me talk about all things CRM? Check out my blog http://leontribe.blogspot.com/
  • Yaniv Arditi Profile Picture
    3,990 on at

    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

  • Jim Brower Profile Picture
    65 on at

    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

  • Community Member Profile Picture
    on at

    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

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Introducing the 2026 Season 1 community Super Users

Congratulations to our 2026 Super Stars!

Congratulations to our 2025 Community Spotlights

Thanks to all of our 2025 Community Spotlight stars!

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
JS-09031509-0 Profile Picture

JS-09031509-0 3

#2
AS-17030037-0 Profile Picture

AS-17030037-0 2

#2
Mark Eckert Profile Picture

Mark Eckert 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans