Question 1 of 25
What is a potential design issue with the following code? trigger accountTrigger on Account (before update){ Boolean processOpportunity = false; List<opportunity> opptysClosedLost = new List<opportunity>() List<opportunity> IstAllOpp = [select StageName from Opportunity where accountId IN :Trigger.newMap.keySet()]; if(!IstAllOpp.isEmpty()) processOpportunity = true; while(processOpportunity) { for(opportunity o : IstAllOpp) if(o.StageName == 'Closed - Lost') opptysClosedLost.add(o); processOpportunity = false; if(!opptysClosedLost.isEmpty()) delete opptysClosedLost;
Show answer
How others answered
- D50%
- B50%
Only 50% agreed on this answer — treat it as contested rather than settled.
Explanation
Here is a better format of the code in case someone else wants to compare: trigger accountTrigger on Account (before update){ Boolean processOpportunity = false; List opptysClosedLost = new List (); List IstAllOpp = [select StageName from Opportunity where accountId IN :Trigger.newMap.keySet()]; if(!IstAllOpp.isEmpty()){ processOpportunity = true; while(processOpportunity){ for(opportunity o : IstAllOpp){if(o.StageName == 'Closed - Lost'){ opptysClosedLost.add(o); processOpportunity = false;}}} if(!opptysClosedLost.isEmpty()){delete opptysClosedLost;} The design flaw is that neither loop is necessary, a proper query could have collected the correct records to begin with and then delete them.
Adapted from community discussion by lorenac2 · 2022-12-30 · 3 community upvotes. Third-party contribution; reviewed by our quality filter, not independently verified.
Supporting references
