繁体   English   中英

Dynamics 365 CRM Online-使用不在集合中的字段(尚未更新)

[英]Dynamics 365 CRM Online - Use a Field that is NOT in collection (Hasn't been updated)

总体问题是:如何在D365 CRM的插件中访问不在集合中(尚未更新)的实体变量?

我已经为此进行了一段时间的努力,现在,我求助于您,论坛神,以帮助您了解这里发生的事情。

我对D365 CRM(v9.0)中的工作订单实体进行了自定义。 本质上,每当更新三个字段之一时,我每次都需要执行相同的逻辑。 无论出于何种原因,只有在一次操作中更新了所有三个字段后,该插件才会起作用。 如果我只更新一个,则会收到“字典中不存在键”错误,表示我的变量不在当前集合中。

我试图将我的“包含”检查更改为包含XYZ || 包含ABC || 包含123,但立即失败。 然后,我尝试将每个条件相互嵌套,但是当然,最低的嵌套不会被触及。 然后,我尝试取消嵌套,然后分别尝试每个“包含”检查,然后继续对所有三个if块进行逻辑处理。 所有这些都使我无法做到这一点。

我在这里缺少什么吗? 出于测试目的,我将其设置为对所有属性(不仅是我要检查的三个属性)触发,但即使这样对我也失败了。 我还与一位同事交谈,他的经验比我在CRM的开发方面要多得多,并且您可以在“ FieldValue”函数调用中看到其中的一些工件(尽管我已经删除了FieldValue此代码段的功能)。

我是CRM开发的新手,但是从4.0开始就在MSCRM工作。
任何意见是极大的赞赏。

总体问题是:如何在D365 CRM的插件中访问不在集合中(尚未更新)的实体变量?

下面的代码,删除了对我客户名称的引用:

using System;
using System.Linq;
using System.Collections.Generic;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Client;
using Microsoft.Xrm.Sdk.Query;
using Microsoft.Crm.Sdk.Messages;
using System.ServiceModel;
using System.Data.SqlClient;
using System.Threading.Tasks;

namespace ClientNTE
{
    public class NTEExceedance : IPlugin
    {
        public void Execute(IServiceProvider serviceProvider)
        {
            IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
            IOrganizationServiceFactory factory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
            IOrganizationService service = factory.CreateOrganizationService(context.UserId);
            //Extract the tracing service for use in debugging sandboxed plug-ins.
            ITracingService tracingService = (ITracingService)serviceProvider.GetService(typeof(ITracingService));

            Money subtotal = null;
            Money nte = null;
            Decimal nte_percent = 0;

            Decimal subtotalDecimal = 0;
            Decimal nteDecimal = 0;
            Decimal amountDiffDecimal = 0;
            Decimal percentDifference = 0;

            try
            {

                if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity)
                {
                    Entity entity = (Entity)context.InputParameters["Target"];
                    if (entity.LogicalName == "msdyn_workorder")
                    {
                        //code fires onChange of NTE Amount (same logic will apply to NTE % and Est Subtotal Amount)
                        if (entity.Attributes.Contains("CLIENT_nteamount") == true)
                        {


                            //trying to use the FieldValue function to grab these fields into collection, commented out for now
                            //String NewValue = FieldValue(service, new Guid(entity["msdyn_workorderid"].ToString()));
                            //String NewSubTotal = FieldValue(service, new Guid(entity["msdyn_workorderid"].ToString()), entity["msdyn_estimatesubtotalamount"].ToString());
                            //String NewNTE = FieldValue(service, new Guid(entity["msdyn_workorderid"].ToString()), entity["CLIENT_nteamount"].ToString());
                            //String Newpercent = FieldValue(service, new Guid(entity["msdyn_workorderid"].ToString()), entity["CLIENT_ntepercent"].ToString());

                            subtotal = (Money)entity.Attributes["msdyn_estimatesubtotalamount"];
                            nte = (Money)entity.Attributes["CLIENT_nteamount"];
                            nte_percent = (Decimal)entity.Attributes["CLIENT_ntepercent"];



                            subtotalDecimal = subtotal.Value;
                            nteDecimal = nte.Value;

                            amountDiffDecimal = (subtotalDecimal - nteDecimal);
                            percentDifference = ((amountDiffDecimal / nteDecimal) * 100);

                            // decimal percentDifference = 100;
                            //decimal nte_percent = 50;
                            if (percentDifference > nte_percent)
                            {
                                //know this snippet works
                                entity["CLIENT_nteexceeded"] = true;
                            }
                            if (percentDifference <= nte_percent)
                            {
                                //know this snippet works
                                entity["CLIENT_nteexceeded"] = false;
                            }
                        }
                        if (entity.Attributes.Contains("CLIENT_ntepercent") == true)
                        {
                            subtotal = (Money)entity.Attributes["msdyn_estimatesubtotalamount"];
                            nte = (Money)entity.Attributes["CLIENT_nteamount"];
                            nte_percent = (Decimal)entity.Attributes["CLIENT_ntepercent"];


                            subtotalDecimal = subtotal.Value;
                            nteDecimal = nte.Value;

                            amountDiffDecimal = (subtotalDecimal - nteDecimal);
                            percentDifference = ((amountDiffDecimal / nteDecimal) * 100);

                            // decimal percentDifference = 100;
                            //decimal nte_percent = 50;
                            if (percentDifference > nte_percent)
                            {
                                //know this snippet works
                                entity["CLIENT_nteexceeded"] = true;
                            }
                            if (percentDifference <= nte_percent)
                            {
                                //know this snippet works
                                entity["CLIENT_nteexceeded"] = false;
                            }
                        }
                        if (entity.Attributes.Contains("msdyn_estimatesubtotalamount") == true)
                        {


                            subtotal = (Money)entity.Attributes["msdyn_estimatesubtotalamount"];
                            nte = (Money)entity.Attributes["CLIENT_nteamount"];
                            nte_percent = (Decimal)entity.Attributes["CLIENT_ntepercent"];


                            subtotalDecimal = subtotal.Value;
                            nteDecimal = nte.Value;

                            amountDiffDecimal = (subtotalDecimal - nteDecimal);
                            percentDifference = ((amountDiffDecimal / nteDecimal) * 100);

                            // decimal percentDifference = 100;
                            //decimal nte_percent = 50;
                            if (percentDifference > nte_percent)
                            {
                                //know this snippet works
                                entity["CLIENT_nteexceeded"] = true;
                            }
                            if (percentDifference <= nte_percent)
                            {
                                //know this snippet works
                                entity["CLIENT_nteexceeded"] = false;
                            }


                            /*
                            Money m = (Money)entity.Attributes["new_evalmoneyvalue"];

                            decimal actualAmount = m.Value;

                            entity["new_evaldecimal"] = actualAmount;

                            entity["new_evalmoneyvalue"] = new Money((decimal)actualAmount * 2);
                            */
                        }

                    }
                }



            }
            catch (FaultException<OrganizationServiceFault> e)
            {
                tracingService.Trace("CLIENTPlugin - Update NTEExceededNonCalc: {0}", e.ToString());
                throw e;
            }
        }

    }

}

如何在D365 CRM的插件中访问不在集合中(尚未更新)的实体变量?

答案: 图像 (前图像或后图像)

在“更新”步骤中注册具有必要属性的图像,这样您将获得整个实体对象或标记的每个单个属性。 基本上,它是由平台进行的有效检索调用,并在Context本身中为您服务。

例如,在您的情况下,使用PreImage(所有3个属性)注册Post-Update步骤。 因此,修改后的属性将位于(Entity)context.InputParameters["Target"] 可以从(Entity)context.PreEntityImages["Image"]使用未经修改的属性。 阅读更多

还含有可能会作为真始终,因此检查金钱/十进制领域,如解释在这里

Money myMoneyField = (Money)EntityObject.GetAttributeValue<Money>(Amount);

decimal actualAmount;

if (myMoneyField != null)
{
    actualAmount = myMoneyField.Value;
}

现在,您拥有交易前后的属性值,因此您可以决定并存储要在计算中使用的值。

Entity orderEntity = (Entity)context.InputParameters["Target"];
Entity preOrderEntity = (Entity)context.PreEntityImages["Image"];
Decimal preNTEamount = preOrderEntity.GetAttributeValue<Money>("CLIENT_nteamount") != null ? ((Money)preOrderEntity.GetAttributeValue<Money>("CLIENT_nteamount")).Value : 0;
Decimal newNTEamount = orderEntity.GetAttributeValue<Money>("CLIENT_nteamount") != null ? ((Money)orderEntity.GetAttributeValue<Money>("CLIENT_nteamount")).Value : preNTEamount;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM