Filter Contact Based on Account
Requirement: Created a VF Page . Show All Accounts on Drop Down List, When Select Account then related contacts with that Account should be visible in table.
Solution: Create a VF page and Controller Class as mention below
Page Code
<apex:page controller="ControllerforAccount">
<apex:form >
<apex:pageBlock >
<apex:pageBlockSection columns="1" title="Account">
<apex:selectList value="{!selectedAccountId}" size="1">
<apex:selectOptions value="{!lstSelectOption}"></apex:selectOptions>
<apex:actionSupport event="onchange" action="{!AllContact}" reRender="contactlistId,outputID"/>
</apex:selectList>
<apex:outputLabel value="{!strMessage }" id="outputID"></apex:outputLabel>
</apex:pageBlockSection>
<apex:pageBlockSection columns="1" id="contactlistId" title="Contacts" >
<apex:pageBlockTable value="{!lstContact}" var="con" rendered="{!isShow}" >
<apex:column value="{!con.Name}"/>
<apex:column value="{!con.email}"/>
</apex:pageBlockTable>
</apex:pageBlockSection>
</apex:pageBlock>
</apex:form>
</apex:page>
Class Code
public class ControllerforAccount {
// string when you select an accound this variable will fill from account Id
public string selectedAccountId {get;set;}
public Boolean isshow{get;set;}
public string strMessage{get;set;}
public List<selectoption> lstSelectOption{get;set;}
public List<Contact> lstContact {get;set;}
public ControllerforAccount (){
lstContact = new List<Contact>();
isshow =false;
lstSelectOption = new List<selectoption>();
ShowAccountRecord();
}
// This method will return list of selectoption and will be bind in VF page in select list
/* public List<selectoption> getlstAccount(){
List<selectoption> lstSelectOption= new List<selectoption>();
lstSelectOption.add(new selectOption('None','-None-')); // adding first value in list as None
for(Account acc : [select Id,Name from Account]){
lstSelectOption.add(new selectoption(acc.Id,acc.name));
}
return lstSelectOption;
}*/
public void ShowAccountRecord()
{
lstSelectOption.add(new selectOption('None','-None-'));
for(Account acc : [select Id,Name from Account]){
lstSelectOption.add(new selectoption(acc.Id,acc.name));
}
}
//This method is calling from actionsupport action in VF page
public void AllContact(){
system.debug('selectedAccountId3333******** '+selectedAccountId);
lstContact = [select Id,Name,email from Contact where AccountId=:selectedAccountId ];
if(lstContact.size()>0 && lstContact!=null)
{
isshow = true;
strMessage ='';
}
else
{
isshow =false;
strMessage ='No Record found!!!';
}
}
}
snapshot
Thanks
Sumit Shukla
Mob-9711055997
SkypeId: shuklasumit1
sumitshukla.mca@gmail.com
Solution: Create a VF page and Controller Class as mention below
Page Code
<apex:page controller="ControllerforAccount">
<apex:form >
<apex:pageBlock >
<apex:pageBlockSection columns="1" title="Account">
<apex:selectList value="{!selectedAccountId}" size="1">
<apex:selectOptions value="{!lstSelectOption}"></apex:selectOptions>
<apex:actionSupport event="onchange" action="{!AllContact}" reRender="contactlistId,outputID"/>
</apex:selectList>
<apex:outputLabel value="{!strMessage }" id="outputID"></apex:outputLabel>
</apex:pageBlockSection>
<apex:pageBlockSection columns="1" id="contactlistId" title="Contacts" >
<apex:pageBlockTable value="{!lstContact}" var="con" rendered="{!isShow}" >
<apex:column value="{!con.Name}"/>
<apex:column value="{!con.email}"/>
</apex:pageBlockTable>
</apex:pageBlockSection>
</apex:pageBlock>
</apex:form>
</apex:page>
Class Code
public class ControllerforAccount {
// string when you select an accound this variable will fill from account Id
public string selectedAccountId {get;set;}
public Boolean isshow{get;set;}
public string strMessage{get;set;}
public List<selectoption> lstSelectOption{get;set;}
public List<Contact> lstContact {get;set;}
public ControllerforAccount (){
lstContact = new List<Contact>();
isshow =false;
lstSelectOption = new List<selectoption>();
ShowAccountRecord();
}
// This method will return list of selectoption and will be bind in VF page in select list
/* public List<selectoption> getlstAccount(){
List<selectoption> lstSelectOption= new List<selectoption>();
lstSelectOption.add(new selectOption('None','-None-')); // adding first value in list as None
for(Account acc : [select Id,Name from Account]){
lstSelectOption.add(new selectoption(acc.Id,acc.name));
}
return lstSelectOption;
}*/
public void ShowAccountRecord()
{
lstSelectOption.add(new selectOption('None','-None-'));
for(Account acc : [select Id,Name from Account]){
lstSelectOption.add(new selectoption(acc.Id,acc.name));
}
}
//This method is calling from actionsupport action in VF page
public void AllContact(){
system.debug('selectedAccountId3333******** '+selectedAccountId);
lstContact = [select Id,Name,email from Contact where AccountId=:selectedAccountId ];
if(lstContact.size()>0 && lstContact!=null)
{
isshow = true;
strMessage ='';
}
else
{
isshow =false;
strMessage ='No Record found!!!';
}
}
}
snapshot
Thanks
Sumit Shukla
Mob-9711055997
SkypeId: shuklasumit1
sumitshukla.mca@gmail.com
Comments
Post a Comment