【发布时间】:2013-10-18 08:48:00
【问题描述】:
我就是你所说的 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
(翻译后的粗体文本:“业务流程错误”)
【问题讨论】:
-
我认为错误在于
activitypointer,您需要指定要创建的确切活动实体类型(任务、约会...) -
对我如何做到这一点有什么建议吗?我是 CRM 插件开发的初学者。非常感谢我能得到的所有帮助。
-
您说您想在用户创建联系人时创建活动,但在代码中您正在寻找“帐户”实体。
-
@Scorpion 你发现了另一个错误! @DJZorrow 例如,您需要使用
task而不是activitypointer,并将帐户更改为@Scorpion 所写的联系人
标签: c# dynamics-crm-2011