简体   繁体   English

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

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

The overall question is: How do I access entity variables that are not in my collection (haven't been updated) in a plugin for D365 CRM? 总体问题是:如何在D365 CRM的插件中访问不在集合中(尚未更新)的实体变量?

I have been wrestling with this for quite a while, and now, I turn to you, the forum Gods, for help in understanding what's happening here. 我已经为此进行了一段时间的努力,现在,我求助于您,论坛神,以帮助您了解这里发生的事情。

I have customizations on the Work Order entity within D365 CRM (v9.0). 我对D365 CRM(v9.0)中的工作订单实体进行了自定义。 Essentially, whenever one of three fields is updated, I need to perform the same logic each time. 本质上,每当更新三个字段之一时,我每次都需要执行相同的逻辑。 For whatever reason, the plugin will ONLY work when all three fields are updated in a single operation. 无论出于何种原因,只有在一次操作中更新了所有三个字段后,该插件才会起作用。 If I just update one, I will get the "key not present in dictionary" error, signifying that my variable is not in the current collection. 如果我只更新一个,则会收到“字典中不存在键”错误,表示我的变量不在当前集合中。

I have tried to alter my "contains" check to be contains XYZ || 我试图将我的“包含”检查更改为包含XYZ || contains ABC || 包含ABC || contains 123, but that fails instantly. 包含123,但立即失败。 Then, I tried to nest each of those conditions within each other, but of course, the lowest nests don't get touched. 然后,我尝试将每个条件相互嵌套,但是当然,最低的嵌套不会被触及。 Then I tried to un-nest, and just try each of the "contains" checks individually, and go ahead and do the logic in all three if blocks. 然后,我尝试取消嵌套,然后分别尝试每个“包含”检查,然后继续对所有三个if块进行逻辑处理。 All of this has failed me to this point. 所有这些都使我无法做到这一点。

Is there something I am missing here? 我在这里缺少什么吗? For testing purposes, I have it set to fire on All Attributes (not just the three that i want to check on), but even that fails for me. 出于测试目的,我将其设置为对所有属性(不仅是我要检查的三个属性)触发,但即使这样对我也失败了。 I also spoke with a colleague who has quite a bit more experience than I do in the development side of the house for CRM, and you can see some of the artifacts there in the "FieldValue" function calls (though I have stripped out the FieldValue function from this code snip). 我还与一位同事交谈,他的经验比我在CRM的开发方面要多得多,并且您可以在“ FieldValue”函数调用中看到其中的一些工件(尽管我已经删除了FieldValue此代码段的功能)。

I am new-ish to CRM development, but have been working in MSCRM since 4.0. 我是CRM开发的新手,但是从4.0开始就在MSCRM工作。
Any advice is greatly appreciated. 任何意见是极大的赞赏。

The overall question is: How do I access entity variables that are not in my collection (haven't been updated) in a plugin for D365 CRM? 总体问题是:如何在D365 CRM的插件中访问不在集合中(尚未更新)的实体变量?

Code below, with references to my client's names removed: 下面的代码,删除了对我客户名称的引用:

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;
            }
        }

    }

}

How do I access entity variables that are not in my collection (haven't been updated) in a plugin for D365 CRM? 如何在D365 CRM的插件中访问不在集合中(尚未更新)的实体变量?

Answer: Images (Pre-Image or Post-Image) 答案: 图像 (前图像或后图像)

Register an Image with necessary attributes in Update step, so you will get the whole entity object or every single attribute you mark. 在“更新”步骤中注册具有必要属性的图像,这样您将获得整个实体对象或标记的每个单个属性。 Basically its an efficient Retrieve call by platform & serve you in Context itself. 基本上,它是由平台进行的有效检索调用,并在Context本身中为您服务。

For example in your case, register the Post-Update step with PreImage (all 3 attributes). 例如,在您的情况下,使用PreImage(所有3个属性)注册Post-Update步骤。 So the modified attribute will be in (Entity)context.InputParameters["Target"] . 因此,修改后的属性将位于(Entity)context.InputParameters["Target"] Unmodified attributes can be consumed from (Entity)context.PreEntityImages["Image"] . 可以从(Entity)context.PreEntityImages["Image"]使用未经修改的属性。 Read more 阅读更多

Also Contains may come as true always, so check the Money/Decimal fields like explained here . 还含有可能会作为真始终,因此检查金钱/十进制领域,如解释在这里

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

decimal actualAmount;

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

Now you have the attribute values before & after the transaction, hence you decide & store the value to use in calculation. 现在,您拥有交易前后的属性值,因此您可以决定并存储要在计算中使用的值。

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