Skip to main content

Notifications

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

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    Re: Who Deleted That Record?

    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

  • Jim Brower Profile Picture
    Jim Brower 65 on at
    Re: Who Deleted That Record?

    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

  • Yaniv Arditi Profile Picture
    Yaniv Arditi 3,990 on at
    Re: Who Deleted That Record?

    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

  • Leon Tribe Profile Picture
    Leon Tribe 7,034 on at
    Re: Who Deleted That Record?
    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/

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

Announcing Our 2025 Season 1 Super Users!

A new season of Super Users has arrived, and we are so grateful for the daily…

Vahid Ghafarpour – Community Spotlight

We are excited to recognize Vahid Ghafarpour as our February 2025 Community…

Congratulations to the January Top 10 leaders!

Check out the January community rock stars...

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 292,160 Super User 2025 Season 1

#2
Martin Dráb Profile Picture

Martin Dráb 230,962 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans