+ Start a Discussion
gastoniengastonien 

trigger doesn't work in bulk

trigger avgofresiduals on child__c (after insert, after update, after delete,after undelete) {

    //Set<ID> ids = Trigger.newMap.keySet();
   
    Set<Id> AcctIds = new Set<Id>();    
    //Set<Id> AcctIds = Trigger.newMap.keySet();    
    
    List<child__c > ConList = new List<child__c >();
    
    if(trigger.isInsert || trigger.isUPdate) {
        for(child__c Con : trigger.New) {
            AcctIds.add(Con.Deal__c);     
        }  
    }
    if(trigger.isDelete || trigger.isUndelete) {
        for(child__c  Con : trigger.Old) {
            AcctIds.add(Con.Deal__c);     
        }  
    }       
    
     Map<Id, List<child__c >> AcctContactList = new Map<Id, List<child__c >>();
     
    ConList = [SELECT Id, Deal__c, Volume__c,
               FROM child__c 
               WHERE Deal__c IN : AcctIds 
               ORDER BY CreatedDate DESC 
               LIMIT 3
               ];
               
   List<deal__c> AcctList = new List<deal__c>();
               
    double j=0;

    for(child__c  Con : ConList) {

            if(con.Volume__c!= null) {
                j += con.Volume__c;
            } 
    
                       
    }    
    AcctList = [SELECT volumesum 
                FROM deal__c
                WHERE Id IN : AcctIds
                ];
    
   
        List<child__c > ContList = new List<child__c >();
        ContList = AcctContactList.get(Acc.Id);
        Acc.volumesum = j;
        
       
    }
    update AcctList;

}
Best Answer chosen by gastonien
BalajiRanganathanBalajiRanganathan
If you need to have deal with sum of last three child_c, then the below code should work.
trigger avgofresiduals on child__c (after insert, after update, after delete,after undelete) {

     Set<Id> AcctIds = new Set<Id>();    
    
    if(trigger.isInsert || trigger.isUPdate) {
        for(child__c Con : trigger.New) {
            AcctIds.add(Con.Deal__c);     
        }  
    }
    if(trigger.isDelete || trigger.isUndelete) {
        for(child__c  Con : trigger.Old) {
            AcctIds.add(Con.Deal__c);     
        }  
    }                     
   
       List<deal__c> AcctList = [SELECT volumesum, (SELECT Id, Deal__c, Volume__c, FROM childs__r ORDER BY CreatedDate DESC LIMIT 3) 
                FROM deal__c
                WHERE Id IN : AcctIds
                ];
    
  for(deal__c Acc : AcctList) {  
    double j=0;

    for(child__c  Con : Acc.childs__r) {

            if(con.Volume__c!= null) {
                j += con.Volume__c;
            }           
    } 
    Acc.volumesum = j;
        
   }
   update AcctList;
}

 

All Answers

PratikPratik (Salesforce Developers) 
Hi,

Thanks for sharing the details. Will you please let us know what error you are getting?

Thanks,
Pratik
gastoniengastonien
it works fine during an update and single insert but when performing a data load(bulk), the result(volumesum) give wrong results
BalajiRanganathanBalajiRanganathan
did you put your complete code?
1) why are using LIMIT 3 on your SOQL to get conList
2)I did not see where you have defined Acc and where you are populating AcctContactList
gastoniengastonien
trigger avgofresiduals on child__c (after insert, after update, after delete,after undelete) {

    //Set<ID> ids = Trigger.newMap.keySet();
   
    Set<Id> AcctIds = new Set<Id>();    
    //Set<Id> AcctIds = Trigger.newMap.keySet();    
    
    List<child__c > ConList = new List<child__c >();
    
    if(trigger.isInsert || trigger.isUPdate) {
        for(child__c Con : trigger.New) {
            AcctIds.add(Con.Deal__c);     
        }  
    }
    if(trigger.isDelete || trigger.isUndelete) {
        for(child__c  Con : trigger.Old) {
            AcctIds.add(Con.Deal__c);     
        }  
    }       
    
     Map<Id, List<child__c >> AcctContactList = new Map<Id, List<child__c >>();
     
    ConList = [SELECT Id, Deal__c, Volume__c,
               FROM child__c 
               WHERE Deal__c IN : AcctIds 
               ORDER BY CreatedDate DESC 
               LIMIT 3
               ];
               
   List<deal__c> AcctList = new List<deal__c>();
               
    double j=0;

    for(child__c  Con : ConList) {

            if(con.Volume__c!= null) {
                j += con.Volume__c;
            } 
    
                       
    }    
    AcctList = [SELECT volumesum 
                FROM deal__c
                WHERE Id IN : AcctIds
                ];
    
  for(deal__c Acc : AcctList) {  
        List<child__c > ContList = new List<child__c >();
        ContList = AcctContactList.get(Acc.Id);
        Acc.volumesum = j;
        
       
    }
    update AcctList;

}


sorry just added  acc, limit 3 because I want to get only the last 3 records
BalajiRanganathanBalajiRanganathan

do you want to get only 3 records or 3 records for each deal.?
i think you are looking for 3 records for each deal.

you should write your SOQL(parent-child relationship query) something like below and process it.

SELECT volumesum, (SELECT Id, Deal__c, Volume__c, FROM childs__c ORDER BY CreatedDate DESC LIMIT 3) FROM deal__c WHERE Id IN : AcctIds ]

 

gastoniengastonien
Im getting this error:Didn't understand relationship 'childs__c ' in FROM part of query call. If you are attempting to use a custom relationship, be sure to append the '__r' after the custom relationship name. Please reference your WSDL or the describe call for the appropriate names. 
gastoniengastonien
ok I got got it running but still doesn't sum up well in bulk
BalajiRanganathanBalajiRanganathan
can you post your updated code?
gastoniengastonien
trigger avgofresiduals on child__c (after insert, after update, after delete,after undelete) {

    //Set<ID> ids = Trigger.newMap.keySet();
   
    Set<Id> AcctIds = new Set<Id>();    
    //Set<Id> AcctIds = Trigger.newMap.keySet();    
    
    List<child__c > ConList = new List<child__c >();
    
    if(trigger.isInsert || trigger.isUPdate) {
        for(child__c Con : trigger.New) {
            AcctIds.add(Con.Deal__c);     
        }  
    }
    if(trigger.isDelete || trigger.isUndelete) {
        for(child__c  Con : trigger.Old) {
            AcctIds.add(Con.Deal__c);     
        }  
    }       
    
     Map<Id, List<child__c >> AcctContactList = new Map<Id, List<child__c >>();
     
    ConList = [SELECT Id, Deal__c, Volume__c,
               FROM child__c 
               WHERE Deal__c IN : AcctIds 
               ORDER BY CreatedDate DESC 
               LIMIT 3
               ];
               
   List<deal__c> AcctList = new List<deal__c>();
               
    double j=0;

    for(child__c  Con : ConList) {

            if(con.Volume__c!= null) {
                j += con.Volume__c;
            } 
    
                       
    }    
    AcctList = [SELECT volumesum, (SELECT Id, Deal__c, Volume__c, FROM childs__c ORDER BY CreatedDate DESC LIMIT 3) 
                FROM deal__c
                WHERE Id IN : AcctIds
                ];
    
  for(deal__c Acc : AcctList) {  
        List<child__c > ContList = new List<child__c >();
        ContList = AcctContactList.get(Acc.Id);
        Acc.volumesum = j;
        
       
    }
    update AcctList;

}

there is another trigger on this object(a before trigger) that create the childs and links it to the parent, any idea on how to fix this one so it work on insert?( create child , link to parent and then sum)  is it a way to fire a trigger after like an hour or so? because this trigger works fine in an update
BalajiRanganathanBalajiRanganathan
If you need to have deal with sum of last three child_c, then the below code should work.
trigger avgofresiduals on child__c (after insert, after update, after delete,after undelete) {

     Set<Id> AcctIds = new Set<Id>();    
    
    if(trigger.isInsert || trigger.isUPdate) {
        for(child__c Con : trigger.New) {
            AcctIds.add(Con.Deal__c);     
        }  
    }
    if(trigger.isDelete || trigger.isUndelete) {
        for(child__c  Con : trigger.Old) {
            AcctIds.add(Con.Deal__c);     
        }  
    }                     
   
       List<deal__c> AcctList = [SELECT volumesum, (SELECT Id, Deal__c, Volume__c, FROM childs__r ORDER BY CreatedDate DESC LIMIT 3) 
                FROM deal__c
                WHERE Id IN : AcctIds
                ];
    
  for(deal__c Acc : AcctList) {  
    double j=0;

    for(child__c  Con : Acc.childs__r) {

            if(con.Volume__c!= null) {
                j += con.Volume__c;
            }           
    } 
    Acc.volumesum = j;
        
   }
   update AcctList;
}

 
This was selected as the best answer