You need to sign in to do that
Don't have an account?
kshannon
Governor limit issue with Trigger on child to rollup summary to parent
Hey all,
I'm trying to bulkify this trigger I wrote, seems my original setup is causing too many SOQL queries, clearly due to the soql being in the loop. I am working with a few different ways to fix this, but looking for some community guidance if possible. I am triggering on the Revenue object, which is related back to the Account. What I need to do is depending on the Canvas_Id__c, I am updating the overall total per canvas field on the account.
See code below:
I simplified this trigger to just the concept, also need to make the account update into a map so that i'm not constantly updating inside the loop as well.
Thanks all!
I'm trying to bulkify this trigger I wrote, seems my original setup is causing too many SOQL queries, clearly due to the soql being in the loop. I am working with a few different ways to fix this, but looking for some community guidance if possible. I am triggering on the Revenue object, which is related back to the Account. What I need to do is depending on the Canvas_Id__c, I am updating the overall total per canvas field on the account.
See code below:
trigger ProductsSoldRollUp on Revenue__c (after delete, after insert, after undelete, after update) { public static List<Account> accountsList; public static List<Revenue__c> updateRevenueList; accountsList = new List<Account>(); String ProductRevenueRecordTypeId = [select Id from RecordType where (Name='Product Revenue') and (SobjectType='Revenue__c')].Id; String ShippingRevenueRecordTypeId = [select Id from RecordType where (Name='Shipping Revenue') and (SobjectType='Revenue__c')].Id; for (Integer i = 0; i < Trigger.new.size(); i++) { Integer total_quantity = 0; Account acct = [SELECT Royalties_Paid__c FROM Account WHERE Id = :Trigger.new[i].Account__c]; List<Revenue__c> revList = [SELECT Canvas_Id__c, Royalty__c,Quantity__c,RecordTypeId FROM Revenue__c WHERE Account__c = :Trigger.new[i].Account__c]; for(Revenue__c r : revList){ if(r.RecordTypeId == ProductRevenueRecordTypeId && r.Quantity__c != null){ if(r.Canvas_Id__c == Canvas__c.getInstance('1').Canvas_Id__c.intValue()){ // T-Shirt p_quantity += r.Quantity__c.intValue(); } else if(r.Canvas_Id__c == Canvas__c.getInstance('2').Canvas_Id__c.intValue()){ // Tank t_quantity += r.Quantity__c.intValue(); } else if(r.Canvas_Id__c == Canvas__c.getInstance('3').Canvas_Id__c.intValue()){ // Kids e_quantity += r.Quantity__c.intValue(); } total_quantity++; } } acct.p_total__c = p_quantity; acct.t_total__c = t_quantity; acct.e_total__c = e_quantity; update acct; } }
I simplified this trigger to just the concept, also need to make the account update into a map so that i'm not constantly updating inside the loop as well.
Thanks all!
I have updated code to reduce your SOQL quaries.
Please find the below trigger:
Please do let me know if it helps you.
Regards,
Mahesh
Thanks for the help on the SOQL out of the loop Naval.
We don't need to put into map to bulkify the code. If the values are already exists in the list, we can directly insert into DB.
if there is list of Accounts stored in lstAccounts then you can use the below:
Regards,
Mahesh
Yes if we are expecting duplicate records then thats true.
But in your scenario, we are storing the IDs in a set "accIdSet". Hence we will not get duplicate Ids any time.
Please do let me know if my thinking is wrong.
Regards,
Mahesh