在表单

时间:2018-07-30 03:33:35

标签: c# dynamics-crm dynamics-crm-365

我有实体信用额度,上面有3个字段,分别是信用等级,信用额度和配置,配置是查找字段,信用额度字段是货币,信用等级是选项集(A,B,C) 。因此更新时它将验证2次,首先是信用等级不能与字段配置已经拥有的信用等级相同

示例:我的预信用等级为B,我的配置已经有2个信用额度,分别为A和B,当我将信用等级更新为A时,这是一个错误,因为配置不能具有2个相同的信用等级

第二次验证是在更新字段信用额度时进行的,因此当前信用额度不能超过原图像信用额度字段,当我尝试将其更新为550美元时,我的原图像信用额度为500美元,将显示错误消息。

因此,我创建了2个插件来同时验证条件和执行的两个事件管道阶段,并将其设置为PreOperation。

,这是信用等级验证

using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Query;
using System;
using System.Collections.Generic;
using TrainingConfiguration.Plugins.Model;

namespace TrainingConfiguration.Plugins
{
    public class CreditRatingUpdateValidation : IPlugin
    {
        public void Execute(IServiceProvider serviceProvider)
        {
            IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
            Entity entity = (Entity)context.InputParameters["Target"];
            IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
            IOrganizationService service = (IOrganizationService)serviceFactory.CreateOrganizationService(context.UserId);
            if (entity.LogicalName.Contains("ita_creditlimit"))
            {

                Entity preImage = (Entity)context.PreEntityImages["CreditImage"];
                EntityReference configuration = (EntityReference)(preImage.Attributes["ita_configuration"]);
                string configurationId = configuration.Id.ToString();

                string fetchXml = @"<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false'>
                      <entity name='ita_creditlimit'>
                      <attribute name='ita_creditlimitid' />
                      <attribute name='ita_name' />
                      <attribute name='ita_creditrating' />
                      <attribute name='ita_configuration' />
                      <order attribute='ita_name' descending='false' />
                      <filter type='and'>                     
                      <condition attribute='ita_configuration' operator='eq' value=""{0}"" /> 
                      </filter>
                      </entity>
                      </fetch>";
                fetchXml = string.Format(fetchXml, configurationId);
                var qe = new FetchExpression(fetchXml);
                var result = service.RetrieveMultiple(qe);
                var preRating = ((OptionSetValue)preImage.Attributes["ita_creditrating"]).Value;
                var postRating = ((OptionSetValue)entity.Attributes["ita_creditrating"]).Value;
                if (result.Entities.Count > 0)
                {
                    List<String> listCreditRating = new List<string>();                        
                    for (int i = 0; i < result.Entities.Count; i++)
                    {
                        string creditRating = ((OptionSetValue)result.Entities[i].Attributes["ita_creditrating"]).Value.ToString();                           
                        listCreditRating.Add(creditRating);
                    }


                    if (postRating.ToString() != preRating.ToString())
                    {
                        bool alreadyExist = listCreditRating.Contains(postRating.ToString());
                        if (alreadyExist == true)
                        {
                            throw new InvalidPluginExecutionException("Can't have 2 same rating");
                        }
                    }

                }
            }
        }
    }
}

此处用于信用额度验证

using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Query;
using System;
using System.Collections.Generic;
using System.Linq;
using TrainingConfiguration.Plugins.Model;

namespace TrainingConfiguration.Plugins
{
    public class CreditUpdateValidation : IPlugin
    {
        public void Execute(IServiceProvider serviceProvider)
        {
            IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
            ITracingService tracingService = (ITracingService)serviceProvider.GetService(typeof(ITracingService));
            IOrganizationServiceFactory serviceFactory =
            (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
            IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);
            Entity entity = (Entity)context.InputParameters["Target"];

            if (entity.LogicalName.Equals("ita_creditlimit"))
            {
                decimal parseInt1 = 0;
                decimal parseInt2 = 0;
                Entity preImage = (Entity)context.PreEntityImages["CreditImage"];
                if (context.PreEntityImages.Contains("CreditImage"))
                {
                    Money preValue = (Money)preImage.Attributes["ita_creditlimit"];

                    parseInt1 = preValue.Value;
                }
                Money postValue = (Money)entity.Attributes["ita_creditlimit"];

                parseInt2 = postValue.Value;

                if (parseInt2 > parseInt1)
                {
                    throw new InvalidPluginExecutionException("Can't Exceed the credit limit");
                }

            }

        }
    }
}

这两个插件一开始都可以正常工作。在尝试一起运行之前,我不知道为什么当我尝试运行 一起运行它将有一个奇怪的异常,有时会说ita_rating键的属性不是 找到或发现ita_creditlimit,即使我已经在插件注册中的过滤属性上添加了它 我已经尝试解决这一问题了一天,但我仍然不知道为什么会发生这些奇怪的行为 因为当我尝试禁用其中一个插件时,另一个插件将起作用,任何人都可以给予启发 是什么原因造成的?这是我的代码还是其他东西?而且我已经尝试使用try and catch了,这是我得到的例外情况

System.Collections.Generic.KeyNotFoundException: The given key was not present in the dictionary.
at System.Collections.Generic.Dictionary`2.get_Item(TKey key)
at TrainingConfiguration.Plugins.CreditUpdateValidation.Execute(IServiceProvider serviceProvider) 

1 个答案:

答案 0 :(得分:0)

我认为我已经知道这里出了什么问题,因此Money postValue = (Money)entity.Attributes["ita_creditlimit"];var value1 = (OptionSetValue)entity.Attributes["ita_creditrating"];仅在值更改时才具有属性。例如,当我的信用额度为$ 500时,我只是不让它在那里更新我的信用额度,多数民众赞成在出现错误时进行修复,因此我尝试使用catch进行修复,当其错误时,我将PreImage用作我的信用额度。