2

我正在尝试为 MS CRM 运行示例插件。但我收到以下错误:

发生错误。请联系系统管理员或参阅 Microsoft Dynamics CRM SDK 故障排除指南。

这是代码:

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

这是一个示例代码,我已经验证了该插件使用的所有实体和字段都已定义并位于那里。但我不断收到此业务错误。

4

2 回答 2

2

我找到了解决方案:我们必须使用,而不是明确提及“帐户”:

Entity entity = (Entity)context.InputParameters["Target"];

错误的第二个原因是 CRM 内部的限制不允许创建新帐户。用于创建新的“联系人”时效果很好。

非常感谢大家的帮助。

于 2013-07-31T08:48:34.790 回答
1

请像这样检查

Entity entity = context.InputParameters["account"] as Entity;

有时不能正常工作。

于 2013-07-30T09:10:10.633 回答