using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xrm.Sdk;
using Microsoft.Crm.Sdk.Messages;
using Microsoft.Xrm.Sdk.Query;
namespace CrmPackageSalamandra.CountChildSum
{
public class CalculateTotalSum : IPlugin
{
public void Execute(IServiceProvider ServiceProvider)
{
IPluginExecutionContext Context = (IPluginExecutionContext)ServiceProvider.GetService(typeof(IPluginExecutionContext));
IOrganizationServiceFactory ServiceFactory = (IOrganizationServiceFactory)ServiceProvider.GetService(typeof(IOrganizationServiceFactory));
IOrganizationService Service = ServiceFactory.CreateOrganizationService(Context.UserId);
if (Context.PostEntityImages.Contains("PostImage") && Context.PostEntityImages["PostImage"] is Entity)
{
Цитата:
//Из контекста получаю сущность полис
Entity QuantityCust = (Entity)Context.InputParameters["Target"];
var Quantity = (EntityReference)QuantityCust.Attributes["new_contact"];
decimal Total = FetchResult(Quantity.Id, Service);
// Updating Parent Entity
Entity customer = new Entity("contact");
customer.Id = Quantity.Id;
customer["erpc_total"] = Total;
Service.Update(customer);
}
}
private static decimal FetchResult(Guid quantity, IOrganizationService service)
{
string value_sum = @"<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false' aggregate='true'>
<entity name='new_insurance_policy'>
<attribute name='new_payment' alias='totalamount_sum' aggregate='sum'/>
<filter type='and'>
<condition attribute='new_contact' operator='eq' value='{0}' />
</filter>
</entity>
</fetch>";
decimal TotalValue = 0;
value_sum = string.Format(value_sum, quantity);
EntityCollection value_sum_result = (EntityCollection)service.RetrieveMultiple(new FetchExpression(value_sum));
foreach (var c in value_sum_result.Entities)
{
decimal aggregate2 = ((Decimal)((AliasedValue)c.Attributes["totalamount_sum"]).Value);
TotalValue = aggregate2;
}
return TotalValue;
}
}
}