+ Start a Discussion
Mohan Raj 33Mohan Raj 33 

Can any one help me : I want to apex code to count the how many Contacts and Opportunities in the particular account in the salesforce.

I have a apex controller as followingly :
Public class datareturn{

    //public String lst { get; set; }
    Public class innerClass{
         Account acct {get; set;}
         Contact cont {get; set;}
         Opportunity opps {get; set;}
         
         public innerClass(){
             this.acct = new Account();
             this.cont = new Contact();
             this.opps = new Opportunity();
             }
             }
             
              
              Public List<innerClass> lstList{get;set;}
                Public datareturn(){
                lstList = new List<innerClass>();
                 List<Account> lstAccount = [Select Id, Name, (Select Id, Name, Email, Phone from Contacts), 
                                                (Select Id, Name, Amount, CloseDate from Opportunities) From Account];
            
                system.debug(lstAccount);
                if(lstAccount.size()>0){
                   
                        
                         for(Account acct:lstAccount){
                            ///lst.acct = acct;
                            List<Contact> contactlist = acct.Contacts;
                            List<Opportunity> opportunitylist = acct.Opportunities;
                          
                               Integer contactsize = acct.Contacts.size();
                               system.debug(contactsize);
                                Integer Opportunitysize = acct.Opportunities.size();
                                system.debug(Opportunitysize);
                                
                                Integer n;
                               if(contactsize >Opportunitysize){
                                        n = contactsize;
                                       }
                                       
                                      else{
                                       n = Opportunitysize;
                                       }
                               
                                for(Integer i = 0; i<n ; i++){
                                        innerClass lst = new innerClass();
                                           lst.acct = acct;
                                         lst.cont =  contactlist[i];
                                         lst.opps =  opportunitylist[i];
                                     lstList.add(lst);
                                     system.debug(lstList);
                                     }
                             //   }
                                   
                               }
                              
                                 
                           }
                          
                   }
            
            }
But I Have an error : System.ListException: List index out of bounds: 0
in the line 50. So that's why I want a help. 
Best Answer chosen by Mohan Raj 33
AshlekhAshlekh
Hi,

You are facing issue because suppose the size of Contact list is 6 and opportunity list size is 4.

Now according to your below logic n will be 6 ==> n=6.  
==== 6 > 4
if(contactsize >Opportunitysize){
                                        n = contactsize; n = 6
                                       }
                                       
                                      else{
                                       n = Opportunitysize;
                                       }
Now you using for loop untill value of i lessthan n(6)
for(Integer i = 0; i<n ; i++){
Now suppose the value of i = 5 then what happpen
                            
innerClass lst = new innerClass();
  lst.acct = acct;   
  lst.cont =  contactlist[i];  //Contact list size is 6 and now value of i is 5, it is okay. 
  lst.opps =  opportunitylist[i]; //oppty list size is 4 and now value of i is 5, opp[5] will give you error.
I think you have got it.

Solution 
innerClass lst = new innerClass();   
lst.acct = acct;   
lst.cont =  contactlist[i]; 
if(opportunitylist.size()>i)
lst.opps =  opportunitylist[i];
-Thanks
Ashlekh Gera


                                  

All Answers

AshlekhAshlekh
Hi,

You are facing issue because suppose the size of Contact list is 6 and opportunity list size is 4.

Now according to your below logic n will be 6 ==> n=6.  
==== 6 > 4
if(contactsize >Opportunitysize){
                                        n = contactsize; n = 6
                                       }
                                       
                                      else{
                                       n = Opportunitysize;
                                       }
Now you using for loop untill value of i lessthan n(6)
for(Integer i = 0; i<n ; i++){
Now suppose the value of i = 5 then what happpen
                            
innerClass lst = new innerClass();
  lst.acct = acct;   
  lst.cont =  contactlist[i];  //Contact list size is 6 and now value of i is 5, it is okay. 
  lst.opps =  opportunitylist[i]; //oppty list size is 4 and now value of i is 5, opp[5] will give you error.
I think you have got it.

Solution 
innerClass lst = new innerClass();   
lst.acct = acct;   
lst.cont =  contactlist[i]; 
if(opportunitylist.size()>i)
lst.opps =  opportunitylist[i];
-Thanks
Ashlekh Gera


                                  
This was selected as the best answer
Mohan Raj 33Mohan Raj 33
Thank you very much for your help.