簡體   English   中英

Dynamics CRM插件-刪除時刷新匯總

[英]Dynamics CRM Plugin - Refresh rollup on delete

我有一個特殊的情況需要處理。 我有一個插件,可以在創建或更新發票明細時刷新發票上的特定匯總字段。 現在,當發票明細被刪除時,我需要刷新該字段。


分析此問題后,我意識到,由於尚未刪除發票明細記錄,因此無法在操作前刷新匯總字段,而在操作后,由於它已消失,因此無法從該特定記錄中檢索發票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);
}

你有什么建議嗎? 我為此感到生氣,什么也想不起來...

您可以在活動前獲取guid並將其傳遞給活動后-MSDN文檔

來自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. } } } } 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM