Toggle Side Panel

  • Home
  • Articles
    • All Articles
    • Blogs
    • Videos
    • Infographics
  • 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
  • Marketplace
  • Advertise With Us
  • Contact Us
  • Discussions
More options
    Sign in Sign up
    • Home
    • Articles
      • All Articles
      • Blogs
      • Videos
      • Infographics
    • 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
    • Marketplace
    • Advertise With Us
    • 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, 2 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

       

    • 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

    application solution

    Popular Salesforce Blogs

    Coveo For Salesforce in Community

    Coveo For Salesforce in Community

    Blog in Others

    Introduction Coveo provide AI-powered search for every Salesforce customer inside Service Cloud, Community Cloud, and App Cloud. Following key points for Coveo Search: It gives your…

    App Cloud, Code View, Community Cloud, Coveo, Coveo Cloud Platform
    Neha Sep 10, 2018
    4,538  Views

    Salesforce Workflow Vs Process Builder in Salesforce

    Blog in Others

    Workflow Rule By using Workflows we can: 1)  Update a field 2) Send an email 3) Create a Task 4) Send an outbound message Don’t…

    DML Operation, Process Limit Value, Salesforce Automation, Salesforce Classic, Salesforce Lightning Experience
    Sumit kumar Feb 24, 2020
    7,451  Views

    Data Cleansing and Migration: The Foundation for a Successful Salesforce Implementation in 2024

    Blog in Data Migration

    The success of a Salesforce implementation depends largely on the quality of data that powers it. Right from extracting the correct data from multiple sources,…

    Business Efficiency, CRM Data, Data Backup, Data Cleansing, Data Cleansing and Migration
    Carol Jun 3, 2024
    575  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

    © 2025 - 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®.

    Try AuditMyCRM - It is a Salesforce CRM Audit tool which comprehensively scans your Salesforce org and gives you the list of errors or warnings you need to take care of.
    We use cookies to enhance your browsing experience. Please see our privacy policy if you'd like more information on our use of cookies.