locked
CRM2013. C#. Activate, Update and Deactivate case. RRS feed

  • Question

  • Hi.

    Have a plugin which starts on Email PreCreate. Plugin must activate the email regarding Case, update some field in the Case and deactivate the Case back.

    Here is the plugin code:

    IPluginExecutionContext context = localContext.PluginExecutionContext;
    IOrganizationService service = localContext.OrganizationService;
    
    if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)
    {
        Entity entity = (Entity)context.InputParameters["Target"];
    
        try
        {
            var regardingCaseId = ((EntityReference)entity.Attributes["regardingobjectid"]).Id;
            Entity regardingCase = service.Retrieve("incident", regardingCaseId, new ColumnSet(true));
            int status = ((OptionSetValue)regardingCase.Attributes["statuscode"]).Value;
            if (status == 5)
            {
                
                // Activate case
                SetStateRequest setStateRequest = new SetStateRequest()
                {
                    EntityMoniker = new EntityReference
                    {
                        Id = regardingCaseId,
                        LogicalName = "incident",
                    },
                    State = new OptionSetValue(0),
                    Status = new OptionSetValue(1)
                };
                service.Execute(setStateRequest);
                
                // UPDATE Add some changes
                regardingCase["description"] = "xxxxx";
                service.Update(regardingCase);
    
                // Deactivate case
                setStateRequest = new SetStateRequest()
                {
                    EntityMoniker = new EntityReference
                    {
                        Id = regardingCaseId,
                        LogicalName = "incident",
                    },
                    State = new OptionSetValue(1),
                    Status = new OptionSetValue(5)
                };
                service.Execute(setStateRequest);
            }
        }
        catch
        {
            return;
        }
    }

    In result it throws error:

    Unhandled Exception: System.ServiceModel.FaultException`1[[Microsoft.Xrm.Sdk.OrganizationServiceFault, Microsoft.Xrm.Sdk, Version=6.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35]]: There is no active transaction. This error is usually caused by custom plug-ins that ignore errors from service calls and continue processing.Detail: 
    <OrganizationServiceFault xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.microsoft.com/xrm/2011/Contracts">
      <ErrorCode>-2147220911</ErrorCode>
      <ErrorDetails xmlns:d2p1="http://schemas.datacontract.org/2004/07/System.Collections.Generic" />
      <Message>There is no active transaction. This error is usually caused by custom plug-ins that ignore errors from service calls and continue processing.</Message>
      <Timestamp>2014-07-09T10:25:31.5511394Z</Timestamp>
      <InnerFault i:nil="true" />
      <TraceText i:nil="true" />
    </OrganizationServiceFault>

    But plugin completes fine and activate the case if I remove Update step and Deactivate step.

    Why it does not work with update and deactivate steps? And how to fix it?

    Thanks!



    • Edited by Dismantled Wednesday, July 9, 2014 10:51 AM
    Wednesday, July 9, 2014 10:45 AM

Answers

  • try to retrieve again the case after you activate it, so the code will be

    // ...
    service.Execute(setStateRequest);
    
    // retrieve again the now active case
    regardingCase = service.Retrieve("incident", regardingCaseId, new ColumnSet(true));
    
    // UPDATE Add some changes
    regardingCase["description"] = "xxxxx";
    service.Update(regardingCase);
    
    // ...

    this because when you update it your entity was containing old values for statecode and statuscode.


    My blog: www.crmanswers.net - Rockstar 365 Profile

    • Marked as answer by Dismantled Wednesday, July 9, 2014 12:25 PM
    Wednesday, July 9, 2014 11:22 AM
  • try with a CloseIncidentRequest, here an example

                Entity incidentResolution = new Entity("incidentresolution");
                incidentResolution["subject"] = "Incident resolved";
                incidentResolution["incidentid"] = new EntityReference("incident", regardingCaseId);
    
                // Close the incident with the resolution.
                CloseIncidentRequest closeIncidentRequest = new CloseIncidentRequest
                {
                    IncidentResolution = incidentResolution,
                    Status = new OptionSetValue(5)
                };
                service.Execute(closeIncidentRequest);


    My blog: www.crmanswers.net - Rockstar 365 Profile



    Wednesday, July 9, 2014 12:03 PM

All replies

  • try to retrieve again the case after you activate it, so the code will be

    // ...
    service.Execute(setStateRequest);
    
    // retrieve again the now active case
    regardingCase = service.Retrieve("incident", regardingCaseId, new ColumnSet(true));
    
    // UPDATE Add some changes
    regardingCase["description"] = "xxxxx";
    service.Update(regardingCase);
    
    // ...

    this because when you update it your entity was containing old values for statecode and statuscode.


    My blog: www.crmanswers.net - Rockstar 365 Profile

    • Marked as answer by Dismantled Wednesday, July 9, 2014 12:25 PM
    Wednesday, July 9, 2014 11:22 AM
  • Thanks Guido!

    This helps for UPDATE step without Deactivate step. With Deactivate it throws the same error.

    Tested on this code:

    SetStateRequest setStateRequest = new SetStateRequest()
                            {
                                EntityMoniker = new EntityReference
                                {
                                    Id = regardingCaseId,
                                    LogicalName = "incident",
                                },
                                State = new OptionSetValue(0),
                                Status = new OptionSetValue(1)
                            };
                            service.Execute(setStateRequest);
    
                            // retrieve again the now active case
                            regardingCase = service.Retrieve("incident", regardingCaseId, new ColumnSet(true));
                            
                            regardingCase["description"] = "xxxxx";
                            service.Update(regardingCase);
    
                            // retrieve again the updated case
                            regardingCase = service.Retrieve("incident", regardingCaseId, new ColumnSet(true));
    
                            setStateRequest = new SetStateRequest()
                            {
                                EntityMoniker = new EntityReference
                                {
                                    Id = regardingCaseId,
                                    LogicalName = "incident",
                                },
                                State = new OptionSetValue(1),
                                Status = new OptionSetValue(5)
                            };
                            service.Execute(setStateRequest);

    Wednesday, July 9, 2014 11:48 AM
  • try with a CloseIncidentRequest, here an example

                Entity incidentResolution = new Entity("incidentresolution");
                incidentResolution["subject"] = "Incident resolved";
                incidentResolution["incidentid"] = new EntityReference("incident", regardingCaseId);
    
                // Close the incident with the resolution.
                CloseIncidentRequest closeIncidentRequest = new CloseIncidentRequest
                {
                    IncidentResolution = incidentResolution,
                    Status = new OptionSetValue(5)
                };
                service.Execute(closeIncidentRequest);


    My blog: www.crmanswers.net - Rockstar 365 Profile



    Wednesday, July 9, 2014 12:03 PM
  • Sure! Closing of Incident required creation of the Incident Resolution record.
    Guido it works perfect. Thank you a lot!

    Wednesday, July 9, 2014 12:25 PM
  • Hi Guido,

    I'm trying to use your solution but i keep getting the same error.

    "There is no active transaction. This error is usually caused by custom plug-ins that ignore errors from service calls and continue processing"

    service object it's from which type?

    I'm working with OrganizationServiceProxy.

    Thanks

    Wednesday, April 20, 2016 1:48 PM
  • Thanks for this!
    Friday, June 10, 2016 9:44 AM