2012-01-04 70 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