locked
CRM2013. Incorrect plugin execution. RRS feed

  • Question

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

    Sunday, July 13, 2014 3:26 PM

Answers

  • Problem solved.

    It was the recursion with the other real-time workflow which throws an exception at the update step.

    • Marked as answer by Dismantled Monday, July 14, 2014 4:53 PM
    Monday, July 14, 2014 4:53 PM

All replies

  • By logging and tracing I've found what the "service.Execute(setStateRequest);" does not make it job. And plugin throw an exception "Value cannot be null"  on the try to update the locked regarding case recrod.

            SetStateRequest setStateRequest = new SetStateRequest()
            {
                EntityMoniker = new EntityReference
                {
                    Id = regardingCaseId,
                    LogicalName = "incident",
                },
                State = new OptionSetValue(0),
                Status = new OptionSetValue(1)
            };
            service.Execute(setStateRequest);
    In what could be the issue?  The regardingCaseId value is provided and correct.

    Monday, July 14, 2014 11:17 AM
  • Hi,

    Since you're trying to close an incident, I would suggest trying the "CloseIncidentRequest". 'SetStateRequest', for some reason, doesn't seem to work well with Incidents.

    Sample can be found here.


    Admin QuikView Solution for CRM 2013

    Monday, July 14, 2014 11:35 AM
  • Problem solved.

    It was the recursion with the other real-time workflow which throws an exception at the update step.

    • Marked as answer by Dismantled Monday, July 14, 2014 4:53 PM
    Monday, July 14, 2014 4:53 PM