User Case :- we have user case we need to display all account Information with primitive data types Boolean as isSelected variable as Checkbox, we can Select Some Account in different Datatable and then again i can select some account from there if we click on the check box it will show the related Contact Details and Opportunity Details in the output section, and if contact is not present for the selected Accounts it will show the Message as "No Contact Available" "No Opportunity Available" so we will use wrapper class in apex and try to get all the data to visualforce
Apex Class :-
public class AccountSelectClassController {
public class Accountwrapper{
public Account accountObj {get;set;}
public Boolean isSelected {get;set;}
public Accountwrapper(){
this.accountObj = New Account();
this.isSelected = false;
}
}
//Wrapper List
public List<Accountwrapper> accountWrapperList {get;set;}
//Selected Account
public List<Account> accountSelectedList {get;set;}
//Opportunity List
Public List<Opportunity> selectedOppList {get;set;}
//Contacts List
Public List<Contact> selectedContacts {get;set;}
//Select Wrapper
public List<Accountwrapper> accountWrapperListSelect {get;set;}
public AccountSelectClassController(){
//initialization wrapper List memory
accountWrapperList = new List<Accountwrapper>();
//get the List of Accoun to Show
List<Account> accList = [Select Id, Name, Phone, Industry, Rating from Account];
//Loop to put Account information in Wrapper class
for(Account acc : accList){
//initialization wrapper Class
Accountwrapper accwrap = new Accountwrapper();
accwrap.accountObj = acc;
accwrap.isSelected = false;
accountWrapperList.add(accwrap);
}
selectedOppList = new List<Opportunity>();
selectedContacts = new List<Contact>();
}
public void processSelected() {
accountSelectedList = new List<Account>();
accountWrapperListSelect = new List<AccountWrapper>();
for(Accountwrapper wrap : accountWrapperList){
if(wrap.isSelected == true){
accountSelectedList.add(wrap.accountObj);
wrap.isSelected = false;
accountWrapperListSelect.add(wrap);
}
}
}
public Boolean getHasSelectedAccount(){
for(AccountWrapper wrapper : accountWrapperList){
if(wrapper.isSelected){
return true;
}
}
return false;
}
public void getRelatedData(){
getRelatedOpportunity();
getRelatedContacts();
}
public void getRelatedOpportunity(){
selectedOppList = new List<Opportunity>();
Set<Id> accIds = new Set<Id>();
for(AccountWrapper wrapper : accountWrapperList){
if(wrapper.isSelected){
accIds.add(wrapper.accountObj.Id);
}
}
List<Opportunity> OpportunityList = [Select Id, Name, Account.Name From Opportunity Where AccountId IN : accIds];
selectedOppList.addAll(OpportunityList);
}
public void getRelatedContacts(){
selectedContacts = new List<Contact>();
Set<Id> accIds = new Set<Id>();
for(AccountWrapper wrapper : accountWrapperList){
if(wrapper.isSelected){
accIds.add(wrapper.accountObj.Id);
}
}
List<Contact> conList = [Select Id, Name, Account.Name From Contact Where AccountId IN : accIds ];
selectedContacts.addAll(conList);
}
}
Visualforce Page :-
<apex:page controller="AccountSelectClassController">
<apex:form >
<apex:pageBlock >
<apex:pageBlockButtons >
<apex:commandButton value="Show Selected Accounts" action="{!processSelected}" rerender="ShowSelectedAccounts"/>
</apex:pageBlockButtons>
<apex:pageBlockSection title="All Accounts" collapsible="false" columns="2">
<apex:pageBlockTable value="{!accountWrapperList}" var="objWrapper">
<apex:Column >
<apex:inputCheckbox value="{!objWrapper.isSelected}">
<apex:actionSupport event="onselect"/>
</apex:inputCheckbox>
</apex:Column>
<apex:column value="{!objWrapper.accountObj.Name}"/>
<apex:column value="{!objWrapper.accountObj.Industry}"/>
<apex:column value="{!objWrapper.accountObj.Phone}"/>
<apex:column value="{!objWrapper.accountObj.Rating}"/>
</apex:pageBlockTable>
<apex:pageBlockTable value="{!accountWrapperListSelect}" var="objWrapper" id="ShowSelectedAccounts" title="Selected Account">
<apex:Column >
<apex:inputCheckbox value="{!objWrapper.isSelected}">
<apex:actionSupport event="onchange" rerender="OpportunityDetails,ContactDetails" action="{!getRelatedData}"/>
</apex:inputCheckbox>
</apex:Column>
<apex:column value="{!objWrapper.accountObj.Name}"/>
<apex:column value="{!objWrapper.accountObj.Industry}"/>
<apex:column value="{!objWrapper.accountObj.Phone}"/>
<apex:column value="{!objWrapper.accountObj.Rating}"/>
</apex:pageBlockTable>
</apex:pageBlockSection>
</apex:pageBlock>
<apex:outputPanel id="ContactDetails">
<apex:pageBlock title="Related Contacts" rendered="{!HasSelectedAccount || selectedContacts.size > 0 }">
<apex:PageBlockTable value="{!selectedContacts}" var="Contact">
<apex:column value="{!Contact.Name}"/>
<apex:column value="{!Contact.Account.Name}"/>
</apex:PageBlockTable>
</apex:pageBlock>
<apex:outputPanel rendered="{!HasSelectedAccount && selectedContacts.size == 0 }" >
<div style="text-align:center;font-size: 25px;">
<b>No Related Contacts Found For The Account</b>
</div>
</apex:outputPanel>
</apex:outputPanel>
<apex:outputPanel id="OpportunityDetails">
<apex:pageBlock title="Related Opportunity" rendered="{!HasSelectedAccount || selectedOppList.size > 0 }">
<apex:pageBlockTable value="{!selectedOppList}" var="Opportunity">
<apex:column value="{!Opportunity.Name}"/>
<apex:column value="{!Opportunity.Account.Name}"/>
</apex:pageBlockTable>
</apex:pageBlock>
<apex:outputPanel rendered="{!HasSelectedAccount && selectedOppList.size == 0 }" >
<div style="text-align:center;font-size: 25px;">
<b>No Related Opportunity Found For The Account</b>
</div>
</apex:outputPanel>
</apex:outputPanel>
</apex:form>
</apex:page>
it will look like this :-
feature :-


