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

时间:2012-01-04 11:02:44

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

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

当用户通过CRM界面创建或更新帐户时,插件工作正常,但是当创建帐户时,插件会失败,并且“服务器无法处理请求”消息。如果通过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);
        }
    }

2 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:0)

原来这是因为我期待由于CRM中一些奇怪的行为导致的数据不存在。

我正在将dynamicEntity传递给插件,如此

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

但这却缺少像accountid这样的重要事情。通过使用PostEntityImage实体来修复它,它具有所有预期的数据,如此

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