Just sharing a sample code that runs on the Activation of case (incident) record and updates one field in it.
We need to register the plugin in both SetState and SetStateDynamicEntity message. Interestingly what we found was the plugin runs perfectly even if it is registered only for SetStateDynamicEntity. It triggers properly either we update it from CRM UI or through the CRM SDK for SetStateDynamicEntity message.
protected override void ExecuteCrmPlugin(LocalPluginContext localContext) { if (localContext == null) { throw new InvalidPluginExecutionException("localContext"); } // TODO: Implement your custom Plug-in business logic. var context = localContext.PluginExecutionContext; var tracingService = localContext.TracingService; if (context.InputParameters.Contains("EntityMoniker")) { tracingService.Trace("Inside Entity Moniker"); var targetEntity = (EntityReference)context.InputParameters["EntityMoniker"]; if (targetEntity.LogicalName == "incident") { tracingService.Trace("Inside Incident"); int state = ((OptionSetValue)context.InputParameters["State"]).Value; tracingService.Trace("State = " + state); // if status is active if (state == 0) { tracingService.Trace("State Active"); // update one field in the record Entity incident = new Entity("incident"); incident.Id = context.PrimaryEntityId;incident.Attributes["sab_fash_casestatusid"] = new EntityReference("sab_fashioncasestatus", new Guid("6F456BDC-CD7D-E811-813C-5065F38B15F2")); localContext.OrganizationService.Update(incident); tracingService.Trace("Record Updated"); } } } } }
Hope it helps..
One thought on “Sample Plugin code to run on Reactivate Case (Incident) in Dynamics 365”