2016-10-31 10 views
0

ダイナミクスのためのC#およびプラグインが新しく追加されました。学習してテストするために、私は正常に動作するカップルの非常に単純なプラグインを作成しました。今、私は実際にプラグインで行う必要のあることをさらに深めようとしています。カスタムエンティティでフィールドの値を取得しようとしていて、その値を使用して関連カスタムの属性を更新していますエンティティ。c# - dynamics crm onlineプラグイン - 関連するエンティティの属性を設定するフィールド値を使用します。

My Pluginは、カスタムエンティティ(new_registration)の更新メッセージに登録されています。非同期で動作後に実行されます。プラグインを更新および起動するフィールド(オプションの「ステータス」フィールド)は、コード内では使用されません。ここで

が私のコードです:

string slot = (string)entity.Attributes["new_yourtimeslot"]; 

私は取得していますエラー:テストを通じて

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 

using System.ServiceModel; 
using Microsoft.Xrm.Sdk; 

namespace PlugInTests 
{ 
    public class AdjustTimeSlots: IPlugin 
    { 
     public void Execute(IServiceProvider serviceProvider) 
     { 
      //Extract the tracing service for use in debugging sandboxed plug-ins. 
      ITracingService tracingService = 
       (ITracingService)serviceProvider.GetService(typeof(ITracingService)); 

      // Obtain the execution context from the service provider. 
      IPluginExecutionContext context = (IPluginExecutionContext) 
       serviceProvider.GetService(typeof(IPluginExecutionContext)); 

      if (context.InputParameters != null) 
      { 
       Entity entity = (Entity)context.InputParameters["Target"]; 
       Guid id = entity.Id; 
       tracingService.Trace("got input parameters"); 

       //get time slot 
       string slot = (string)entity.Attributes["new_yourtimeslot"]; 
       EntityReference eventclass = (EntityReference)entity.Attributes["new_eventregistrationrelationshipid"]; 
       tracingService.Trace("got time slot"); 

       //set updated entity (event/class) 
       Entity parentevent = new Entity("new_eventclass"); 
       parentevent.Id = eventclass.Id; 
       parentevent.Attributes["new_timeslotsfordelete"] = slot; 


       // Obtain the organization service reference. 
       IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory)); 
       IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId); 

       //update event record 
       tracingService.Trace("Update time slot plugin"); 
       service.Update(parentevent); 
      } 
     } 
    } 
} 

、私がこの行に(少なくとも最初は)失敗していることを絞り込むました

指定されたキーが辞書に存在しませんでした。

私はチェックして二重チェックしましたが、私はフィールド名が正しいことを知っています。入力パラメータから値を取得する方法が間違っていますか?または私は何かをつまんでいる私は私が台無しにするかもしれないことを理解していない?どんな助けでも感謝しています。

答えて

3

常に属性値を安全な方法で取得しようとします(属性コレクションの属性を確認するか、以下のようなSDKメソッドを使用してください)。属性値がnullの場合、その属性は属性コレクションの一部として返されません。

var slot = entity.GetAttributeValue<string>("new_yourtimeslot"); 

次のコードスニペットは、正しい

EntityReference eventclass = (EntityReference)entity.Attributes["new_eventregistrationrelationshipid"]; 

属性名と関係名がほとんど同じではない見ていません。関係名にはターゲットエンティティと関連するエンティティが含まれていることが多く、検索の対象となる属性の名前は、たぶん異なる名前のnew_eventregistrationidでしょうか? Customization - Field Propertiesを見て名前を再度確認してください。

var eventclass = entity.GetAttributeValue<EntityReference>("new_eventregistrationid"); 
+0

ありがとう:

また、安全に関連する属性を取得します。それはとても役に立ちました。これらの属性値を取得する際の調整が機能しました。その構文の参照はどこですか?私はmsdnのドキュメントを読んできましたが、私はその構文を見たとは思いませんか?古い構文を持つ古いオンラインの例と混同することがあります。助けてくれてありがとう。 –

関連する問題