2

再定式化された質問 (4 月 24 日):

VS2012 用の CRM 開発者ツールキットを使用して、CRM2011 プラグインを作成しています。プラグインはCREATE、「Invoice Product」エンティティのメッセージに登録されます。Pipeline-Stage は運用後であり、実行は同期的です。を含む投稿画像に登録しますbaseamount

ツールキットは、次のような実行関数を作成します。

protected void ExecutePostInvoiceProductCreate(LocalPluginContext localContext)
{
    if (localContext == null)
    {
        throw new ArgumentNullException("localContext");
    }

    IPluginExecutionContext context = localContext.PluginExecutionContext;

    Entity postImageEntity = (context.PostEntityImages != null && context.PostEntityImages.Contains(this.postImageAlias)) ? context.PostEntityImages[this.postImageAlias] : null;
}

運用後の段階なので、 baseamountinの値はpostImageEntityユーザー入力からすでに計算されているはずですよね? ただし、 の値baseamountpostImageEntityゼロです。baseamount同じことが、次のコードを使用して取得するターゲット エンティティの値にも当てはまります。

Entity targetEntity = (context.InputParameters != null && context.InputParameters.Contains("Target")) ? (Entity)context.InputParameters["Target"] : null;

以下のような取得リクエストを使用して、正しい値を取得していますbaseamount:

Entity newlyCreated = service.Retrieve("invoicedetail", targetEntity.Id, new ColumnSet(true));
decimal baseAmount = newlyCreated.GetAttributeValue<Money>("baseamount").Value;

この問題は、更新イベントの運用後の段階では発生しません。

なぜこれが当てはまるのかについて、あなたのアイデア/説明/提案を聞いてうれしいです...

(詳細情報: リモート デバッグ、分離モードなし、データベースに格納されたプラグイン)

元の質問:

請求書の詳細が作成されたときに支払われる税額を計算することになっている CRM 2011 のプラグインに取り組んでいます。この目的のために、運用後の段階でエンティティ イメージからbaseamount新しく作成されたエンティティを取得しようとしています。invoicedetail私が理解している限り、投稿エンティティ イメージは、新しい請求書の詳細が作成された後のデータベース内のエンティティのスナップショットです。したがって、新しく作成された請求書の詳細のすべてのプロパティが含まれている必要があります。

登録したエイリアス (「postImage」) を持つエンティティを含む IPluginExecutionContext の「postentityimages」プロパティを取得しています。この "postImage" エンティティには "baseamount" のキーが含まれていますが、その値は 0 です。なぜそうなのか、そしてそれに対して何ができるのかを理解してくれる人はいますか?

(postImage には、登録したエンティティのすべてではなくサブセットのみが含まれていることにも気付きました。)

コードは次のようになります。

  protected void ExecutePostInvoiceProductCreate(LocalPluginContext localContext)
  {
        if (localContext == null)
        {
            throw new ArgumentNullException("localContext");
        }

        // Get PluginExecutionContext to obtain PostEntityImages
        IPluginExecutionContext context = localContext.PluginExecutionContext;

        // This works: I get a postImage that is not null.
        Entity postImage = (context.PostEntityImages != null && context.PostEntityImages.Contains(this.postImageAlias)) ? context.PostEntityImages[this.postImageAlias] : null;

        // Here is the problem: There is a "baseamount" key in the postImage 
        // but its value is zero!
        decimal baseAmount = ((Money)postImage["baseamount"]).Value;

  }

追加: 運用後の更新の事前および事後イメージには、basemount のゼロ以外の値が含まれています。

4

3 に答える 3

1

3 つの注意点があります。1 つは問題を解決することを願っており、残りの 2 つはベスト プラクティスです。

  1. baseamount投稿画像の属性には、登録時に入力するように指定した値のみが入力されるため、投稿画像に含める必要があることを指定したことを確認してください。(ユーザーがそのフィールドに対する権限を持っていない場合、セキュリティ上の問題が発生する可能性もありますが、それは 0 で null ではないため、これは問題ではないと思います)
  2. エンティティのインデックスにアクセスするだけでなく、GetAttributeValue を使用します。文字列キーがエンティティ Attribute コレクションにない場合は、エラーがスローされます。

    decimal baseAmount = e.GetAttributeValue("baseamount").Value;

  3. これは請求書の詳細の作成時に実行されるため、ターゲットには、作成中に入力されたすべてのデータが含まれている必要があります。これにより、ポスト イメージが不要になります。
于 2013-04-19T12:53:53.330 に答える
0

次のコードを使用して簡単なプラグインを作成できます。参照はこちら

public void Execute(IServiceProvider serviceProvider)
    {
        // Obtain the execution context from the service provider.
        ITracingService tracingService = (ITracingService)serviceProvider.GetService(typeof(ITracingService));

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

        IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
        IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);

        // The InputParameters collection contains all the data passed in the message request.
        if (context.InputParameters.Contains("Target") &&
            context.InputParameters["Target"] is Entity)
        {
            Entity entity = (Entity)context.InputParameters["Target"];

            // Verify that the target entity represents an account.
            // If not, this plug-in was not registered correctly.
            if (entity.LogicalName != "account")
                return;

            try
            {

                     //Access / get data of entity
                     string country = entity.Attributes.ContainsKey("address1_county") ? entity.Attributes["address1_county"].ToString() : "";
                     //Update existing values in entity (Account)
                     entity.Attributes["name"] = "My Name"

            }

            catch (Exception ex)
            {
                throw new InvalidPluginExecutionException(ex.Message + ", Stack trace : " +  ex.StackTrace);
            }

        }
    }
于 2013-10-29T09:51:17.010 に答える
0

この問題を締めくくるために: 最終的に、サーバーに CRM インスタンスを再インストールしたところ、問題はなくなりました。

于 2013-11-14T08:40:30.483 に答える