Recalculate Rollup Field using Plugin in Dynamics 365

I came across a situation where we need to update Rollup Field value when it’s related entity field value changed(New record added or update existing value). Rollup field calculating values based on related entity field. i.e., We have subgrid of related entity on the primary entity.

Rollup Field Value = Sum of related entity record field values

We need to register our plugin on Create and Update message of related entity field to test the below code.

Plugin Code :

// Main class..

public class RecalculateRollupFieldOnRelatedEntityPostOp : IPlugin
{
private Money rollupField;
Entity _relatedEntity;
EntityReference __relatedEntityRef;
decimal oldRollupFieldValue, newollupFieldValue;

// Execute method..
public void Execute(IServiceProvider serviceProvider)
{
var _context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
var _serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
var _service = _serviceFactory.CreateOrganizationService(_context.UserId);
var _tracingService = (ITracingService)serviceProvider.GetService(typeof(ITracingService));

if (_context.InputParameters.Contains("Target") && _context.InputParameters["Target"] is Entity)
{
var _primaryEntityContext = (Entity)_context.InputParameters["Target"];

if (_primaryEntityContext.LogicalName != "EntityLogicalName")
return;

if (_primaryEntityContext.Contains("RelatedEntityLookupSchemaName") ||
_primaryEntityContext.GetAttributeValue("RelatedEntityLookupSchemaName") != null)
{
UpdateRollupField(_service, _tracingService, _primaryEntityContext);
}

}

}

// Function to calcualte Rollup field..

private void UpdateRollupField(IOrganizationService _service, ITracingService _tracingService, Entity _primaryEntityContext)
{
__relatedEntityRef = _primaryEntityContext.GetAttributeValue("CRMLookupfieldName");

if (__relatedEntityRef != null)
{
_relatedEntity = _service.Retrieve(__relatedEntityRef.LogicalName, __relatedEntityRef.Id, new ColumnSet("relatedEntityRollupField"));
rollupField = _relatedEntity.GetAttributeValue("relatedEntityRollupField");
}
if (rollupField != null)
{
oldRollupFieldValue = _relatedEntity.GetAttributeValue("relatedEntityRollupField").Value;
// print the old value of the roll-up field
_tracingService.Trace("Old Rollupfield Value) = " + _relatedEntity.GetAttributeValue("relatedEntityRollupField"));

CalculateRollupFieldRequest crfrRequested = new CalculateRollupFieldRequest
{
Target = new EntityReference(__relatedEntityRef.LogicalName, __relatedEntityRef.Id),
FieldName = "relatedEntityRollupField"
};

CalculateRollupFieldResponse responseRequested = (CalculateRollupFieldResponse)_service.Execute(crfrRequested);

// read the entity and value of the rollup field
_relatedEntity = responseRequested.Entity;

// print the new value of the roll-up field
_tracingService.Trace("New Rollupfield Value) = " + _relatedEntity.GetAttributeValue("relatedEntityRollupField"));

newollupFieldValue = _relatedEntity.GetAttributeValue("relatedEntityRollupField").Value;
}
}
}

 

 

 

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s