1

I want to display contact and opportunity related to account i.e) when account name is selected from dropdown i need to display related records in VF page using wrapper class. How to achieve this? Could you please anyone post the code?

Below is the code I developed to display Opportunity, but how to display both Contact and Opportunity?

<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"/>
            </apex:selectList>
            <apex:pageBlockTable value="{!Opplist}" id="OppTable" var="o">
            <apex:column value="{!o.opp.Name}"/>
            </apex:pageBlockTable>
     </apex:pageBlockSection>
    </apex:pageBlock>
   </apex:form>
</apex:page>

Wrapper Class

public class WrapperOpp {

   public List<OppWrapper> Opplist = new List<OppWrapper>();
   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) from Account where name =:SelectedValue])
       {
         for (opportunity opp :a.opportunities)
         Opplist.add(new OppWrapper(false,opp));
       }
     return null;
     }

     public List<OppWrapper> getOppList()
     {
        System.debug('count'+Opplist.size());
        return Opplist;}

     public class OppWrapper{
    public Boolean selected{get;set;}
    public Opportunity opp{get;set;}
    public OppWrapper(Boolean selected1, Opportunity opp1)
    {
        selected = selected1;
        opp = opp1;
    }

}
}
6
  • 1
    Its a common functionality. Where are you exactly stuck?
    – Raul
    Commented Feb 28, 2017 at 7:34
  • I am new to sales force .i never used wrapper class ? I need full code to achieve this functionality using wrapper class..
    – Chithra
    Commented Feb 28, 2017 at 7:43
  • 1
    I would suggest you to try it out yourself. If you get stuck in between, post the code you tried and specify the point where you get stuck. Please do not ask here to post the code as a whole without even giving it a try. Wrapper Class. Here's another example for wrapper class.
    – SE_User
    Commented Feb 28, 2017 at 7:54
  • We cannot provide you with the code, i don't see any effort from your side. If you are new then learn the basics, try something and let us know when you have any specific questions.
    – Raul
    Commented Feb 28, 2017 at 7:56
  • Follow this link , it will help you with example . Commented Feb 28, 2017 at 8:13

1 Answer 1

1

You're on the right track. You just need to replicate the same code you used for the opportunity and use it for the contact:

  • Create a new wrapper for the contact
  • Create a new list for that wrapper
  • Add a getter method to expose it on the page
  • Put the Contacts on the Account query
  • Populate the contact list

Take a look on the code below:

<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;
        }
    }
}
2
  • Can you add a TL;DR or analysis so it's not just a code dump?
    – Adrian Larson
    Commented Feb 28, 2017 at 15:35
  • @AdrianLarson done =D Commented Feb 28, 2017 at 15:42

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .