You need to sign in to do that
Don't have an account?
Mohan 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.
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.
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. Now you using for loop untill value of i lessthan n(6) Now suppose the value of i = 5 then what happpen
I think you have got it.
Solution -Thanks
Ashlekh Gera
All Answers
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. Now you using for loop untill value of i lessthan n(6) Now suppose the value of i = 5 then what happpen
I think you have got it.
Solution -Thanks
Ashlekh Gera