Hi have a requirement to write a plugin that does a roll up in Microsoft Dynamics 2013,
I have written the code but the plug in does nothing , so i resorted to debugging and found out the code had an error
the sample code is below
namespace OfficeTest
{
public class Test : 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 != "plx_remmitance")
{
return;
}
try
{
var productionID = entity.GetAttributeValue<EntityReference>("plx_production").Id; // The Line which gennerates the error
string fetchxml = @"<fetch mapping='logical' version='1.0' distinct='false' output-format='xml-platform'>
<entity name='plx_remmitance'>
<attribute name='plx_amount' alias='totalamount' aggregate='sum'/>
<filter type='and'>
<condition value='" + productionID.ToString() + @"' attribute='plx_production' operator='eq' />
</filter>
</entity>
</fetch>";
IOrganizationServiceFactory serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);
EntityCollection result = service.RetrieveMultiple(new FetchExpression(fetchxml));
decimal total = 0;
foreach (var c in result.Entities)
{
total = ((Money)((AliasedValue)c["totalamount"]).Value).Value;
}
ColumnSet orderCols = new ColumnSet(new String[] { "plx_totalremiitanceamount" });
Entity Production = service.Retrieve("plx_production", productionID, orderCols);
Console.WriteLine(total);
Production.Attributes["plx_totalremiitanceamount"] = new Money(total);
service.Update(Production);
}
catch (FaultException<OrganizationServiceFault> ex)
{
throw new InvalidPluginExecutionException("An error occuredin Test", ex);
}
catch (Exception ex)
{
tracingService.Trace("Test: {0}", ex.ToString());
}
}
}
}
}
The image for the debug process is within

Will appreciate any suggestion or better ways to achieve the roll up field with plugins.
The Production ID field which is a look up to the Production entity is where the error comes from
var productionID = entity.GetAttributeValue<EntityReference>("plx_production").Id;
after this step in the debug it goes to the exception line
Am testing on a 365 environment which already has roll up field but Dynamics 2013 does not have.
Regards.
*This post is locked for comments
I have the same question (0)