I have written trigger on child object for calculating rollup summary.
Whenever child record insert, update or delete the sum of all child record tax value update in parent record.
Code Snippet:
List<Id> lstOrderId=new List<Id>();
List<ECS__eCommSource_Order__c> lstupdate = new List<ECS__eCommSource_Order__c>();
if(Trigger.isInsert)
{
for(ECS__Products_Purchased__c es:Trigger.New)
{
lstOrderId.add(es.ECS__Order__c);
}
}
if(Trigger.isUpdate || Trigger.isDelete)
{
for(ECS__Products_Purchased__c esp:Trigger.Old)
{
lstOrderId.add(esp.ECS__Order__c);
}
}
Map<Id,ECS__eCommSource_Order__c> MapOrder = new Map<Id,ECS__eCommSource_Order__c>([SELECT Id,Name FROM ECS__eCommSource_Order__c WHERE Id IN: lstOrderId]);
List<ECS__eCommSource_Order__c> lstOrder = [SELECT Id,Name,ECS__Tax_Total__c,Order_Line_Tax__c,(SELECT Id,Name,ECS__Order__c,ECS__Tax__c FROM ECS__Products_Purchased__r) FROM ECS__eCommSource_Order__c WHERE Id IN: lstOrderId];
if(lstOrder.size()>0 && !lstOrder.isEmpty())
{
for(ECS__eCommSource_Order__c es1:lstOrder)
{
Decimal totalcount =0;
if(es1.ECS__Products_Purchased__r.size()>0)
{
for(ECS__Products_Purchased__c ecp:es1.ECS__Products_Purchased__r)
{
if(ecp.ECS__Tax__c !=null)
{
totalcount +=ecp.ECS__Tax__c ;
}
}
}
MapOrder.get(es1.Id).Order_Line_Tax__c = totalcount;
lstupdate.add(MapOrder.get(es1.Id));
}
update lstupdate;
}
This is working fine when insert bulk records from anonymous window or manually from user interface.
But in my requirement, Parent and child records inserting the same time from external system.
At this time some parent records not updating with sum of child record tax value even values exists in child records.
Any help required to find the solution.