+ Start a Discussion
Mathieu CanyMathieu 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 :

http://boards.developerforce.com/t5/Apex-Code-Development/quot-Loop-must-iterate-over-a-collection-type-quot-Error/td-p/178457

 

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;
    }

 

 

bob_buzzardbob_buzzard

I'm guessing you are seeing the error on this line:

 

 

   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;
                             }
                 }
            }
        }
    } 

 

 

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.

 

 

 

 

chandra2ravichandra2ravi

 

     public list<Opportunity> opporto { get; private set; }
         public Opportunity total { get; private set; }
         public Opportunity totalsextant { get; private set; }
        
        public opportunityTotal(Opportunity o) {
              opporto.add(o);
                    total = new Opportunity(Amount = 0);
                    totalsextant = new Opportunity(Amount = 0);
                    for(Opportunity b:opporto) {
                        
                            if(b.Amount != null){
                            if(b.IsPrivate != false){
                            
                                  totalsextant.Amount += b.Amount;
                             }
                 }
            }
        }
        

 

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.

Mathieu CanyMathieu Cany

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 :

 

 

opporto.add(o);

 

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.

 

 

            public class opportunityTotal {
         public list <Opportunity> opporto { get; private set; }
         public Opportunity total { get; private set; }
         public Opportunity totalparemploye { get; private set; }
         public Opportunity totalparemployepercue { get; private set; }
         
        
        
        public opportunityTotal(Opportunity o) {
        			opporto.add(o);
                    total = new Opportunity(Amount__c = 0);
                    totalparemploye = new Opportunity(Commission_to_Sextant__c = 0);
                    totalparemployepercue = new Opportunity(Commission_to_Sextant__c = 0);
                    for(Opportunity b:opporto){
                            if(b.Amount__c != null){
                            {
                            
                                  totalparemploye.Commission_to_Sextant__c += b.Commision_owner_1__c + b.Amount_commision_2__c + b.Commission_1_Bis__c;
                            }
                              	  totalparemployepercue.Commission_to_Sextant__c += b.Commission_owner_percue__c;
                            }
            }
        }
    } 
    
    
    
     public List<OpportunityTotal> getnouvellevueparemploye() {
        List<OpportunityTotal> nouvellevueparemploye = new List<OpportunityTotal>();
        for(Opportunity o:[select name 
                from Opportunity
                ])  
                 {
            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;
    }
           
    
    

 

Regards.

 

 

bob_buzzardbob_buzzard

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:

 

 

 public opportunityTotal(Opportunity o) 
 {
    total = new Opportunity(Amount__c = 0);
    totalparemploye = new Opportunity(Commission_to_Sextant__c = 0);
    totalparemployepercue = new Opportunity(Commission_to_Sextant__c = 0);
    if(o.Amount__c != null){
    {
        totalparemploye.Commission_to_Sextant__c += o.Commision_owner_1__c + o.Amount_commision_2__c + o.Commission_1_Bis__c;
    }
                              	     
    totalparemployepercue.Commission_to_Sextant__c += o.Commission_owner_percue__c;
     }

     }
    }