Quantcast
Channel: CRM Development forum
Viewing all articles
Browse latest Browse all 8223

CRM2013. C#. Activate, Update and Deactivate case.

$
0
0

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!




Viewing all articles
Browse latest Browse all 8223

Trending Articles