You need to sign in to do that
Don't have an account?
Nataku
Batchifying code
Hi Guys,
I'm running into to the governor limit - Too many SOQL query 101
My trigger works normally until I tried to dump 2.5k worth of records in. I thought I was batchifying code properly, but now I'm confused. (btw I tried moving the AggregatedResult out from - for(AggregatedResult ar) - to it's own line and it ran into the same issue. May be I'm looking in the wrong place?
Thanks,
I'm running into to the governor limit - Too many SOQL query 101
My trigger works normally until I tried to dump 2.5k worth of records in. I thought I was batchifying code properly, but now I'm confused. (btw I tried moving the AggregatedResult out from - for(AggregatedResult ar) - to it's own line and it ran into the same issue. May be I'm looking in the wrong place?
trigger HQDisplayTotalNewAccount on Account (after insert, after undelete) { //variable to keep all HQ IDs for processing Set<Id> parentIds = new Set<Id>(); //grab what was being saved first for (Account acc : Trigger.new) { //process if a child, else change the values back if (acc.ParentId != null) { parentIds.add(acc.ParentId); } } if (parentIds.size() > 0) { List<Account> updates = new List<Account>(); // Leave the job of calculating the sum to the database for (AggregateResult ar : [ SELECT ParentId p ,sum(Couture_Display__c) sumCD ,sum(Abode_Display__c) sumAD ,sum(KW_Gallery_Display__c) sumKGD ,sum(KW_Gallery_Cabinet__c) sumKGC ,sum(KW_Tower_Display__c) sumKTD ,sum(KW_Wire_Rack__c) sumKWR ,sum(KW_Mod_10__c) sumKMT ,sum(Evoke_Lam_Gallery_Display__c) sumELGD ,sum(Evoke_LV_Gallery_Display__c) sumELVG ,sum(Evoke_Gallery_Cabinet__c) sumEGC ,sum(Evoke_Tower_Display__c) sumETD ,sum(Evoke_Wire_Rack__c) sumEWR ,sum(Evoke_Mod_10__c) sumEMT ,sum(X2012_Sales__c) sumTW ,sum(X2013_Sales__c) sumTR ,sum(X2014_Sales__c) sumFO FROM Account WHERE ParentId in :parentIds GROUP BY ParentId ]) { // Update without the cost of a preceding query updates.add(new Account(Id = (Id) ar.get('p') , Couture_Display__c = (Decimal) ar.get('sumCD') , Abode_Display__c = (Decimal) ar.get('sumAD') , KW_Gallery_Display__c = (Decimal) ar.get('sumKGD') , KW_Gallery_Cabinet__c = (Decimal) ar.get('sumKGC') , KW_Tower_Display__c = (Decimal) ar.get('sumKTD') , KW_Wire_Rack__c = (Decimal) ar.get('sumKWR') , KW_Mod_10__c = (Decimal) ar.get('sumKMT') , Evoke_Lam_Gallery_Display__c = (Decimal) ar.get('sumELGD') , Evoke_LV_Gallery_Display__c = (Decimal) ar.get('sumELVG') , Evoke_Gallery_Cabinet__c = (Decimal) ar.get('sumEGC') , Evoke_Tower_Display__c = (Decimal) ar.get('sumETD') , Evoke_Wire_Rack__c = (Decimal) ar.get('sumEWR') , Evoke_Mod_10__c = (Decimal) ar.get('sumEMT') , X2012_Sales__c = (Decimal) ar.get('sumTW') , X2013_Sales__c = (Decimal) ar.get('sumTR') , X2014_Sales__c = (Decimal) ar.get('sumFO') ) ); } // This update will cause this trigger to fire again update updates; } }Oh, and this trigger is suppose to add up each column from all the child under a parent and then update that column in the parent with the total.
Thanks,
You need to build a list like this:
List<List<<Account>> wrapperList = new List<List<Account>>();
List<Account> innerList = new List<Account>();
for( --- your aggregate results --- ar ) {
if(innerList.size() == 10000) {
wrapperList.add(innerList);
innerList = new List<Account>();
}
innerList.add(new Account( --- your aggregate object --- ar));
}
After this, you can loop through the wrapperList, getting a full list of accounts and updating those. You can perform the update inside a future method (using the annotation @future), which means the update will be done asynchronously, i.e. whenever your ORG gets the resources to perform the action.
I hope this helps! ;)
All Answers
Also, is it possible you have additional triggers on Account that could be inserting / cloning accounts? Perhaps you're running into some trigger recursion that could be calling the query over and over again. That's the only other thing I can think of.
sorry, I'm a bit of a noob at this. what did you mean by future methods? and (still looking this up) how does List<List<Account>> work ? I mean wouldn't the first list just contain one record, which is the list with the actual data?
@James Loghry
I just tried pulling the results and adding up the numbers myself instead of using sum within SOQL, but it's still throwing the same error
I do have another trigger that does some stuff on insert. I'm going to try and remove that trigger to see if thats the cause. (still poking through my list of tiggers to make sure nothing else is doing stuff on insert)
Thanks,
You need to build a list like this:
List<List<<Account>> wrapperList = new List<List<Account>>();
List<Account> innerList = new List<Account>();
for( --- your aggregate results --- ar ) {
if(innerList.size() == 10000) {
wrapperList.add(innerList);
innerList = new List<Account>();
}
innerList.add(new Account( --- your aggregate object --- ar));
}
After this, you can loop through the wrapperList, getting a full list of accounts and updating those. You can perform the update inside a future method (using the annotation @future), which means the update will be done asynchronously, i.e. whenever your ORG gets the resources to perform the action.
I hope this helps! ;)