C# MS CRM插件的一般错误
我正在尝试为MS CRM运行一个示例插件。但我得到了以下错误: 发生了一个错误。请与系统管理员联系或参阅 Microsoft Dynamics CRM SDK疑难解答指南 代码如下:C# MS CRM插件的一般错误,c#,dynamics-crm-2011,runtime-error,dynamics-crm,C#,Dynamics Crm 2011,Runtime Error,Dynamics Crm,我正在尝试为MS CRM运行一个示例插件。但我得到了以下错误: 发生了一个错误。请与系统管理员联系或参阅 Microsoft Dynamics CRM SDK疑难解答指南 代码如下: public void Execute(IServiceProvider serviceProvider) { // Obtain the execution context from the service provider. IPluginExecutionContex
public void Execute(IServiceProvider serviceProvider)
{
// Obtain the execution context from the service provider.
IPluginExecutionContext context = (IPluginExecutionContext)
serviceProvider.GetService(typeof(IPluginExecutionContext));
// The InputParameters collection contains all the data passed in the message request.
if (context.InputParameters.Contains("account") &&
context.InputParameters["account"] is Entity)
{
// Obtain the target entity from the input parmameters.
Entity entity = (Entity)context.InputParameters["account"];
try
{
//check if the account number exist
if (entity.Attributes.Contains("account number") == false)
{
//create a task
Entity task = new Entity("task");
task["subject"] = "Account number is missing";
task["regardingobjectid"] = new EntityReference("account", new Guid(context.OutputParameters["id"].ToString()));
//adding attribute using the add function
// task["description"] = "Account number is missng for the following account. Please enter the account number";
task.Attributes.Add("description", "Account number is missng for the following account. Please enter the account number");
// Obtain the organization service reference.
IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);
// Create the task in Microsoft Dynamics CRM.
service.Create(task);
}
}
catch (FaultException ex)
{
throw new InvalidPluginExecutionException("An error occurred in the plug-in.", ex);
}
}
}
}//end class
这是一个示例代码,我已经验证了该插件使用的所有实体和字段都已定义,并且都位于这些位置。但是我不断地收到这个业务错误。请像这样检查它
Entity entity = context.InputParameters["account"] as Entity;
有些时候,它不能正常工作 我找到了解决方案:我们不必明确提到“账户”,而必须使用:
Entity entity = (Entity)context.InputParameters["Target"];
错误的第二个原因是CRM内部的限制,即不允许创建新帐户。用于创建新的“联系人”时效果良好
非常感谢大家的帮助。您的代码中有几个逻辑错误(例如,您需要在
InputParameters
中查找Target
属性,而不是account
),但由于这些错误,您当前的代码将不会执行任何操作,我的最佳猜测是您的目标框架是4.5而不是4.0。此外,您可以尝试运行此示例: