function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
aa143aa143 

Select accounts and get related contacts in inputCheckbox on a visualforce page

I am having a requirement when i select an account through lookup then its related contacts must come in form of check box, so we can select the contacts by checking check box and don't check what contacts we doesn't want.
eg: If i select an account and it is having 5 contacts which appear in form of checknboxes and i select 2 checkboxes and submit it by selecting 2 only. How can i achieve the functionaliy. please some 1 help me urgently needed.
Sonam_SFDCSonam_SFDC
Hi, This can be achieved by using a wrapper class to display the contacts related to the account, check the sample code on the link below which explains the concept of Wrapper class with a example:
https://developer.salesforce.com/page/Wrapper_Class
Arunkumar RArunkumar R
Please find the below sample code to achieve your problem,

Apex Class:

public class contactCheckbox
{
public contact selectedAccount{get;set;}

 public contactCheckbox(ApexPages.StandardController stdCtrl) {
selectedAccount=(Contact)stdCtrl.getRecord();
    }

     public List<SelectOption> getItems()
      {
   
      List<Contact> cntList=[select Name from Contact where AccountId=:selectedAccount.AccountID];
      List<SelectOption> options = new List<SelectOption>();
        
        for (Contact c:cntList) 
        {
            options.add(new SelectOption(c.Name,c.Name));
        }
        return options;
      }
      

}

Visualforce Page:

<apex:page standardController="Contact" extensions="contactCheckbox" showheader="false">

<apex:form >
<center>
<br/>
<br/>
        <apex:outputLabel value="Account Name"> &nbsp;&nbsp;
                
         <apex:inputField value="{!Contact.AccountId}" required="false" id="lid"><br/><br/><br/>
         <apex:actionsupport event="onchange" rerender="check"/>
         </apex:inputField>
                
         </apex:outputLabel>
         <apex:outputPanel id="check">
         <apex:outputLabel value="Contacts:" rendered="{!contact.AccountId!=null}">
         <apex:selectCheckboxes value="{!selectedAccount}">
            <apex:selectOptions value="{!items}"/>
        </apex:selectCheckboxes><br/>
        </apex:outputLabel>
        </apex:outputPanel>
</center>
</apex:form>

</apex:page>

For more details refer http://salesforcekings.blogspot.in/2014/05/getting-related-contact-records-as_27.html