28. Mai 2015 13:22
if (entity.LogicalName == OPD_gutschriftprodukt.EntityLogicalName)
{
tracingService.Trace("Entität ist gutschriftprodukt");
bool isSupportedMessage =
(context.MessageName == "Create") ||
(context.MessageName == "Update") ||
(context.MessageName == "Delete") ||
(context.MessageName == "SetState") ||
(context.MessageName == "SetStateDynamicEntity");
bool isDeleteMessage = (context.MessageName == "Delete");
bool isSetStateMessage = (context.MessageName == "SetState" || context.MessageName == "SetStateDynamicEntity");
// Run the PlugIn only once for each update/delete
bool proceedProcessing = (((context.MessageName == "SetState") ||
(context.MessageName == "SetStateDynamicEntity")) && context.Depth < 8) ||
(context.Depth < 2);
if (proceedProcessing && isSupportedMessage)
{
tracingService.Trace("proceedProcessing und isSupportedMessage sind true");
if (isDeleteMessage)
{
tracingService.Trace("isDeleteMessage true");
// A PreEntityImage "Target" has to be registered for this PlugIn-Step
prePostImageEntity = (Entity) context.PreEntityImages["PreUpdateImage"];
//gutschriftprodukt = new OpdGutschriftprodukt((DynamicEntity)context.PreEntityImages.Properties["opd_gutschriftprodukt"]);
}
else
{
tracingService.Trace("isDeleteMessage false");
prePostImageEntity = (Entity)context.PostEntityImages["PostUpdateImage"];
//gutschriftprodukt = new OpdGutschriftprodukt((DynamicEntity)context.PostEntityImages.Properties["opd_gutschriftprodukt"]);
}
}
if (prePostImageEntity != null && prePostImageEntity.Contains("opd_gutschriftid") && prePostImageEntity["opd_gutschriftid"] != null)
{
EntityReference gutschriftid = (EntityReference)prePostImageEntity["opd_gutschriftid"];
tracingService.Trace("image nicht null und enthält opdgutschrift id nicht nll");
if (isDeleteMessage)
{
tracingService.Trace("isdeletemessage true");
QueryExpression query = new QueryExpression();
query.EntityName = OPD_gutschriftprodukt.EntityLogicalName;
query.ColumnSet = new ColumnSet();
query.ColumnSet.Columns.Add("opd_erw_betrag");
FilterExpression filter = new FilterExpression();
ConditionExpression condition = new ConditionExpression("opd_gutschriftid", ConditionOperator.Equal, gutschriftid.Id);
filter.Conditions.Add(condition);
condition = new ConditionExpression("opd_erw_betrag", ConditionOperator.NotNull);
filter.Conditions.Add(condition);
condition = new ConditionExpression("opd_gutschriftproduktid", ConditionOperator.NotEqual, prePostImageEntity["opd_gutschriftproduktid"]);
filter.Conditions.Add(condition);
query.Criteria = filter;
DataCollection<Entity> entityCollection = _service.RetrieveMultiple(query).Entities;
tracingService.Trace("Entitycollection geladen");
decimal gutschriftsumme = 0;
foreach (OPD_gutschriftprodukt produkt in entityCollection)
{
gutschriftsumme += produkt.OPD_erw_betrag.Value;
}
OPD_gutschrift gutschrift = new OPD_gutschrift();
gutschrift.Id = gutschriftid.Id;
gutschrift.OPD_Gutschriftbetrag_netto = new Money(gutschriftsumme);
IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);
//</snippetFollowupPlugin4>
// Create the task in Microsoft Dynamics CRM.
tracingService.Trace("gutschriftupdate");
service.Update(gutschrift);
}
else
{
tracingService.Trace("isdeletemessage false");
QueryExpression query = new QueryExpression();
query.EntityName = OPD_gutschriftprodukt.EntityLogicalName;
query.ColumnSet = new ColumnSet();
query.ColumnSet.Columns.Add("opd_erw_betrag");
FilterExpression filter = new FilterExpression();
ConditionExpression condition = new ConditionExpression("opd_gutschriftid", ConditionOperator.Equal, gutschriftid.Id);
filter.Conditions.Add(condition);
condition = new ConditionExpression("opd_erw_betrag", ConditionOperator.NotNull);
filter.Conditions.Add(condition);
query.Criteria = filter;
DataCollection<Entity> entityCollection = _service.RetrieveMultiple(query).Entities;
decimal gutschriftsumme = 0;
foreach (OPD_gutschriftprodukt produkt in entityCollection)
{
gutschriftsumme += produkt.OPD_erw_betrag.Value;
}
OPD_gutschrift gutschrift = new OPD_gutschrift();
gutschrift.Id = gutschriftid.Id;
gutschrift.OPD_Gutschriftbetrag_netto = new Money(gutschriftsumme);
IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);
//</snippetFollowupPlugin4>
// Create the task in Microsoft Dynamics CRM.
tracingService.Trace("gutschriftupdate");
service.Update(gutschrift);
}
}
}
28. Mai 2015 14:58
29. Mai 2015 12:52
1. Juni 2015 12:14
QueryExpression query = new QueryExpression();
query.EntityName = OPD_gutschriftprodukt.EntityLogicalName;
query.ColumnSet = new ColumnSet();
query.ColumnSet.Columns.Add("opd_erw_betrag");
FilterExpression filter = new FilterExpression();
ConditionExpression condition = new ConditionExpression("opd_gutschriftid", ConditionOperator.Equal, gutschriftid.Id);
filter.Conditions.Add(condition);
condition = new ConditionExpression("opd_erw_betrag", ConditionOperator.NotNull);
filter.Conditions.Add(condition);
query.Criteria = filter;
DataCollection<Entity> entityCollection = _service.RetrieveMultiple(query).Entities;
1. Juni 2015 14:50
2. Juni 2015 08:56
2. Juni 2015 09:41
3. Juni 2015 08:36
3. Juni 2015 09:09
DataCollection<Entity> entityCollection = _service.RetrieveMultiple(query).Entities;