CRM插件不会更新或执行任何更改字段值的操作

时间:2016-10-28 03:47:08

标签: c# linq plugins dynamics-crm-2011 microsoft-dynamics

所以我编写了一个插件来做一些简单的计算,并根据某些条件更新字段。该插件在分析或创建任何我可以调试我的代码的实例时编译并不会导致任何错误,这令人沮丧。不管怎么说,这里都是:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using BPT.PluginCommon.BaseClasses;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Query;
using Xrm;

namespace Engage.Crm.Plugins
{
    public class VoidPayment : BPTPluginBase
    {
        bpt_DuesHeader oDuesHeader = new bpt_DuesHeader();
        Account org = new Account();
        public override void HandleAfterOp()
        {
            try
            {
                base.HandleAfterOp();
                var crmContext = new XrmServiceContext(this.OrganizationService);
                if (this.PluginExecutionContext.MessageName == MessageName.Create ||
                    this.PluginExecutionContext.MessageName == MessageName.Update)
                {
                    if (this.InputTargetEntity.Attributes.Contains("gih_void"))
                    {
                        var Void = (bool) this.InputTargetEntity.Attributes["gih_void"];
                        var voidReason = (OptionSetValue) this.InputTargetEntity.Attributes["gih_voidreason"];
                        var totalPayments = (Money) this.InputTargetEntity.Attributes["bpt_TotalPayments"];
                        var amountBilled =
                            crmContext.bpt_DuesHeaderSet.Where(
                                    o => o.bpt_DuesHeaderId == this.PluginExecutionContext.PrimaryEntityId)
                                .ToList()
                                .Sum(o => o.bpt_TotalAmountBilled == null ? 0 : o.bpt_TotalAmountBilled.Value);
                        if (Void)
                        {
                            this.oDuesHeader.bpt_TotalAdjustments = new Money(amountBilled);
                            this.oDuesHeader.bpt_TotalAmountBilled =
                                new Money(oDuesHeader.bpt_TotalAdjustments.Value + totalPayments.Value);
                            this.oDuesHeader.bpt_Balance = new Money(amountBilled);
                            if (voidReason.Value == 914020000)
                                //should be dropped not default option       
                            {
                                oDuesHeader.gih_terminationdate = DateTime.Now;
                            }
                        }
                        OrganizationService.Update(oDuesHeader);
                    }
                }
            }
            catch (Exception ex)
            {
                this.TracingService.Trace(this.ToString() + " {0}", "Exception: {0}", ex.ToString());
                throw;
            }

        }
    }
}

抱歉代码编写得不好!救命!该插件注册为后期操作和同步。任何见解都会有所帮助,如果主持人可以帮助格式化那些非常受欢迎的代码,因为它不会让我在某些地方添加四个空格。

1 个答案:

答案 0 :(得分:0)

CRM中的插件创建一次,然后多次使用,甚至可以同时使用,因此除了设置ID之外,不要使用类级字段。您正在创建一个可能真正做出一些不必要的更改的竞争条件。