У меня 36 полей, выровненных по 3 строки, поэтому у меня 12 строк. Для каждой строки мне нужно построить field3
как разницу между field1
- field2
. Эту операцию необходимо повторить для каждой строки. Моя трудность заключается в том, что я новичок в мире CRM и не могу найти решение или руководство. Прикрепляю небольшую схему и мой текущий код. Кроме того, все поля имеют разные имена, схема должна иметь представление. Я даже не могу искать тип, потому что они все одного типа.
Схема:
field1 field2 field3 = field1-field2
field4 field5 field6 = field4-field5
field7 field8 field9 = field7-field8
etc.
Код:
public class BudgetingOnChangeUpdateOffset : IPlugin
{
public void Execute(IServiceProvider serviceProvider)
{
ITracingService tracingService =
(ITracingService)serviceProvider.GetService(typeof(ITracingService));
IPluginExecutionContext context = (IPluginExecutionContext)
serviceProvider.GetService(typeof(IPluginExecutionContext));
if (context.InputParameters.Contains("Target") &&
context.InputParameters["Target"] is Entity)
{
Entity entity = (Entity)context.InputParameters["Target"];
if (entity.LogicalName != "budgeting")
return;
IOrganizationServiceFactory serviceFactory =
(IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);
try
{
var budget = (Money)entity.Attributes["budget"];
var consumptive = (Money)entity.Attributes["cons"];
var offset = budget.Value - consumptive.Value;
entity.Attributes["offset"] = offset;
service.Update(entity);
}
catch (Exception ex)
{
tracingService.Trace("MyPlugin: {0}", ex.ToString());
throw;
}
}
}
}