Dynamics 365 CRM Online-使用不在集合中的字段(尚未更新) [英] Dynamics 365 CRM Online - Use a Field that is NOT in collection (Hasn't been updated)

查看:147
本文介绍了Dynamics 365 CRM Online-使用不在集合中的字段(尚未更新)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

总体问题是:如何在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.

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

I have customizations on the Work Order entity within 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.

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

I have tried to alter my "contains" check to be contains XYZ || contains ABC || contains 123, but that fails instantly. 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. All of this has failed me to this point.

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

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开发的新手,但从4.0开始就在MSCRM工作.
任何建议,我们将不胜感激.

I am new-ish to CRM development, but have been working in MSCRM since 4.0.
Any advice is greatly appreciated.

总体问题是:如何在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;
            }
        }

    }

}

推荐答案

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

How do I access entity variables that are not in my collection (haven't been updated) in a plugin for D365 CRM?

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

Answer: Images (Pre-Image or Post-Image)

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

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.

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

For example in your case, register the Post-Update step with PreImage (all 3 attributes). So the modified attribute will be in (Entity)context.InputParameters["Target"]. Unmodified attributes can be consumed from (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;

这篇关于Dynamics 365 CRM Online-使用不在集合中的字段(尚未更新)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆