I'm creating a simple plugin in CRM 2011 to autopopulate Case closed date when a Case is resolved. For this I register my plugin in "SetState" and "SetStateDynamicEntity" of Incident entity. I created a new date field "new_closeddate" to contain the value.
For some reason my plugin never fires even though I registered it several times and did IISReset. Could anyone please guide me in the right direction? Thank you much for your help.
Here is my code below.
Thanks,
-elisabeth
public override void OnExecute(IServiceProvider serviceProvider, IPluginExecutionContext context)
{
var trace = (ITracingService)serviceProvider.GetService(typeof(ITracingService));
// The InputParameters collection contains all the data passed in the message request.
var targetReference = context.GetParameterCollection<EntityReference>(context.InputParameters, "EntityMoniker");
if (targetReference == null)
throw new InvalidPluginExecutionException(OperationStatus.Failed, "Target Entity cannot be null");
var state = (OptionSetValue)context.InputParameters["State"];
/* Only proceed if Incident is being set to Resolved */
if (state == null || state.Value != 1)
return;
var postImage = context.PostEntityImages["PostImage"];
if (postImage == null)
throw new InvalidPluginExecutionException(OperationStatus.Failed, "Post Image is required");
var serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
var service = serviceFactory.CreateOrganizationService(context.UserId);
Entity CaseIncident = new Entity("incident");
CaseIncident.Attributes = new AttributeCollection();
CaseIncident.Attributes.Add("new_closeddate", DateTime.Now);
}
protected override bool CanExecute(IPluginExecutionContext context)
{
return context.PreConditionCheck(
0,
new List<int> { MessageProcessingStage.AfterMainOperationInsideTransaction },
new List<string> { MessageName.Update },
new List<string> { Schema.Incident.EntityLogicalName });
}