在CRM 2011插件中禁用Order上的价格订单时,需要在提交前启动事务

时间:2014-02-17 12:59:40

标签: c# dynamics-crm-2011 crm

我写了一个执行此操作的插件:

tracingService.Trace("Retrieving Order");
//retrieve the entity order as the input Entity
//get both the copies of the entity, and get the updated fields from the new one
var entity = (Entity)context.InputParameters["Target"];
var oldEntity = Helpers.ActualEntity(new EntityReference("salesorder", entity.Id), service);

tracingService.Trace("Before cast");
//Early bound entity
var order = new salesorder(entity);
var oldOrder = new salesorder(oldEntity);


//if the orderprice is not locked return
if ((order.ispricelocked == false) || (oldOrder.ispricelocked == false))
    return;
else
//if the orderprice is locked, unlock it
{
    var req = new UnlockSalesOrderPricingRequest();
    req.SalesOrderId = order.Id;
    var resp = (UnlockSalesOrderPricingResponse)service.Execute(req);
}

当我执行它时,我得到:

  

未处理的异常:System.ServiceModel.FaultException 1[[Microsoft.Xrm.Sdk.OrganizationServiceFault, Microsoft.Xrm.Sdk, Version=5.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35]]: Need to start a transaction before commitDetail: <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"> <KeyValuePairOfstringanyType> <d2p1:key>CallStack</d2p1:key> <d2p1:value xmlns:d4p1="http://www.w3.org/2001/XMLSchema" i:type="d4p1:string"> at Microsoft.Crm.Extensibility.VersionedPluginProxyStepBase.Execute(PipelineExecutionContext context) at Microsoft.Crm.Extensibility.Pipeline.Execute(PipelineExecutionContext context) at Microsoft.Crm.Extensibility.MessageProcessor.Execute(PipelineExecutionContext context) at Microsoft.Crm.Extensibility.InternalMessageDispatcher.Execute(PipelineExecutionContext context) at Microsoft.Crm.Extensibility.ExternalMessageDispatcher.ExecuteInternal(IInProcessOrganizationServiceFactory serviceFactory, IPlatformMessageDispatcherFactory dispatcherFactory, String messageName, String requestName, Int32 primaryObjectTypeCode, Int32 secondaryObjectTypeCode, ParameterCollection fields, CorrelationToken correlationToken, CallerOriginToken originToken, UserAuth userAuth, Guid callerId, Guid transactionContextId, Int32 invocationSource, Nullable 1 requestId,Version endpointVersion)          at Microsoft.Crm.Extensibility.OrganizationSdkServiceInternal.ExecuteRequest(OrganizationRequest请求,CorrelationToken correlationToken,CallerOriginToken callerOriginToken,WebServiceType serviceType,UserAuth userAuth,Guid targetUserId,Boolean traceRequest,OrganizationContext context,Boolean returnResponse)          在Microsoft.Crm.Extensibility.OrganizationSdkServiceInternal.ExecuteRequest(OrganizationRequest请求,CorrelationToken correlationToken,CallerOriginToken callerOriginToken,WebServiceType serviceType)          在Microsoft.Crm.Extensibility.OrganizationSdkServiceInternal.Execute(OrganizationRequest请求,CorrelationToken correlationToken,CallerOriginToken callerOriginToken,WebServiceType serviceType)                             需要在提交之前启动事务         2014-02-17T12:56:33.4886464Z                         

我检查了CRM跟踪,我得到的是:

Crm异常:消息:需要在提交之前启动事务,ErrorCode:-2147220911

你以前见过吗?

2 个答案:

答案 0 :(得分:2)

我知道两种可能的解释:

  1. 你正在吞咽FaultException
  2. FaultException 抛出时,执行会从交易中退出,所以如果你吞下它(即你对失败感到满意,只想继续)你就是不再允许使用IOrganizationService对象(并且您获得的消息是CRM必须告诉您的方式)。

    1. 你错过了什么
    2. 确保所有主要字段都填写在您正在处理的记录中以及所有最终相关字段中。相关记录中的 null 名称经常使CRM蓬勃发展。

答案 1 :(得分:0)

我可以告诉你问题出在哪里

  var Order = new salesorder(entity);

因此需要先创建它,然后再处理进一步的请求。

检查是否

      Order.Id is null or Guid.Empty  

首先必须创建订单实体。