
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;
}
}
}
}
}
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