You need to sign in to do that
Don't have an account?
rick82000
Rollup Summary For Lookup Relationship
Hello,
I am trying to roll up a custom amount fields based on the parent/child relationship on the account.
For Example:
- There is a Parent Company record, and two Child company records.
- Each Child Company has 100k in the field (Assets Under Advisement). I would like to sum the Child company Assets Under
- Advisement on the parent company. Hence the parent company Assets Under Advisement should be 200k.
Here is my Trigger for the same (I need help on how can I complete this trigger without running on Apex Governor limit for too many SOQL Queries. Please note that my trigger is not complete.Of course :smileytongue::)
trigger RecalculateAssetsUnderAdvicement on Account (after update) { Double AssetsTotal =0.0; Account[] Originator = Trigger.new; Account [] UpdateAcc = new Account[]{}; for (Account Origin : Originator){ for (Account OuterLoop : [select id, Assets_Under_Advicement__c from Account where id =: Origin.ParentId]) { for (Account ChildRecords: [select id, Assets_Under_Advicement__c from Account where id =: Origin.ParentId]) { AssetsTotal = ChildRecords.Assets_Under_Advicement__c; } OuterLoop.Assets_Under_Advicement__c = AssetsTotal; UpdateAcc.add(OuterLoop); } update UpdateAcc; } }
trigger RecalculateAssetsUnderAdvicement on Account (after update) {
Double AssetsTotal =0.0;
Account[] Originator = Trigger.new;
for (Account Origin : Originator){
if (Origin.ParentId != null) {
for (Account ChildRecords: [select id, Assets_Under_Advicement__c from Account where id =: Origin.ParentId]) {
AssetsTotal = ChildRecords.Assets_Under_Advicement__c;
}
}
update UpdateAcc;
}
}
I have around 2600 records that i had to iterate through. I wrote another method that worked for me. I simply added and subtracted the value to the parent record instead of re-summing all records.
Bob, I am curious to know do we have to do the Batch Apex Method if we have more than 1000 records?
Thanks for you help.
All Answers
I think you want something like the following:
All the parents/children are retrieved up front using minimal queries and stored in collections, then the collections are iterated and changes made as appropriate. The update is moved outside of the loop so that it can be accomplished with a single call.
Caveat - I haven't compiled this so there may be some typos, but the principle should work.
Bob Buzzard,
Thanks for responding. I know you warned me about typos, but I am confused on your half SOQL statement.
I am not sure if you are trying to reference a specific account or how does this part of the trigger works.
Thank you for your help in advance.
Ha - a somewhat larger typo than I anticipated. Revised code snippet below - as you can see the parent ids list is created slightly earlier now and used to retrieve all the parent accounts through a single SOQL query.
The MAP has a limit for 1000 records. So does the list. How do you get over that limit?
What are your data volumes then?
I have around 2600 records that i had to iterate through. I wrote another method that worked for me. I simply added and subtracted the value to the parent record instead of re-summing all records.
Bob, I am curious to know do we have to do the Batch Apex Method if we have more than 1000 records?
Thanks for you help.
Here's another alternative:
Salesforce Roll-Up Summary Trigger