I have a specific situation that I need to handle. I have a plugin that refreshes specific rollup field on invoice when invoice detail is created or updated. Now I need to refresh that field when invoice detail is deleted.
Analyzing this problem, I realized that I cannot refresh rollup field on pre operation because the invoice detail record is not deleted yet, and on post operation I cannot retrieve invoice Guid from that particular record because it is gone.
Here the piece of code that handles rollup refresh on create/update:
Entity invoiceDetail = service.Retrieve("invoicedetail", targetId, new ColumnSet(true));
Guid invoiceID = ((EntityReference)invoiceDetail["invoiceid"]).Id;
if (targetEntity.Attributes.Contains("extendedamount"))
{
Entity myEntity = service.Retrieve("invoice", invoiceID, new ColumnSet(true));
CalculateRollupFieldRequest rollupRequest = new CalculateRollupFieldRequest
{
Target = new EntityReference("invoice", invoiceID),
FieldName = "detailamount"
};
CalculateRollupFieldResponse response = (CalculateRollupFieldResponse)service.Execute(rollupRequest);
myEntity = response.Entity;
service.Update(myEntity);
}
Do you have any suggestions? I am going mad over this and can't think of anything...