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

のように期待されるすべてのデータを持っていた代わりにPostEntityImageエンティティを使用してそれを修正私はあなたがAPIを使用して作成時に存在していないデータを期待すると仮定します。スローされたSoapExceptionをキャッチし、そのDetailプロパティを見てください。また、作成したプラグインコードの一部を投稿してください – ccellar

答えて

0

これは、私がCRMに何らかの奇妙な振る舞いがあったためにそこになかったデータを期待していたためです。

私はdynamicEntityを取っていたので

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

のようなプラグインに渡された。しかし、これはアカウントIDのような重要なものがありませんでした。そう

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

時にはプラグインでエラーの実際のソースを確認することが困難な場合が。このような瞬間にあなたの友人が辿り着きます。 this toolを使用してトレースを有効にすることができます。トレースファイルがあるときは、例外で見つけたエラーを検索してみてください。これにより、何が失敗しているかについての詳細がわかります。

+0

私はトレースについて忘れてしまったので、ちょっと助けてくれました。 – Stuart

関連する問題