0

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.

4
  • How do you insert parent and child at the same time? You need the parent record id on the child, so the parent would have to be inserted first. Aug 19, 2016 at 7:40
  • 1
    @RobinDeBondt Not necessarily. If you have an external ID on the parent, you can set the lookup via that. I can't recall the details of whether that can be a single upsert statement though. Either way, the OP isn't using the external ID.
    – Nick C
    Aug 19, 2016 at 7:48
  • @Robin - yes, there is a milliseconds difference between parent and child insertion. But it is showing same time(hour and minute) in user interface. Aug 19, 2016 at 7:51
  • @Nick Cook. I think you are right. External id in parent and look up field value in child are same in my requirement. But how can i fix this issue. Aug 19, 2016 at 7:58

1 Answer 1

2

Don't neglect undelete. Also consider using an aggregate query.

List<Parent__c> parents = new List<Parent__c>() ;
for (AggregateResult aggregate : [
    SELECT Parent__c, count(Id) records
    FROM Child__c GROUP BY Parent__c
])
    parents.add(new Parent __c(
        Id=(Id)aggregate.get('Parent__c'),
        Summary__c = (Integer)aggregate.get('records')
    ));
update parents;
// error handling recommended
// omitted here for brevity

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .