【问题标题】:Retrieve triggering an update in plugin检索触发插件中的更新
【发布时间】:2016-07-21 19:26:41
【问题描述】:

我在InvoiceDetailUpdate(预操作)上有一个插件,我在其中检索关联的Invoice 以从中获取更多信息(即:选择的税务资料在发票级别)在 CRM 2016 中。

我是这样做的:

//xrmObjects is an object containing all useful objects in plugins/workflow...
var invoice = RetrieveEntity(xrmObjects.Service, xrmObjects.TracingService, image["invoiceid"] as EntityReference, new ColumnSet("invoiceid", "pricelevelid", "customerid", "opportunityid", "xtc_tax_definition"));

上面这行特定的代码在InvoiceDetail 上触发另一个Update

这是上面调用的方法:

public static Entity RetrieveEntity(IOrganizationService service, ITracingService tracingService, EntityReference target, ColumnSet columnSet)
{

    Entity entity = new Entity();

    try
    {
        entity = CrmServiceExtensions.ExecuteWithRetry<RetrieveResponse>(service, new RetrieveRequest
        {
            Target = target,
            ColumnSet = columnSet

        }).Entity;
    }
    catch (Exception ex)
    {
       tracingService.Trace($"Error retrieving {target.LogicalName}: {ex.Message}");
       throw;
    }

    return entity;

}

这里是ExecuteWithRetry

public static T ExecuteWithRetry<T>(IOrganizationService service, OrganizationRequest request)
            where T : OrganizationResponse
        {
            T response = null;
            int i = 0;

            // Maximum of five iterations.
            while (i < 5)
            {
                try
                {
                    response = (T)service.Execute(request);

                    // If the Execute does not throw an Exception, break the loop
                    break;
                }
                catch (System.Web.Services.Protocols.SoapException e)
                {
                    // Retry if the SoapException is a "Generic SQL Error",
                    // otherwise rethrow the SoapException.
                    // "Generic SQL Error" might indicate a deadlock.
                    if (e.Detail.InnerText.ToLower().Contains("generic sql error"))
                    {
                        ++i;
                        // Wait (sleep thread) for i * 1000 milliseconds.
                        // So, first iteration waits 1 second,
                        // while fifth iteration will wait 5 seconds.
                        System.Threading.Thread.Sleep(i * 1000);
                    }
                    else throw;
                }
            }

            if (i >= 5)
            {
                throw new Exception("ExecuteWithRetry: too many retries");
            }

            return response;
        }

我已经确认没有发生任何奇怪的事情,InvoiceDetail 上的更新消息在response = (T)service.Execute(request); 行再次触发

我也尝试使用早期绑定和上下文来检索发票,但加载发票的 LoadProperty 方法做同样的事情....

using (XrmServiceContext ctx = new XrmServiceContext(xrmObjects.Service))
{

    Xrm.InvoiceDetail image = xrmObjects.PluginContext.PreEntityImages["invoicedetail"].ToEntity<Xrm.InvoiceDetail>();

    try
    {
        ctx.LoadProperty(image, "invoice_details");
    }
    catch (Exception ex)
    {
        throw new InvalidPluginExecutionException($"Error retrieving invoice details' invoice: {ex.Message}");
    }

}

我在步骤配置中看不到任何可以执行此操作的内容。有什么想法吗?

【问题讨论】:

  • 我假设您已经完成了显而易见的操作并检查了 Invoice Retrieve 消息中没有插件?
  • @jasonscript 实际上,我没有为该消息和该实体配置工作流/插件(实际上没有为 Invoice 配置)
  • 那么在发票明细更新期间对发票执行查询会导致另一个发票明细插件更新触发器发生?
  • @Daryl 似乎如此。但我没有任何关于 Invoice - Retrieve 的配置。如果您手头有一个 CRM2016 实例,您会得到相同的行为吗?

标签: c# .net dynamics-crm-2011 dynamics-crm-2016


【解决方案1】:

我没有使用LoadProperty,而是像这样手动检索发票

var invoice = ctx.InvoiceSet.SingleOrDefault(x => x.Id == image.InvoiceId.Id);

代替:

ctx.LoadProperty(image, "invoice_details");

出于某种原因,LoadProperty 正在触发关于子发票详细信息的不需要的更新消息...

【讨论】:

    猜你喜欢
    • 2022-01-18
    • 2013-04-05
    • 1970-01-01
    • 2021-03-08
    • 2015-08-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多