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

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Customer experience | Sales, Customer Insights,...
Suggested Answer

Get value on Delete Step - Plugin

(0) ShareShare
ReportReport
Posted on by 15

Hi,

Trying to do just a sample plugin triggered on delete of a record, to get an attribute within a deleted record and update it into another random record and field, however it seems like my plugin doesn't even get triggered - enabled Plug in traces and don't see anything there; also registered the pre-image on the lastname attribute.

Here is my sample plugin, let me know what I am doing wrong here:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Xrm.Sdk;
using System.ServiceModel;
using Microsoft.Xrm.Sdk.Query;

namespace MyPlugins
{
public class BeforeDelete_Update : IPlugin
{
public void Execute(IServiceProvider serviceProvider)
{
// Extract the tracing service for use in debugging sandboxed plug-ins.
// If you are not registering the plug-in in the sandbox, then you do
// not have to add any tracing service related code.
ITracingService tracingService =
(ITracingService)serviceProvider.GetService(typeof(ITracingService));

// Obtain the execution context from the service provider.
IPluginExecutionContext context = (IPluginExecutionContext)
serviceProvider.GetService(typeof(IPluginExecutionContext));

// Obtain the organization service reference which you will need for
// web service calls.
IOrganizationServiceFactory serviceFactory =
(IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);

// The InputParameters collection contains all the data passed in the message request.
if (context.InputParameters.Contains("Target") &&
context.InputParameters["Target"] is Entity)
{
// Obtain the target entity from the input parameters.
Entity lead = (Entity)context.InputParameters["Target"];

try
{
// Plug-in business logic goes here.

Entity preImageLead = (Entity)context.PreEntityImages["PreImageLead"];
string oldLastname = preImageLead.Attributes["lastname"].ToString();

var retrievedLead = new Entity("lead", new Guid("19a38ae0-4d0e-ea11-a813-000d3a1bbd52"));

var newLead = new Entity("lead");
newLead.Id = retrievedLead.Id;
newLead["address1_line3"] = oldLastname ;
service.Update(newLead);
}

catch (FaultException<OrganizationServiceFault> ex)
{
throw new InvalidPluginExecutionException("An error occurred in MyPlug-in - BeforeDelete_Update", ex);
}

catch (Exception ex)
{
tracingService.Trace("MyPlugin: {0}", ex.ToString());
throw;
}
}
}
}
}

I have the same question (0)
  • Suggested answer
    Guido Preite Profile Picture
    54,086 Moderator on at

    the problem is with this line

    if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity) {

    in a delete step the Target is not an Entity, is an EntityReference

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

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > Customer experience | Sales, Customer Insights, CRM

#1
Tom_Gioielli Profile Picture

Tom_Gioielli 83 Super User 2025 Season 2

#2
Gerardo Rentería García Profile Picture

Gerardo Rentería Ga... 49 Most Valuable Professional

#3
#ManoVerse Profile Picture

#ManoVerse 40

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans