You need to sign in to do that
Don't have an account?
ChickenOrBeef
Need help writing more efficient loops
Hey everyone,
I've been running into "CPU Time Limit" errors recently, and I believe it has something to do with my sloppy use of loops within loops. So with that said, here's an example of how I normally handle loops within loops. For example, this could be a trigger where I want to simply count the number of Opportunities present under each Account:
I'm assuming that this isn't efficient since this code will loop through every Opp when looping through each Account, regardless if the Opp is attached to that Account or not. Then the IF statement filters down the Opps within each loop, but I'm assuming the code is still looping through every single Opp.
Is there a more efficient way to handle this loop within a loop? Or should I try to avoid loops within loops all together?
Thanks!
-Greg
I've been running into "CPU Time Limit" errors recently, and I believe it has something to do with my sloppy use of loops within loops. So with that said, here's an example of how I normally handle loops within loops. For example, this could be a trigger where I want to simply count the number of Opportunities present under each Account:
Set<String> accountsInTrigger = new Set<String>(); FOR(Opportunity o : opps){ IF(o.AccountId != NULL){ accountsInTrigger.add(o.AccountId); } } List<Opportunity> relatedOpps = [SELECT Id,AccountId FROM Opportunity WHERE AccountId In :accountsInTrigger]; FOR(String account : accountsInTrigger){ FOR(Opportunity opp : oppsInTrigger){ IF(opp.AccountId == account){ /* Insert code here */ } } }
I'm assuming that this isn't efficient since this code will loop through every Opp when looping through each Account, regardless if the Opp is attached to that Account or not. Then the IF statement filters down the Opps within each loop, but I'm assuming the code is still looping through every single Opp.
Is there a more efficient way to handle this loop within a loop? Or should I try to avoid loops within loops all together?
Thanks!
-Greg
Hope this helps.
All Answers
i like chickens
Just to clarify, I didn't mean that I would put DML statements where I put "Insert Code Here", but rather I would just have more code in there doing some work. The actual DML statements would take place outside of the loop. Is that what you were referring to?
Thanks,
Greg
If you are trying to get a count of Opportunities for an account, go with a Rollup Summary field for that on the Account object.
Where are you trying to use the above code?
Is it in a trigger on the Account object as that is where you would like to get a count of Opps for account?
But anyways, I would use an aggregate query to get the count of Opps for an Account using the below SOQL, if I need to.
Thanks,
Venkat
Actually, what I want to do is provide the ordered number on each opportunity. So if an account has three opps created on these dates:
10/7/14
10/23/14
11/13/14
Then I want to have a custom "Order Number" field be populated like this:
10/7/14 - Order Number = "1"
10/23/14 - Order Number = "2"
11/13/14 - Order Number = "3"
I'm assuming I'd have to loop through the Opps by the Created Date. But as I mentioned in my first post, I'm guessing it would be wrong to loop through every single opportunity when looping through each Account. Does that make sense?
Thanks,
Greg
Hope this helps.
In this case, it makes sense as Opportunity is a child of Account and you want to update each child with a value. Your FOR loop is simple to understand and easy to manipulate.