User Case :- we have user case we need to display all account Information with primitive data types Boolean as isSelected variable as Checkbox, 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 AccountOppWrapperDemoVF {
//Wrapper List
public List<AccountWrapper> accountWrapperList {get;set;}
//Opportunity List
Public List<Opportunity> selectedOppList {get;set;}
//Contacts List
Public List<Contact> selectedContacts {get;set;}
public AccountOppWrapperDemoVF(){
//initialization wrapper List memory
accountWrapperList = new List<AccountWrapper>();
//get the List of Accoun to Show
List<Account> accList = [Select Id, Phone, Name, Industry, Rating From Account];
//Loop
for(Account acc : accList){
//initialization wrapper
AccountWrapper accountwrap = new AccountWrapper();
accountwrap.accountObj = acc;
accountwrap.isSelected = false;
//adding values in wrapper List
accountWrapperList.add(accountwrap);
}
selectedOppList = new List<Opportunity>();
selectedContacts = new List<Contact>();
}
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);
}
public Boolean getHasSelectedAccount(){
for(AccountWrapper wrapper : accountWrapperList){
if(wrapper.isSelected){
return true;
}
}
return false;
}
//Wrapper Class
public class AccountWrapper{
public Account accountObj {get;set;}
public Boolean isSelected {get;set;}
//Wrapper Class Constructor
public AccountWrapper(){
this.accountObj = New Account();
this.isSelected = false;
}
}
}
Visualforce Page :-
<apex:page controller="AccountOppWrapperDemoVF">
<!--defines a section that allows the user to enter input and submit it using the
<apex:commandButton> or <apex:commandLink> component -->
<apex:form >
<apex:pageBlock title="Display Account Details" tabStyle="Account">
<apex:pageBlockTable value="{!accountWrapperList}" var="objWrapper">
<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:pageBlock>
<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: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:form>
</apex:page>
it will look like this :-
feature :-


