字典中不存在给定的键(dynamics 365 插件)

The given key was not present in the dictionary (plugin for dynamics 365)

我为主要实体 'incident' 制作了一个插件,它在预操作阶段触发。我收到错误 'The given key was not present in the dictionary'。我不确定这里有什么问题,因为这是我的第一个插件。目的是检查实体 'Warranty'

中是否存在 Case 的序列号 (zst_txtsn)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Xrm.Sdk;
using System.ServiceModel;
using Microsoft.Xrm.Sdk.Query;

namespace ActiveWarranty
{
    public class Class1 : IPlugin
    {
        public void Execute(IServiceProvider serviceProvider)
        {

            ITracingService tracingService =
            (ITracingService)serviceProvider.GetService(typeof(ITracingService));


            IPluginExecutionContext context = (IPluginExecutionContext)
                serviceProvider.GetService(typeof(IPluginExecutionContext));

    
            if (context.InputParameters.Contains("Target") &&
                context.InputParameters["Target"] is Entity)
            {
    
                Entity Case = (Entity)context.InputParameters["Target"];


                IOrganizationServiceFactory serviceFactory =
                    (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
                IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);

                try
                {
                    string serialNumber = Case.Attributes["zst_txtsn"].ToString();
                    Entity Warranty = new Entity("zst_warranty");

                    QueryExpression query = new QueryExpression("zst_warranty");
                    query.ColumnSet = new ColumnSet(new string[] { "zst_name" });
                    query.Criteria.AddCondition("zst_serialno", ConditionOperator.Equal, serialNumber);

                    EntityCollection collection = service.RetrieveMultiple(query);

                    if (collection.Entities.Count > 0)
                    {
                        Case["zst_activewarranty"] = true;
                    }
                    else if (collection.Entities.Count == 0)
                    {
                        Case["zst_activewarranty"] = false;
                    }

                    service.Update(Case);
                }

                catch (FaultException<OrganizationServiceFault> ex)
                {
                    throw new InvalidPluginExecutionException("An error occurred in FollowUpPlugin.", ex);
                }

                catch (Exception ex)
                {
                    tracingService.Trace("FollowUpPlugin: {0}", ex.ToString());
                    throw;
                }
            }
        }
    }
}

Any help would be much appreciated.

当对象为空时,.ToString()会抛出错误。您可以尝试其中之一。

string serialNumber = Case.Attributes["zst_txtsn"] + "";

string serialNumber = "";
if(Case.GetAttributeValue<string>("zst_txtsn") != null)
{
    serialNumber = Case.GetAttributeValue<string>("zst_txtsn").ToString();
}

注意事项

1- 如果此插件已在创建事件中注册 service.update(case) 会失败,因为 Target 还没有 id 在这种情况下,任何更新都应该在没有 service.update

的目标上完成

2- 如果此插件是在更新事件上注册的,如果您没有在插件注册工具中为触发器正确设置过滤属性,则会导致循环。此外,由于它处于前期阶段,因此使用您的布尔值更新目标以将其传递给平台应该就足够了。

在上述两种情况下,您应该删除 service.update

3- 如果 zst_serialno 是一个查找,并且您正在使用序列号文本值进行查询 那么你的查询是错误的 你必须使用 LinkEntities / $expand 来查询相关的实体属性