【问题标题】:Dynamics CRM Plugin - Refresh rollup on deleteDynamics CRM 插件 - 删除时刷新汇总
【发布时间】:2016-11-25 14:03:58
【问题描述】:

我有需要处理的特定情况。我有一个插件,可以在创建或更新发票详细信息时刷新发票上的特定汇总字段。 现在我需要在删除发票详细信息时刷新该字段。


分析这个问题,我意识到我无法在操作前刷新汇总字段,因为尚未删除发票详细记录,而在操作后我无法从该特定记录中检索发票 Guid,因为它已经消失了。

这里是处理创建/更新时汇总刷新的代码:

Entity invoiceDetail = service.Retrieve("invoicedetail", targetId, new ColumnSet(true));
Guid invoiceID = ((EntityReference)invoiceDetail["invoiceid"]).Id;
if (targetEntity.Attributes.Contains("extendedamount"))
{
    Entity myEntity = service.Retrieve("invoice", invoiceID, new ColumnSet(true));
    CalculateRollupFieldRequest rollupRequest = new CalculateRollupFieldRequest
    {
        Target = new EntityReference("invoice", invoiceID),
        FieldName = "detailamount"
    };
    CalculateRollupFieldResponse response = (CalculateRollupFieldResponse)service.Execute(rollupRequest);
    myEntity = response.Entity;
    service.Update(myEntity);
}

你有什么建议吗?我要疯了,什么都想不出来……

【问题讨论】:

  • 您使用的是什么版本的 CRM?
  • 我正在使用 CRM 2016
  • bocasa 我想知道为什么你不能使用前置事件,在此之后记录将被删除,如果删除操作失败,它将回滚。
  • 这是因为我想刷新发票上汇总字段中的值,该值是根据要删除的 invoicedetail 数据计算得出的。因此,如果我在预操作中执行此操作,我将刷新该值并且它会保持不变,因为尚未删除 invoicedetail。

标签: c# plugins dynamics-crm rollup


【解决方案1】:

您可以获得活动前的指导,并将其传递给活动后 - MSDN documentation

来自 MSDN 的示例代码:

using System;

// Microsoft Dynamics CRM namespace(s)
using Microsoft.Xrm.Sdk;

namespace Microsoft.Crm.Sdk.Samples
{
    /// <summary>
    /// A plug-in that sends data to another plug-in through the SharedVariables
    /// property of IPluginExecutionContext.
    /// </summary>
    /// <remarks>Register the PreEventPlugin for a pre-operation stage and the 
    /// PostEventPlugin plug-in on a post-operation stage.
    /// </remarks>
    public class PreEventPlugin : IPlugin
    {
        public void Execute(IServiceProvider serviceProvider)
        {
            // Obtain the execution context from the service provider.
            Microsoft.Xrm.Sdk.IPluginExecutionContext context = (Microsoft.Xrm.Sdk.IPluginExecutionContext)
                serviceProvider.GetService(typeof(Microsoft.Xrm.Sdk.IPluginExecutionContext));

            // Create or retrieve some data that will be needed by the post event
            // plug-in. You could run a query, create an entity, or perform a calculation.
            //In this sample, the data to be passed to the post plug-in is
            // represented by a GUID.
            Guid contact = new Guid("{74882D5C-381A-4863-A5B9-B8604615C2D0}");

            // Pass the data to the post event plug-in in an execution context shared
            // variable named PrimaryContact.
            context.SharedVariables.Add("PrimaryContact", (Object)contact.ToString());
        }
    }

    public class PostEventPlugin : IPlugin
    {
        public void Execute(IServiceProvider serviceProvider)
        {
            // Obtain the execution context from the service provider.
            Microsoft.Xrm.Sdk.IPluginExecutionContext context = (Microsoft.Xrm.Sdk.IPluginExecutionContext)
                serviceProvider.GetService(typeof(Microsoft.Xrm.Sdk.IPluginExecutionContext));

            // Obtain the contact from the execution context shared variables.
            if (context.SharedVariables.Contains("PrimaryContact"))
            {
                Guid contact =
                    new Guid((string)context.SharedVariables["PrimaryContact"]);

                // Do something with the contact.
            }
        }
    }
}

【讨论】:

  • 好兄弟!目前实施这个,会让你知道它是怎么回事......
猜你喜欢
  • 1970-01-01
  • 2018-04-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-20
  • 1970-01-01
  • 2019-11-22
  • 1970-01-01
相关资源
最近更新 更多