Hello,
Have a plugin starts on PreOp Email Create.
Plugin needs to retrieve email regarding case, check the case state, activate the case if it closed, update some case data and resolve case back.
The plugin works perfect if I manually create the Email. But have a problem with the automated incoming emails. Plugin stuck on the try to activate the Case. After incorrect plugin execution I could see some strangeness - the email attributes such as TO, FROM, CC are empty! And the Activity Feed of the regarding case becomes empty.
In the code below you can see the EventLog Steps. Plugin stuck after the STEP3 log message. And did not activate the Case. And besides all previous steps logged fine and shows the correct regardingCaseId value.
Any suggestions on my issue are very appreciated.
Here is the code:
public class PreEmailCreate122: Plugin { public PreEmailCreate122() : base(typeof(PreEmailCreate122)) { base.RegisteredEvents.Add(new Tuple<int, string, string, Action<LocalPluginContext>>(20, "Create", "email", new Action<LocalPluginContext>(ExecutePreEmailCreate122))); } // Activate Case void ActivateCase(IOrganizationService service, Guid regardingCaseId) { // StepLog EventLog.WriteEntry("CRMPlugin","STEP3: " + regardingCaseId.ToString(), EventLogEntryType.Error ); // Activate Case SetStateRequest setStateRequest = new SetStateRequest() { EntityMoniker = new EntityReference { Id = regardingCaseId, LogicalName = "incident", }, State = new OptionSetValue(0), Status = new OptionSetValue(1) }; service.Execute(setStateRequest); } // Resolve Case void CloseCase(IOrganizationService service, Guid regardingCaseId) { // StepLog EventLog.WriteEntry("CRMPlugin","STEP4: " + regardingCaseId.ToString(), EventLogEntryType.Error ); // Create incidentresolution Entity incidentResolution = new Entity("incidentresolution"); incidentResolution["subject"] = "Incident resolved"; incidentResolution["incidentid"] = new EntityReference("incident", regardingCaseId); // Close the case with incidentresolution CloseIncidentRequest closeIncidentRequest = new CloseIncidentRequest { IncidentResolution = incidentResolution, Status = new OptionSetValue(5) }; service.Execute(closeIncidentRequest); } protected void ExecutePreEmailCreate122(LocalPluginContext localContext) { if (localContext == null) { throw new ArgumentNullException("localContext"); } IPluginExecutionContext context = localContext.PluginExecutionContext; IOrganizationService service = localContext.OrganizationService; if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity && context.Depth < 2) { Entity entity = (Entity)context.InputParameters["Target"]; try { // Retrive regarding case and status var regardingCaseId = ((EntityReference)entity.Attributes["regardingobjectid"]).Id; Entity regardingCase = service.Retrieve("incident", regardingCaseId, new ColumnSet(true)); int caseStatus = ((OptionSetValue)regardingCase.Attributes["statuscode"]).Value; // StepLog EventLog.WriteEntry("CRMPlugin","STEP1: " + regardingCaseId.ToString(), EventLogEntryType.Error ); // Check condition of the mail direction and regarding case status if ((bool)entity.Attributes["directioncode"] == false && caseStatus == 5) { EventLog.WriteEntry("CRMPlugin","STEP2: " + regardingCaseId.ToString(), EventLogEntryType.Error ); // Activate the case ActivateCase(service, regardingCaseId); // Update case regardingCase = service.Retrieve("incident", regardingCaseId, new ColumnSet(true)); regardingCase["dt_customerrating"] = "Cool!"; service.Update(regardingCase); // Resolve the case CloseCase(service, regardingCaseId); } return; } /*catch (FaultException ex) { var message = ex.Message; if (ex.InnerException != null) message += "InnerExcwption: " + ex.InnerException.Message; throw new InvalidPluginExecutionException("Plugin error: " + message, ex); }*/ catch { return; } } } }