locked
resolving an incident in crm 2011 RRS feed

  • Question

  • hi! i am having a problem on resolving a case using retrieved guid from crm 2011.  i got  an error of "already closed or canceled". do you know any solutions to this? please advice me.

    here's my code:

    public class workflow : IPlugin
    {
        public void Execute(IServiceProvider serviceProvider)
        {
            IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
            IOrganizationServiceFactory factory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
            IOrganizationService service = factory.CreateOrganizationService(context.UserId);
    
            if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)
            {
                Entity entity = (Entity)context.InputParameters["Target"];
    
                if (entity.LogicalName != "incident") { return; }
    
                try
                {
                    Entity caseResolution = new Entity("incidentresolution");
    
                    Guid caseId = (Guid)entity.Attributes["incidentid"];
                    caseResolution["incidentid"] = new EntityReference("incident",
                        caseId);
                    caseResolution["subject"] = "Case resolved!";
    
                    CloseIncidentRequest close = new CloseIncidentRequest();
                    close.IncidentResolution = caseResolution;
                    close.RequestName = "CloseIncident";
                    close.Status = new OptionSetValue(5);
    
                    CloseIncidentResponse closeCase =
                        (CloseIncidentResponse)service.Execute(close);
    
                    service.Update(entity);
                }
    
                #region exception handler
                catch (Exception ex)
                {
                    throw new InvalidPluginExecutionException(String.Concat(
                        Environment.NewLine, "MESSAGE",
                        Environment.NewLine, "     >> ", ex.Message,
                        Environment.NewLine,
    
                        Environment.NewLine, "INNER EXCEPTION",
                        Environment.NewLine, "     >> ", ex.InnerException,
                        Environment.NewLine,
    
                        Environment.NewLine, "SOURCE",
                        Environment.NewLine, "     >> ", ex.Source,
                        Environment.NewLine,
    
                        Environment.NewLine, "STACK TRACE",
                        Environment.NewLine, "     >> ", ex.StackTrace,
                        Environment.NewLine));
                }
                #endregion
    
            } 
        } //serviceProvider
    } //workflow

    Plugin Registration

    Message: Update
    Event: Post-Operation

    THANKS!!





    • Edited by 9marco Thursday, March 29, 2012 8:42 AM
    Thursday, March 29, 2012 8:13 AM

Answers

  • hi Marco,

     your problem is the line service.Update

                    CloseIncidentResponse closeCase =
                        (CloseIncidentResponse)service.Execute(close);
    
                    service.Update(entity);
    

    You cant update the Case after the Case is Closed.

     As the Case becomes Read only , not further modification is allowed on a Closed case hence the  Error

    here is an example of closed case from SDK

    http://msdn.microsoft.com/en-us/library/hh547422.aspx

     hth

    dkay


     if the response answered your question, please take a minute and mark the response as an answer.

    Thursday, March 29, 2012 8:56 AM
  • i removed service.update in my code but still, im having the same error.
    anyways it works fine now. i changed the event to pre-op and then another error occured and says that it triggered an infinite loop. i have added a statement if(context.depth == 1) at the beginning of my code and it worked correctly.

    thanks for the help!


    • Edited by 9marco Friday, March 30, 2012 5:16 AM
    • Marked as answer by 9marco Friday, March 30, 2012 7:05 AM
    Friday, March 30, 2012 5:16 AM

All replies

  • hi Marco,

     your problem is the line service.Update

                    CloseIncidentResponse closeCase =
                        (CloseIncidentResponse)service.Execute(close);
    
                    service.Update(entity);
    

    You cant update the Case after the Case is Closed.

     As the Case becomes Read only , not further modification is allowed on a Closed case hence the  Error

    here is an example of closed case from SDK

    http://msdn.microsoft.com/en-us/library/hh547422.aspx

     hth

    dkay


     if the response answered your question, please take a minute and mark the response as an answer.

    Thursday, March 29, 2012 8:56 AM

  • Hi
     
    Did this help?
     
    Please make sure to mark as answer to the response that helped you get through. This will help others with similar problem identify the answer and also close this thread as resolved.
     
    Thanks
     
    Dkay
    Thursday, March 29, 2012 7:29 PM
  • hi,

    im still having dsame error message. it says that "already closed or canceled" but when i check, still the incident isnt closed. maybe its because of the registration? i used update as message, incident as primary entity, and event as post-op.

    Thanks,

    marco


    • Edited by 9marco Friday, March 30, 2012 2:24 AM
    Friday, March 30, 2012 2:22 AM
  • The issue is exactly as Crm_Developer described above.  Are you still attempting to update the Case after Closing it?  If so, the entire operation will fail and rollback.

    --pogo (pat) @ pogo69.wordpress.com

    Friday, March 30, 2012 3:36 AM
  • i removed service.update in my code but still, im having the same error.
    anyways it works fine now. i changed the event to pre-op and then another error occured and says that it triggered an infinite loop. i have added a statement if(context.depth == 1) at the beginning of my code and it worked correctly.

    thanks for the help!


    • Edited by 9marco Friday, March 30, 2012 5:16 AM
    • Marked as answer by 9marco Friday, March 30, 2012 7:05 AM
    Friday, March 30, 2012 5:16 AM