Toggle Side Panel

  • Blogs
  • Consultants
    • Salesforce Product Expertise
      • Top Salesforce ConsultantsTop Salesforce Consultants
      • Marketing Cloud ConsultantsMarketing Cloud Consultants
      • Service Cloud ConsultantsService Cloud Consultants
      • Experience Cloud ConsultantsExperience Cloud Consultants
      • Analytics Cloud ConsultantsAnalytics Cloud Consultants
    • Salesforce Industry Expertise
      • Non-Profit Cloud ConsultantsNon-Profit Cloud Consultants
      • Financial Service Cloud ConsultantsFinancial Service Cloud Consultants
      • Health Cloud ConsultantsHealth Cloud Consultants
      • Commerce Cloud ConsultantsCommerce Cloud Consultants
      • Manufacturing Cloud ConsultantsManufacturing Cloud Consultants
    • Salesforce Experts by Location
      • USATop Salesforce Consultants in USA
      • IndiaTop Salesforce Consultants in India
      • AustraliaTop Salesforce Consultants in Australia
      • United KingdomTop Salesforce Consultants in UK
      • CanadaTop Salesforce Consultants in Canada
  • Webinars
  • Contact Us
  • Discussions
More options
    Sign in Sign up
    • Blogs
    • Consultants
      • Salesforce Product Expertise
        • Top Salesforce ConsultantsTop Salesforce Consultants
        • Marketing Cloud ConsultantsMarketing Cloud Consultants
        • Service Cloud ConsultantsService Cloud Consultants
        • Experience Cloud ConsultantsExperience Cloud Consultants
        • Analytics Cloud ConsultantsAnalytics Cloud Consultants
      • Salesforce Industry Expertise
        • Non-Profit Cloud ConsultantsNon-Profit Cloud Consultants
        • Financial Service Cloud ConsultantsFinancial Service Cloud Consultants
        • Health Cloud ConsultantsHealth Cloud Consultants
        • Commerce Cloud ConsultantsCommerce Cloud Consultants
        • Manufacturing Cloud ConsultantsManufacturing Cloud Consultants
      • Salesforce Experts by Location
        • USATop Salesforce Consultants in USA
        • IndiaTop Salesforce Consultants in India
        • AustraliaTop Salesforce Consultants in Australia
        • United KingdomTop Salesforce Consultants in UK
        • CanadaTop Salesforce Consultants in Canada
    • Webinars
    • Contact Us
    • Discussions
    Close search

    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

    • Salesforce® Discussions

      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 AM

      We 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, 11 months ago 5 Members · 5 Replies
      • Account Object
      • Contact List in Salesforce
      • Contact Names
      • Contact Object
      • Contact Record
      • Opportunity Object
      • Opportunity Record
      • Salesforce Opportunity
      • Salesforce Visualforce Page
    • 5 Replies
    • PRANAV

      Member
      April 12, 2018 at 10:44 AM

      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

      vf

       

    • [adinserter block='9']
    • Raghav Chaturvedi

      Member
      April 24, 2018 at 5:19 AM

      https://salesforce.stackexchange.com/questions/162365/need-to-display-contact-and-opportunity-related-to-account

    • Raghav Chaturvedi

      Member
      April 24, 2018 at 6:19 AM

      <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;
      }
      }

    • shariq

      Member
      September 20, 2018 at 6:59 PM

      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.

    • Parul

      Member
      September 20, 2018 at 7:08 PM

      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.

    • Public
    • All Members
    • My Connections
    • Only Me
    • Public
    • All Members
    • My Connections
    • Only Me
    • Public
    • All Members
    • My Connections
    • Only Me

    Popular Salesforce Blogs

    How to Manage Salesforce Integrations with Event Monitoring?

    How to Manage Salesforce Integrations with Event Monitoring?

    Blog in Salesforce

    Salesforce offers several features and tools designed to improve business processes. One of the main aspects of leveraging Salesforce is integrating with other systems to…

    Comprehensive Testing, Continuous Monitoring, Data Access, Data Integrity, Data Storage
    SP Aug 7, 2024
    632  Views

    Agentforce vs Traditional Chatbots: Why AI Agents Are the Future of Support

    Blog in Salesforce

    In recent years support systems evolved rapidly. Traditional chatbots serve basic tasks using rules and flows. AI agents (also called “agentic AI” or intelligent agents)…

    Chatbot Development Company
    Casey Sep 11, 2025
    924  Views

    How to Get the Sales Team to Use Salesforce

    Blog in Sales, Salesforce

    Salesforce is a strategic tool for the growth and evolution of companies, but if it is not used to its maximum capability, it may lose…

    Business, Business Cards, Calendar Management, Centralization, Closing Sales
    SkyPlanner Apr 21, 2022
    2,864  Views
    Footer Forcetalks logo

    support@forcetalks.com

    • twitterx

    Quick Links

    Advertise with Us

    Salesforce® Articles

    Dreamforce 2023

    Top Salesforce® Bloggers 2023

    Top Salesforce Consultants

    Get Listed

    Company

    Contact Us

    About Us

    Privacy Policy

    Terms & Conditions

    InsightHub

    Salesforce Blogs

    Salesforce Videos

    Salesforce Groups

    Salesforce Jobs

    © 2026 - Forcetalks ● All Rights Reserved

    Salesforce® is a trademark of Salesforce® Inc. No claim is made to the exclusive right to use “Salesforce”. Any services offered within the Forcetalks website/app are not sponsored or endorsed by Salesforce®.

    We use cookies to enhance your browsing experience. Please see our privacy policy if you'd like more information on our use of cookies.