Skip to main content
1 of 2
Keith C
  • 137.3k
  • 29
  • 218
  • 458

Not sure why you are not just using your "Working Fine" solution...

If you want to use two loops for some reason, then you need to create the wrapper in the outer loop once only and collect the Contacts in the inner loop on that wrapper. Here is one way to do that:

for(Account accRec : accSearch) {
    WrapperAcc w = new WrapperAcc(accRec);
    wrapperLst.add(w);
    for (Contact gpa : accRec.contacts) {
        w.addGpa(gpa);
    }
}
public class WrapperAcc {
    public Account acc {get; private set;}
    public List<Contact> gpaList {get; private set;}
    WrapperAcc(Account acc) {
        this.acc = acc;
        this.gpaList = new List<Contact>();
    }
    void addGpa(Contact gpa) {
        gpaList.add(gpa);
    }
}
Keith C
  • 137.3k
  • 29
  • 218
  • 458