1. Cоздал проект, выбрал Dynamics CRM 2011 Plugin Library;
2. В CRM Explorer стал на Родительскую сущность и нажал Create Plug-in;
3. Зарегил плагин с такими параметрами:
Message = Create
Pipeline Stage Post Operation
Register New Image
Image Type : Post Image
Entity Alice : Post Image
Parameters : All Attributes
В обозревателе решения после действий из Tutorial у меня три CS файла - Sum (как имя класса при регистрации) и Plugin (создан автоматически), Entities (создан автоматически)
Какие мои дальнейшие действия?
Трассировка на сервере включена.
CRM Explorer/Plug-in Assemblies - пусто, нет моего плагина!
Плагин должен агрегировать данные по полю сумма из Дочерних записей и обновлять поле общая сумма в Родительской записи.
Вот код в файле Sum
Цитата:
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 ContractCalculation
{
public class Sum : 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 quantity = (Entity)Context.InputParameters["Target"];
var quantity = (EntityReference)quantity.Attributes["new_contract"];
decimal Total = FetchResult(quantity.Id, Service);
// Updating Parent Entity
Entity contract = new Entity("new_contract");
contract.Id = quantity.Id;
contract["new_total"] = Total;
Service.Update(contract);
}
}
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_request'>
<attribute name='new_insuransepayment' alias='totalamount_sum' aggregate='sum'/>
<filter type='and'>
<condition attribute='new_contract_new_request' 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).Value;
TotalValue = aggregate2;
}
return TotalValue;
}
}
}