Я создаю плагин, который будет равномерно распределять скидку.Я хочу поровну разделить сумму скидки на все продукты. Так что, если у продукта есть цена 2000, а у другого - 1000, поэтому я хочу дать скидку 300, так что она делится на сумму 200, то должна быть 2000 и 100-100 в crm.
public class DivideEqualDiscount : IPlugin
{
static IOrganizationService _service;
public void Execute(IServiceProvider serviceProvider)
{
IPluginExecutionContext context = (IPluginExecutionContext)
serviceProvider.GetService(typeof(IPluginExecutionContext));
ITracingService tracingService =
(ITracingService)serviceProvider.GetService(typeof(ITracingService));
if (context.InputParameters.Contains("Target") &&
context.InputParameters["Target"] is Entity)
{
Entity target = (Entity)context.InputParameters["Target"];
if (target.Attributes.Contains("new_amount"))
{
target["new_amount"] = "52";
}
if (target.Attributes.Contains("new_discount"))
{
target["new_discount"] = "52";
}
string fetchData = @"
<entity name='opportunityproduct' >
<attribute name='manualdiscountamount' />
<attribute name='priceperunit' />
<attribute name='volumediscountamount' />
<attribute name='quantity' />
<attribute name='extendedamount' />
</entity>
</fetch>";
EntityCollection ec = ExecuteFetch(fetchData);
foreach (var item in ec.Entities)
{
if (item.Attributes.Contains("msdyn_costpriceperunit"))
{
target["new_discountamount"] = "";
target["new_discountpercentage"] = "";
}
if (item.Attributes.Contains("quantity"))
{
}
if (item.Attributes.Contains("extendedamount"))
{
}
}
}
}
public static EntityCollection ExecuteFetch(string fetchXmlString)
{
return _service.RetrieveMultiple(new FetchExpression(fetchXmlString));
}
}
}