【问题标题】:MS Dynamics CE CRM 365 - Pre Operation Plugin - Given key was not present in the dictionaryMS Dynamics CE CRM 365 - 操作前插件 - 字典中不存在给定键
【发布时间】:2019-03-05 09:14:26
【问题描述】:

在做了几年其他工作后,我又回到了 CRM 业务。我已经在质疑我的人生选择了。我不明白这里有什么问题。我正在尝试创建一个将在事件/案例创建时运行的简单插件。它将查看描述字段是否包含有效的 url,如果是,则应将找到的第一个 url 更新到另一个字段。这是插件的执行方法。

    public void Execute(IServiceProvider serviceProvider)
    {
        ITracingService tracingService = (ITracingService)serviceProvider.GetService(typeof(ITracingService));
        IPluginExecutionContext pluginExecutionContext = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));

        if (pluginExecutionContext.InputParameters.Contains("Target") && pluginExecutionContext.InputParameters["Target"] is Entity)
        {
            Entity targetEntity = (Entity)pluginExecutionContext.InputParameters["Target"];
            if (targetEntity.LogicalName != Incident.EntityLogicalName)
            {
                return;
            }

            IOrganizationServiceFactory orgServiceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
            IOrganizationService orgService = orgServiceFactory.CreateOrganizationService(pluginExecutionContext.UserId);

            try
            {
                string desc = (string)targetEntity.Attributes["description"];
                string pattern = @"\b(?:https?://|www\.)[^ \f\n\r\t\v\]]+\b";

                MatchCollection collection = Regex.Matches(desc, pattern);
                if (collection.Count > 0)
                {
                    throw new Exception(collection[0].Value);
                }
            }
            catch (Exception ex)
            {
                tracingService.Trace("Error in CaseUrlPlugin {0}", ex.ToString());
                throw ex;
            }
        }
    }

问题是,当我创建一个新案例(用文本和 url 填充的描述字段)并点击保存时,我得到“字典中不存在给定键”异常,好像描述字段不存在一样。当我在该错误窗口中单击“确定”并再次单击“保存”时,会创建描述字段,并且我的代码会通过该链接引发异常。

那么为什么第一次没有出现描述字段呢?我不喜欢这个 post 操作的想法,因为这需要另一个 sql 事务权限(再次保存事件)?

【问题讨论】:

    标签: dynamics-crm microsoft-dynamics dynamics-crm-365


    【解决方案1】:

    啊 ffs。问题是当我第一次尝试保存时,我使用 CTRL + S 而我的焦点仍然在描述字段上,因为这是我填写的最后一个字段。现在看来,如果专注于该字段并按 CTRL + S,则 UI 不会注册要填充的字段。点击保存图标可以工作,因为这会使焦点离开描述字段。当然,在我的情况下,为该错误窗口点击确定也会使该字段不聚焦,因此第二次保存工作......

    好吧,至少我在这里发布问题后马上就明白了。尝试解决这种方法太久了。

    【讨论】:

    • Ctrl + s 和保存图标将相同的数据提交到数据库。因此,我无法相信这个答案是正确的。此外,第二次保存记录不会触发操作前创建插件,因为您提交的是更新而不是创建。
    • 我可以在我的环境(云)中轻松重现这一点。仅当我没有注册预操作插件时,Ctrl + s 和保存图标才会提交相同的数据。如果我这样做了,那么 ctrl + s 似乎会“退出”关注的字段。我不知道为什么,我不能保证它发生在干净的环境中。然而我的环境几乎和新的一样,不应该有任何会导致这种情况的东西。此外,在我的情况下,第二次点击保存仍然是创建,因为第一次因异常而终止。无论如何,我会在不久的将来对此进行更多调查。
    猜你喜欢
    • 2019-01-01
    • 1970-01-01
    • 2018-12-05
    • 1970-01-01
    • 2018-02-21
    • 1970-01-01
    • 1970-01-01
    • 2020-05-19
    • 2021-03-18
    相关资源
    最近更新 更多