+ Start a Discussion
Avnit KumarAvnit Kumar 

How display accounts and related contacts using wrapper and map?

Hello Peeps
I am quite new in salesforce so please anyone can help me. I have to display the Accounts in checkbox and their related contacts in selectoptions. condition is that when i tick the checkbox of any account and click on the clone button the their related contacts will be cloned. sorry forgot to tell you i've to create a custom clone button too. And i have to do this by using wrapper and map. Proper code will be appriciated.

Thanx in Advance
Jason FlippenJason Flippen
Hi Avnit Kumar,

Here's a sample Wrapper class that might help you.  You add an Account to it and then it populates a SelectOption List with the related Contacts.
 
public class AccountWrapper {
    public Account Record {get;set;}
    public List<SelectOption> ContactList {get;set:}
    
    public AccountWrapper(Account a_account) {
        Record = a_account;
        ContactList = getContactList(a_account.Id);
    }

    private List<SelectOption> getAccountList(Id a_accountId) {
        List<SelectOption> returnList = new List<SelectOption>();
        
        returnList.add(new SelectOption('','-- Select Contact --'));
        for (Contact cont : [SELECT Id, Name FROM Contact WHERE AccountId = :a_accountId ORDER BY Name]) {
            returnList.add(new SelectOption(cont.Id,cont.Name));
        }
        
        return returnList;
    }
}

In the Visualforce Page you can then reference the Wrapper Object variable, or iterate through the List of Wrapper objects to display the data.  Here's an example of both types variables.  Keep in mind that you need to instantiate them in the constructor method of your class or an "Action" method...
 
public AccountWrapper AcctWrapper {get;set;}

OR

public List<AccountWrapper> AcctWrapperList {get;set;}

I hope this helps.  Let me know if you need any additional details.

Jason