2017-10-30 12 views
2

私は親エンティティと子エンティティを持っています。私は、各親エンティティの子エンティティの数を数えるためのプラグインを作成しており、その数を親エンティティのnoOfProductフィールドに表示しています。したがって、新しい子エンティティを作成するたびに、noOfProductの数値の値は1になります。ただし、子エンティティを削除すると、プラグインがトリガされないため、値は変わりません。メッセージプラグインが発行されていません。Dynamics CRM

は、私はこれは私の完全なコードです

step: create 
primary entity: child_entity 
event_pipeline: post-operation 
synchronous 
Plugin Images: post-image 

、私のプラグインを登録しました。

using System; 
using System.IO; 
using System.ServiceModel; 
using System.ServiceModel.Description; 
using Microsoft.Xrm.Sdk; 
using Microsoft.Xrm.Sdk.Query; 
using Microsoft.Xrm.Sdk.Messages; 
using Microsoft.Xrm.Sdk.Client; 
using System.Net; 
using System.Web.Services; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 

namespace NoOfProductsPlugin 
{ 
    public class NoOfProducts : IPlugin 
    { 
    public void Execute(IServiceProvider serviceProvider) 
    { 
     ITracingService tracingService = 

    (ITracingService)serviceProvider.GetService(typeof(ITracingService)); 
     IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext)); 

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

     //for create and update event 
     if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is Entity) 
     { 
      // Obtain the target entity from the input parmameters. 
      Entity targetEntity = (Entity)context.InputParameters["Target"]; 

      // Verify that the entity represents a connection. 
      if (targetEntity.LogicalName != "child_entity") 
      { 
       return; 
      } 
      else 
      { 
       try 
       { 
        //triggered upon create or update message 
        if (context.MessageName == "Create" || context.MessageName == "Update") 
        { 
         Entity postMessageImage; 
         Guid oppId = new Guid(); 

         if (context.PostEntityImages.Contains("postMessageImage") && context.PostEntityImages["postMessageImage"] is Entity) 
         { 
          postMessageImage = (Entity)context.PostEntityImages["postMessageImage"]; 
          oppId = ((EntityReference)postMessageImage.Attributes["lookup_fieldtoParent"]).Id; 
         } 

         //throw new InvalidPluginExecutionException 

         queryOppProd(service, oppId); 
        } 

       } 
       catch (FaultException<OrganizationServiceFault> ex) 
       { 
        throw new InvalidPluginExecutionException("An error occurred :-" + ex.Message, ex); 
       } 
       //</snippetFollowupPlugin3> 

       catch (Exception ex) 
       { 
        tracingService.Trace("An error occurred : {0}" + ex.Message, ex.ToString()); 
        throw; 
       } 
      } 
     } 
     //for delete event use entityreference 
     else if (context.InputParameters.Contains("Target") && context.InputParameters["Target"] is EntityReference) 
     { 
      // Obtain the target entity from the input parmameters. 
      EntityReference targetEntity = (EntityReference)context.InputParameters["Target"]; 

      // Verify that the entity represents a connection. 
      if (targetEntity.LogicalName != "child_entity") 
      { 
       return; 
      } 
      else 
      { 

       try 
       { 
        //triggered upon delete message 
        if (context.MessageName == "Delete") 
        { 
         Guid oppProdId = targetEntity.Id; 

         // retrieve oppid guid 
         Entity oppProd = new Entity("child_entity"); 
         ColumnSet columns_ = new ColumnSet(new string[] { "lookup_fieldtoParent" }); 
         oppProd = service.Retrieve(oppProd.LogicalName, oppProdId, columns_); 

         Guid oppId = new Guid(); 
         oppId = ((EntityReference)oppProd["lookup_fieldtoParent"]).Id; 

         //throw new InvalidPluginExecutionException(
        } 

       } 
       catch (FaultException<OrganizationServiceFault> ex) 
       { 
        throw new InvalidPluginExecutionException("An error occurred :-" + ex.Message, ex); 
       } 
       //</snippetFollowupPlugin3> 

       catch (Exception ex) 
       { 
        tracingService.Trace("An error occurred: {0}" + ex.Message, ex.ToString()); 
        throw; 
       } 
      } 
     } 
    } 
    public void queryOppProd(IOrganizationService service, Guid oppId) 
    { 
     int noOfProduct = 0; 

     QueryExpression oppProdQuery = new QueryExpression { EntityName = "child_entity", ColumnSet = new ColumnSet("child_entityid", "lookup_fieldtoParent") }; 
     oppProdQuery.Criteria.AddCondition("lookup_fieldtoParent", ConditionOperator.Equal, oppId); // to search for child_entity that linked to the selected parent_entity 
     EntityCollection oppProdQueryRetrieve = service.RetrieveMultiple(oppProdQuery); 

     if (oppProdQueryRetrieve != null && oppProdQueryRetrieve.Entities.Count > 0) 
     { 
      for (var i = 0; i < oppProdQueryRetrieve.Entities.Count; i++) 
      { 

       noOfProduct++; 
      } 
     } 

     //declare table used to retrieve the field and update 
     Entity opportunity = new Entity("parent_entity"); 
     ColumnSet columns = new ColumnSet(new string[] { "new_noofproducts" }); 
     opportunity = service.Retrieve(opportunity.LogicalName, oppId, columns); 

     opportunity["new_noofproducts"] = noOfProduct; 
     service.Update(opportunity); 
    } 

    public void queryOppProdOnDel(IOrganizationService service, Guid oppId, Guid oppProdId) 
    { 
     int noOfProduct = 0; 


     //query opportunityProduct by using opportunity guid 
     QueryExpression oppProdQuery = new QueryExpression { EntityName = "child_entity", ColumnSet = new ColumnSet("child_entityid", "lookup_fieldtoParent") }; 

     FilterExpression oppProdQueryFilter = oppProdQuery.Criteria.AddFilter(LogicalOperator.And); 
     oppProdQueryFilter.AddCondition("child_entityid", ConditionOperator.NotEqual, oppProdId); 
     oppProdQueryFilter.AddCondition("lookup_fieldtoParent", ConditionOperator.Equal, oppId); // to search for child_entity that linked to the selected parent_entity 
     EntityCollection oppProdQueryRetrieve = service.RetrieveMultiple(oppProdQuery); 

     if (oppProdQueryRetrieve != null && oppProdQueryRetrieve.Entities.Count > 0) 
     { 
      for (var i = 0; i < oppProdQueryRetrieve.Entities.Count; i++) 
      { 

       noOfProduct++; 
      } 
     } 
     //throw new InvalidPluginExecutionException 

     //declare table used to retrieve the field and update 
     Entity opportunity = new Entity("parent_entity"); 
     ColumnSet columns = new ColumnSet(new string[] { "new_noofproducts" }); 

     opportunity = service.Retrieve(opportunity.LogicalName, oppId, columns); 
     service.Update(opportunity); 
    } 
} 
} 
+0

私はそれが動作する必要があることを確信しています。私はあなたのコードの実行中に何が起こるかを調べるためにトレースを使って実行ログを書き出すことを提案します。 –

+0

同期に切り替えると、動作する可能性が高くなります。また、ロールアップフィールド(コードなし)で同じ結果を得ることができます – Alex

+0

@AndriiButenkoええ、私のコードに何が間違っているのか分かりません。ええ、ログを書くためにトレースを行います。 – xChaax

答えて

0

queryOppProdOnDelを呼び出すことを忘れました。

プラグインアセンブリを登録するとき&メッセージを削除するには、次のスニペットをコードに置き換えてください。

    //triggered upon delete message 
        if (context.MessageName == "Delete") 
        { 
         Guid oppProdId = targetEntity.Id; 

         // retrieve oppid guid 
         Entity oppProd = new Entity("child_entity"); 
         ColumnSet columns_ = new ColumnSet(new string[] { "lookup_fieldtoParent" }); 
         oppProd = service.Retrieve(oppProd.LogicalName, oppProdId, columns_); 

         Guid oppId = new Guid(); 
         oppId = ((EntityReference)oppProd["lookup_fieldtoParent"]).Id; 

         //throw new InvalidPluginExecutionException(

         queryOppProdOnDel(service, oppId, oppProdId); 
        } 

更新:

この行はqueryOppProdOnDelに欠落しています。

opportunity["new_noofproducts"] = noOfProduct; 
+0

こんにちは@Arun、これを強調してくれてありがとう。私はこれを認識していますが、結果は同じです。私がプラグインプロファイラを削除ステップで使用しているとき、プラグインの削除メッセージに問題はないようです。 – xChaax

+0

最新のアップデートも知っていますか? –

+0

どうして私はこれに気づかないのです、ありがとう。どのように削除が 'new_noofproducts'フィールドに正しいデータを反映していないか商品が「0」のときは「1」と表示されます。 – xChaax

0

ポイントのカップル:

  1. step: createdeleteにあなたを登録する必要があります。
  2. かなり確実なポスト画像is not supportedが削除されました。あなたはプレのものを使う必要があります。 msgstr "" "作成操作はプリ画像をサポートせず、削除操作はポスト画像をサポートしていません。"

  3. 基本設計に欠陥があります。同時に多くの変更が発生すると、それらはすべて別々のスレッドで同時に実行される可能性があります。つまり、場合によっては数が正しくない可能性があります。

+0

ありがとう@James、プラグインを両方のステップに登録しました.Plugin-imageなしでpre-operationで 'post'と' post'イメージに 'create'を実行しました。残念ながら、Pluginはメッセージ 'create'をトリガーします。 – xChaax

+1

削除後の操作(同期と非同期)で操作後処理がサポートされています。 –

+1

ターゲットはエンティティではなくEntityReferenceであるため、削除ステップはトリガされません – Sxntk

関連する問題