CRM2011 - “给定的密钥不在字典中”

时间:2013-10-18 08:48:01

标签: c# dynamics-crm-2011

我就是你在CRM插件开发中称之为“n00b”的东西。我正在尝试为Microsoft的Dynamics CRM 2011编写一个插件,该插件将在您创建新联系人时创建一个新的活动实体。我希望此活动实体与联系人实体相关联。

这是我目前的代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xrm.Sdk;

namespace ITPH_CRM_Deactivate_Account_SSP_Disable
{
    public class SSPDisable_Plugin: IPlugin
    {

        public void Execute(IServiceProvider serviceProvider)
        {

            // Obtain the execution context from the service provider.
            IPluginExecutionContext context = (IPluginExecutionContext)
                serviceProvider.GetService(typeof(IPluginExecutionContext));

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

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

            if (entity.LogicalName != "account")
            {
                return;
            }

            Entity followup = new Entity();
            followup.LogicalName = "activitypointer";
            followup.Attributes = new AttributeCollection();
            followup.Attributes.Add("subject", "Created via Plugin.");
            followup.Attributes.Add("description", "This is generated by the magic of C# ...");
            followup.Attributes.Add("scheduledstart", DateTime.Now.AddDays(3));
            followup.Attributes.Add("actualend", DateTime.Now.AddDays(5));

            if (context.OutputParameters.Contains("id"))
            {
                Guid regardingobjectid = new Guid(context.OutputParameters["id"].ToString());
                string regardingobjectidType = "account";

                followup["regardingobjectid"] = new EntityReference(regardingobjectidType, regardingobjectid);

            }

            service.Create(followup);

        }


    }

}

但是当我尝试运行此代码时:当我尝试在CRM环境中创建新联系人时出现错误。错误是:“给定的密钥不在字典中”(链接* 1)。当我尝试保存新联系人时,错误会弹出。

链接* 1:http://puu.sh/4SXrW.png
(翻译粗体文字:“业务流程错误”)

2 个答案:

答案 0 :(得分:2)

Microsoft Dynamics CRM使用术语“活动”来描述几种类型的交互。活动类型包括: 电话,任务,电子邮件,信件,传真和预约。

ActivityPointer (Activity) Entity

使代码正常工作替换以下行:

Entity followup = new Entity();

Entity followup = new Entity("task");

删除以下行:

followup.LogicalName = "activitypointer";

另请阅读我的评论和Guido Preite的推荐。您需要修改代码以使其与联系人一起使用。

<强>被修改

确保CRM中存在ContactId,然后再将其引用到“活动”。

答案 1 :(得分:0)

如果您明确地将属性值添加到已插入的插件中的目标实体,则通常会发生这种情况。

而不是entity.Attributes.Add(...)

使用entity [“attributename”] = ...