Plugins 由API触发时,Dynamics CRM 4.0插件失败

Plugins 由API触发时,Dynamics CRM 4.0插件失败,plugins,dynamics-crm,dynamics-crm-4,Plugins,Dynamics Crm,Dynamics Crm 4,我有一个插件注册,当一个帐户被创建或更新,这是注册后阶段 当用户通过CRM界面创建或更新帐户时,插件工作正常,但是当使用API创建帐户时,插件会失败,并显示有用的“服务器无法处理请求”消息。如果通过api更新帐户,插件也能正常工作 有人知道为什么吗 更新: 下面是创建代码 account = new CrmService.account(); account.ownerid = new CrmService.Owner();

我有一个插件注册,当一个帐户被创建或更新,这是注册后阶段

当用户通过CRM界面创建或更新帐户时,插件工作正常,但是当使用API创建帐户时,插件会失败,并显示有用的“服务器无法处理请求”消息。如果通过api更新帐户,插件也能正常工作

有人知道为什么吗

更新:

下面是创建代码

  account = new CrmService.account();

                account.ownerid = new CrmService.Owner();
                account.ownerid.Value = new Guid("37087BC2-F2F0-DC11-A856-001E0B617486");
                account.ownerid.type = CrmService.EntityName.systemuser.ToString();

                account.name = model.CompanyName;
                account.address1_line1 = model.Address1;
                account.address1_line2 = model.Address2;
                account.address1_stateorprovince = model.County;
                account.address1_country = model.Country;
                account.address1_city = model.TownCity;
                account.address1_postalcode = model.PostCode;
                account.new_companytype = new CrmService.Picklist();

                switch (model.SmeType)
                {
                    case SmeType.Micro:
                        account.new_companytype.Value = 1;
                        break;
                    case SmeType.Small:
                        account.new_companytype.Value = 2;
                        break;
                    case SmeType.Medium:
                        account.new_companytype.Value = 3;
                        break;
                    default:
                        break;
                }

                account.new_balancesheettotal = new CrmService.CrmMoney();
                account.new_balancesheettotal.Value = preQualModel.BalanceSheetGBP;
                account.revenue = new CrmService.CrmMoney();
                account.revenue.Value = preQualModel.SalesTurnoverGBP;
                if (model.Website != null)
                {
                    account.websiteurl = model.Website.ToString();
                }
                account.numberofemployees = new CrmService.CrmNumber();
                account.numberofemployees.Value = (int)preQualModel.NumEmployees;


                accountGuid = svc.Create(account);
                account.accountid = new CrmService.Key();
                account.accountid.Value = accountGuid;
以下是插件代码:

public void Execute(IPluginExecutionContext context)
    {
        DynamicEntity entity = null;

        // Check if the InputParameters property bag contains a target
        // of the current operation and that target is of type DynamicEntity.
        if (context.InputParameters.Properties.Contains(ParameterName.Target) &&
           context.InputParameters.Properties[ParameterName.Target] is DynamicEntity)
        {
            // Obtain the target business entity from the input parmameters.
            entity = (DynamicEntity)context.InputParameters.Properties[ParameterName.Target];

            // TODO Test for an entity type and message supported by your plug-in.
            if (entity.Name != EntityName.account.ToString()) { return; }
            // if (context.MessageName != MessageName.Create.ToString()) { return; }

        }
        else
        {
            return;
        }

        if (entity!=null && !entity.Properties.Contains("address1_postalcode"))
        {
            return;
        }

        if (context.Depth > 2)
        {
            return;
        }

        try
        {
            // Create a Microsoft Dynamics CRM Web service proxy.
            // TODO Uncomment or comment out the appropriate statement.

            // For a plug-in running in the child pipeline, use this statement.
            // CrmService crmService = CreateCrmService(context, true);

            // For a plug-in running in the parent pipeline, use this statement.
            ICrmService crmService = context.CreateCrmService(true);

            #region get erdf area from database

            string postCode = entity.Properties["address1_postalcode"].ToString();
            postCode = postCode.Replace(" ", ""); //remove spaces, db stores pcodes with no spaces, users usually enter them, e.g b4 7xg -> b47xg
            string erdfArea = "";

            SqlConnection myConnection = new SqlConnection(@"REDACTED");

            try
            {
                myConnection.Open();
            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
            }

            try
            {
                SqlDataReader myReader = null;
                SqlCommand myCommand = new SqlCommand("select ErdfAreaType from dim.Locality WHERE PostCode = '" + postCode+"'",
                                                         myConnection);
                myReader = myCommand.ExecuteReader();
                while (myReader.Read())
                {
                    erdfArea = myReader["ErdfAreaType"].ToString();                        
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
            }

            try
            {
                myConnection.Close();
            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
            }

            #endregion

            entity.Properties["new_erdfarea"] = erdfArea;                

            crmService.Update(entity);

        }
        catch (System.Web.Services.Protocols.SoapException ex)
        {
            throw new InvalidPluginExecutionException(
                String.Format("An error occurred in the {0} plug-in.",
                   this.GetType().ToString()),
                ex);
        }
    }

有时很难看到插件中错误的实际来源。在这样的时刻,追踪是你的朋友。您可以使用启用跟踪。当您有跟踪文件时,尝试搜索它们以查找异常中的错误。这会告诉你更多关于失败的细节

事实证明,这是因为我期望的数据由于CRM中的一些奇怪行为而不存在

我是这样接受传递给插件的dynamicEntity的

entity = (DynamicEntity)context.InputParameters.Properties[ParameterName.Target];
但这缺少了一些关键的东西,比如accountid。改为使用PostEntityImage实体修复了此问题,该实体包含所有预期数据,如下所示

entity = (DynamicEntity)context.PostEntityImages[ParameterName.Target];

我假设您期望使用API创建过程中不存在的数据。捕获抛出的SoapException并查看它的Detail属性。也发布一些你的创建和插件代码谢谢我忘记了跟踪,确实帮了一点忙。