【问题标题】:Dynamics CRM 4.0 plugin fails when triggered by API由 API 触发时 Dynamics CRM 4.0 插件失败
【发布时间】:2012-01-04 11:02:43
【问题描述】:

我在创建或更新帐户时注册了一个插件,这是为后期阶段注册的。

当用户通过 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);
        }
    }

【问题讨论】:

  • 我假设您期望使用 API 创建期间不存在的数据。捕捉抛出的 SoapException 并查看它的 Detail 属性。还发布一些你的创建和插件代码

标签: plugins dynamics-crm dynamics-crm-4


【解决方案1】:

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

【讨论】:

  • 谢谢我忘记了追踪,确实有点帮助。
【解决方案2】:

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

我像这样将dynamicEntity传递给插件

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

但这缺少了一些重要的东西,比如 accountid。通过使用 PostEntityImage 实体来修复它,它具有所有预期的数据,就像这样

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

【讨论】:

    猜你喜欢
    • 2018-04-11
    • 1970-01-01
    • 2014-09-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多