【问题标题】:CRM 2011 - Given key was not present in dictionary - Update contact pluginCRM 2011 - 字典中不存在给定键 - 更新联系人插件
【发布时间】:2011-05-31 13:14:55
【问题描述】:

我正在尝试将插件绑定到 Microsoft Dynamics CRM 2011 中的更新联系人事件。 我已经制作了一个插件,并且已经为我的组织注册了程序集和步骤。

截图:CRM registration tool

目前,我正在为我的插件使用示例代码。

public class Plugin : IPlugin
{
    public void Execute(IServiceProvider serviceProvider)
    {
        IPluginExecutionContext context = (IPluginExecutionContext)
        serviceProvider.GetService(typeof(IPluginExecutionContext));

    Entity entity;

    // Check if the input parameters property bag contains a target
    // of the create operation and that target is of type Entity.
    if (context.InputParameters.Contains("Target") &&
    context.InputParameters["Target"] is Entity)
    {
        // Obtain the target business entity from the input parameters.
        entity = (Entity)context.InputParameters["Target"];

        // Verify that the entity represents a contact.
        if (entity.LogicalName != "contact") { return; }
    }
    else
    {
        return;
    }

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

        var id = (Guid)context.OutputParameters["id"];

        AddNoteToContact(service, id);
    }
    catch (FaultException<OrganizationServiceFault> ex)
    {
        throw new InvalidPluginExecutionException(
        "An error occurred in the plug-in.", ex);
    }
}

private static void AddNoteToContact(IOrganizationService service, Guid id)
{
    using (var crm = new XrmServiceContext(service))
    {

        var contact = crm.ContactSet.Where(
        c => c.ContactId == id).First();
        Debug.Write(contact.FirstName);

        var note = new Annotation
        {
            Subject = "Created with plugin",
            NoteText = "This Note was created by the example plug-in",
            ObjectId = contact.ToEntityReference(),
            ObjectTypeCode = contact.LogicalName
        };

        crm.AddObject(note);
        crm.SaveChanges();
    }

}
}

但每次我修改联系表格并保存时,我都会收到此错误:

The given key was not present in the dictionary

我一直在寻找一个星期的答案。我希望这里有人可以指导我解决这个问题。我可以提供您需要的所有代码或信息。但是现在,我想不出更多可以帮助您查看我的错误所在的位置。非常感谢任何帮助。

谢谢!

【问题讨论】:

    标签: plugins crm dynamics-crm-2011


    【解决方案1】:

    如果插件注册为前置步骤,OutputParameters 将不包含键“id”,它会抛出该错误。

    【讨论】:

      【解决方案2】:

      M.Medhat 是绝对正确的,但让我们进一步扩展它以便您理解。

      首先要知道InputParameters和OutputParameters的区别。快速阅读this MSDN article describing the difference between InputParameters and OutputParameters

      请务必注意以下声明:

      如果为前置事件注册插件,则 OutputParameters 属性包将不包含“id”键的值,因为尚未发生核心操作。

      因此,这段代码会中断:

      var id = (Guid)context.OutputParameters["id"];
      

      由于您已经创建了一个实体(通过将其从 InputParameters 中转换出来),您可以删除该行并执行以下操作:

      AddNoteToContact(service, entity.id);
      

      别忘了追踪,它是你最好的朋友。它可以在抛出异常时显示信息。这是一个很好的链接:tracing

      【讨论】:

      • 你是完全正确的。但请记住,在这种情况下,OutputParameters 中永远不会有 id,因为UpdateResponse 不包含此成员。将其与此成员有效的Createresponse 进行比较。
      • 感谢您的帮助。在这个答案之后,我花了一段时间让我的插件工作,但那是因为这个插件中的一些其他问题。通过 entity.Id = GREAT 更改“id”。你可能知道我如何从我的插件中获取 app.config 的值吗?因为我猜这不包含在dll文件中。再次感谢!
      【解决方案3】:

      这是我用来帮助显示插件在为给定消息和目标实体注册时收到的所有参数的一些代码,用它来找出键 现在。

      如果您不太愿意翻阅文档以查看“应该”有什么,而只是尝试查看实际发生的情况,只需将其放入您的插件中,注册您打算使用的步骤,它就会显示给您究竟为该步骤提供了哪些参数。

      var propertiesList = String.Join("\n", 
              context.InputParameters.Select((p,i)=>ParamSelector(p,i,"Input")).Union(
              context.InputParameters.Select((p,i)=>ParamSelector(p,i,"Output"))));
      
      //send the list to the tracing service.
      context.Trace("Listing Inputput and Output Parameters for the plugin.\n" + propertiesList);
      
      // throw an exception to see the trace values pop-up (in a synchronous plugin).
      throw new InvalidPluginExecutionException("Check the trace for a listing of parameters.");
      

      支持格式化的委托:

      private string ParamSelector(KeyValuePair<string, object> p, int index, string inOut) 
      {  
          return String.Format("{2} \tKey:'{0}'\tValue:{1}\n{3}", p.Key, p.Value, inOut, EntityToTraceStrings(p.Value as Entity));    
      }
      
      private string EntityToTraceStrings(Entity entity)
      {
          return entity == null ? String.Empty : String.Concat(
              String.Format("- Entity: {0} Id: {1}\n\t", entity.LogicalName, entity.Id),
              String.Join("\n\t", entity.FormattedValues.Select((p, j) => String.Format("Attribute: {0} \t Value: {1}", p.Key, p.Value))));
      }
      

      【讨论】:

        猜你喜欢
        • 2023-03-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-02-21
        • 1970-01-01
        • 2018-12-05
        • 2011-09-30
        相关资源
        最近更新 更多