2012-01-04 74 views
0

我有一个插件注册帐户时创建或更新,这是注册的后期阶段。动态CRM 4.0插件失败时,由API触发

当用户通过CRM界面创建或更新帐户时,该插件可以正常工作,但是当创建帐户时,使用该API插件失败,并且永远有帮助的“服务器无法处理请求”消息。如果一个帐户通过api更新,该插件也可以正常工作。

任何人有任何想法为什么?

UPDATE:

这里是创建代码

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); 
     } 
    } 
+0

固定它,我假设你希望在与API的创建中不存在的数据。捕获抛出的SoapException并查看它的Detail属性。还发布一些你的创建和插件代码 – ccellar 2012-01-04 11:28:35

回答

0

原来,这是由于我期望的数据不在那里,因为CRM中的一些奇怪的行为。

我正在传递给插件的dynamicEntity像这样

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

但这是缺少像ACCOUNTID紧要之事。通过使用PostEntityImage实体,而不是,它有所有预期的数据,像这样

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

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

+0

谢谢我忘记了跟踪,确实有一点帮助。 – Stuart 2012-01-06 13:27:14