public class AccwithLLContactsClass {
    list<Account> lstaccount=new list<Account>();
    list<Contact> lstContact=new list<Contact>();
    public list<Contact> getconrecords(){
        system.debug('****Accname text value is'+accname);
        lstaccount=[Select id,name from Account where name=:accname];
            for(integer i=0;i<=lstaccount.size();i++)
        lstcontact=[Select id,name,accountId from contact where accountId in:accId];
        system.debug('**** List of Contact for test is'+lstcontact);
        return lstcontact;        
    set<string> accid=new set<string>();
    public pageReference showcontact(){
        return null;

    public string accname{set;get;}

In this line:

for(integer i=0;i<=lstaccount.size();i++)

you probably meant to write:

for(integer i=0;i<lstaccount.size();i++)

This is because the greatest offset that you can reference is one less than the number of elements in the list.

| improve this answer | |

An alternate way to code your getter is as follows:

public list<Contact> lstContact{
      if (this.lstContact == null) {
          this.lstContact = new List<Contact>();
          for (Account a: [select id,
                             (select id, name,accountId from Contacts)
                            from Account where name = :this.accName]) 
      return this.lstcontact;
  private set;

One SOQL statement, no side effects by updating other variables, and efficiently reusable. If the cached value needs to be requeried, some actionMethod need merely set this.lstContact to null.

| improve this answer | |
  • solid​​​​​​​​​​ – martin Jan 25 '16 at 9:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.