Activity › Forums › Salesforce® Discussions › How to display names of Contact & Opportunity related to an Account in a Visualforce Page?
Tagged: Account Object, Contact List in Salesforce, Contact Names, Contact Object, Contact Record, Opportunity Object, Opportunity Record, Salesforce Opportunity, Salesforce Visualforce Page
-
How to display names of Contact & Opportunity related to an Account in a Visualforce Page?
Posted by Meddimala Ranjith on April 12, 2018 at 5:13 AMWe have 3 objects Account, Contact, Opportunity. In a VF page, we need to display
the names of contact & Opportunity which are related to Account.
Parul replied 7 years, 7 months ago 5 Members · 5 Replies -
5 Replies
-
Hi,
The below code will help you
Apex Class
public class AccountRelatedContactsOpportunities {
public List<Contact> getContacts() {
List<Contact> conresults = [Select Id, FirstName, LastName,Title,Email from Contact where accountid=’0012800000hVdfO’];
return conresults;
}
public List<Opportunity> getOpportunities() {
List<Opportunity> oppresults = [Select Id,CloseDate,Amount,StageName,Name from Opportunity where accountid=’0012800000hVdfO’];
return oppresults;
}}
VisualForce Page
<apex:page controller=”AccountRelatedContactsOpportunities”>
<apex:form>
<apex:pageBlock title=”Contacts List” id=”contacts_list”>
<apex:pageBlockTable value=”{! contacts }” var=”ct”>
<apex:column value=”{! ct.FirstName }”/>
<apex:column value=”{! ct.LastName }”/>
<apex:column value=”{! ct.Title }”/>
<apex:column value=”{! ct.Email }”/>
</apex:pageBlockTable>
</apex:pageBlock>
<apex:pageBlock title=”Opportunity List” id=”opportunity_list”>
<apex:pageBlockTable value=”{! opportunities }” var=”ot”>
<apex:column value=”{! ot.Name }”/>
<apex:column value=”{! ot.CloseDate }”/>
<apex:column value=”{! ot.StageName }”/>
<apex:column value=”{! ot.Amount }”/>
</apex:pageBlockTable>
</apex:pageBlock>
</apex:form>
</apex:page>Here I am using hard coded Account Id. You can replace it with yours or you can fetch it dynamically also.
The below is the preview of VF page
- [adinserter block='9']
-
-
<apex:page controller=”AccountContactOpportunity”>
<apex:form >
<apex:pageBlock title=”Account Name”>
<apex:selectList value=”{!AccId}” size=”1″>
<apex:selectOptions value=”{!AccountNames}”/>
<apex:actionSupport event=”onchange” action=”{!showContact}” reRender=”co,co1″/>
</apex:selectList>
<apex:pageBlock title=”Opportunity list” id=”co” >
<apex:pageBlockTable value=”{!opplist}” var=”opp”>
<apex:column value=”{!opp.Name}”/>
<apex:column value=”{!opp.StageName}”/>
</apex:pageBlockTable>
</apex:pageBlock>
<apex:pageBlock title=”Contact list” id=”co1″>
<apex:pageBlockTable value=”{!conlist}” var=”con”>
<apex:column value=”{!con.Name}”/>
<apex:column value=”{!con.Email}”/>
</apex:pageBlockTable>
</apex:pageBlock>
</apex:pageBlock>
</apex:form>
</apex:page>public class AccountContactOpportunity
{
public String AccId{get;set;}
public list<contact> conlist{get;set;}
public list<Opportunity> opplist{get;set;}public List<SelectOption> getAccountNames()
{
List<SelectOption> accOptions= new List<SelectOption>();
accOptions.add( new SelectOption(”,’–Select–‘));
for( Account acc : [select Id,name from Account where name like ‘a%’] )
{
accOptions.add( new SelectOption(acc.Id,acc.name));
}
return accOptions;
}
public pageReference showContact()
{
conlist=[select Name,Email from contact where accountid =:AccId];
system.debug(‘—–>’+conlist);
opplist=[select Name,stageName from opportunity where accountid=:AccId];
system.debug(‘====>’+opplist);
return null;
}
} -
Hi,
apex:page controller=”WrapperOpp” >
<apex:form >
<apex:pageBlock >
<apex:pageBlockSection >
<apex:selectList value=”{!SelectedValue}” size=”1″>
<apex:selectOptions value=”{!Accs}”/>
<apex:actionSupport event=”onchange” action=”{!refresh}” reRender=”OppTable, CtcTable”/>
</apex:selectList>
<apex:pageBlockTable value=”{!Opplist}” id=”OppTable” var=”o”>
<apex:column value=”{!o.opp.Name}”/>
</apex:pageBlockTable>
<apex:pageBlockTable value=”{!ctclist}” id=”CtcTable” var=”con”>
<apex:column value=”{!con.c.Name}”/>
</apex:pageBlockTable>
</apex:pageBlockSection>
</apex:pageBlock>
</apex:form>
</apex:page>public class WrapperOpp {
public List<OppWrapper> Opplist = new List<OppWrapper>();
public List<ContactWrapper> ctclist = new List<ContactWrapper>();
public String SelectedValue { get; set; }public List<SelectOption> Accs {
get{
List<SelectOption> AccName = new List<SelectOption>();
for(Account a :[Select Id, name from Account limit 10]){
AccName.add(new SelectOption(a.name,a.name));
}
return AccName;
}
}
public PageReference refresh(){
Opplist .clear();
for(Account a :[Select id,name,(Select name from opportunities), (Select FirstName from contacts) from Account where name =:SelectedValue]){
for (opportunity opp :a.opportunities) Opplist.add(new OppWrapper(false,opp));
for (Contact c : a.contacts) ctclist.add(new ContactWrapper(false,c));
}
return null;
}public List<OppWrapper> getOppList(){
System.debug(‘count’+Opplist.size());
return Opplist;
}public List<OppWrapper> getCtcList(){
System.debug(‘count’+ctcList.size());
return ctcList;
}public class OppWrapper{
public Boolean selected { get; set; }
public Opportunity opp { get; set; }
public OppWrapper(Boolean selected1, Opportunity opp1){
selected = selected1;
opp = opp1;
}
}public class ContactWrapper{
public Boolean selected { get; set; }
public Contact c { get; set; }public ContactWrapper(Boolean selected, Contact c){
this.selected = selected;
this.c = c;
}
}
}Hope this helps.
-
Adding some points:
<apex:page standardController=”Account”>
Name : <apex:outputField value=”{!Account.Name}”/> <br/>
Phone : <apex:OutputField value=”{!Account.Phone}” /><br/>
<br/>
<b>Contact List</b><br/>
<apex:dataTable value=”{!Account.Contacts}” var=”con”>
<apex:column value=”{!con.Name}”/>
</apex:dataTable><br/><br/>
<b>Opportunity List</b><br/>
<apex:dataTable value=”{!Account.Opportunities}” var=”opty”>
<apex:column value=”{!opty.Name}”/>
</apex:dataTable>
</apex:page>Thanks
Log In to reply.
