私は親エンティティと子エンティティを持っています。私は、各親エンティティの子エンティティの数を数えるためのプラグインを作成しており、その数を親エンティティの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);
}
}
}
私はそれが動作する必要があることを確信しています。私はあなたのコードの実行中に何が起こるかを調べるためにトレースを使って実行ログを書き出すことを提案します。 –
同期に切り替えると、動作する可能性が高くなります。また、ロールアップフィールド(コードなし)で同じ結果を得ることができます – Alex
@AndriiButenkoええ、私のコードに何が間違っているのか分かりません。ええ、ログを書くためにトレースを行います。 – xChaax