You need to sign in to do that
Don't have an account?
Mathieu Cany
LOOP MUST ITERATE OVER A COLLECTION TYPE: SOBJECT
Hello.
I've picked an interesting code on internet and used it. It's works well, but when I've modified it like at the code above, i've got an Error Code from Eclipse : "LOOP MUST ITERATE OVER A COLLECTION TYPE: SOBJECT : Opportunity"
I tryed the sollution provided here :
But it doesn't work. I'm locked In these situation for many hours now. Thanks for your support.
public class opportunityTotal { public Opportunity opporto { get; private set; } public Opportunity total { get; private set; } public Opportunity totalsextant { get; private set; } public opportunityTotal(Opportunity o) { opporto = o; total = new Opportunity(Amount__c = 0); totalsextant = new Opportunity(Commission_to_Sextant__c = 0); for(Opportunity b:o) { if(b.Amount__c != null){ if(b.Classer_le_dossier_dans_les_historiques__c != false){ totalsextant.Commission_to_Sextant__c += b.Commission_to_Sextant__c; } } } } } public List<OpportunityTotal> getnouvellevueparemploye() { List<OpportunityTotal> nouvellevueparemploye = new List<OpportunityTotal>(); for(Opportunity o:[select name,Amount,Amount__c, OwnerId, Owner.FirstName, Owner.LastName, Classer_le_dossier_dans_les_historiques__c, LeadSource, Commission_to_Sextant__c from Opportunity where CreatedDate < TODAY]) { nouvellevueparemploye.add(new OpportunityTotal(o)); } return sortOpportunityTotals(nouvellevueparemploye); } private List<OpportunityTotal> sortOpportunityTotals(List<OpportunityTotal> totals) { List<OpportunityTotal> returnList = new List<OpportunityTotal>(); Map<Decimal, List<OpportunityTotal>> totalMap = new Map<Decimal, List<OpportunityTotal>>(); for(OpportunityTotal t:totals) { if(totalMap.get(t.total.amount) == null) { totalMap.put(t.total.amount, new List<OpportunityTotal>()); } totalMap.get(t.total.amount).add(t); } List<Decimal> keys = new List<Decimal>(totalMap.keySet()); keys.sort(); /* Sort puts things in ascending order so for descending iterate over the keys backwards. */ for(Integer i = (keys.size()-1);i >= 0; i--) { returnList.addAll(totalMap.get(keys.get(i))); } return returnList; }
I'm guessing you are seeing the error on this line:
this is because you are trying to loop over a single opportunity, rather than a collection of opportunities. As your method only processes a single opportunity, you can do away with the for loop.
this is because you are trying to loop over a single opportunity, rather than a collection of opportunities.
please replace highlighted in red lines.
Thanks,
Ravi.
Thanks for your answer. The probleme is still here dispite the solution provided by chandra2ravi.
I added the red lines to my code and the following message appeared when I use it into my VF page :
System.NullPointerException: Attempt to de-reference a null object
The line 154, column 12 which correpond to your red line :
Here please find my code. I've simplified my SOQL query for assure that the reference a null object is not linked to fields into the query.
Regards.
There are a couple of problems here:
(1) Opporto is never initialised
(2) Opporto is never cleared down, meaning that each time through the method the list would grow.
As you are only ever processing one element, there's no need to use a list.
Try: