+ Start a Discussion
m 3m 3 

create a vf page by having account 3 fields,related contact 3 fields and opportunity 3 fields using wrapper class?

Khan AnasKhan Anas (Salesforce Developers) 
Hi,

Greetings to you!

Please try the below code, I have tested in my org and it is working fine. Kindly modify the code as per your requirement.

Visualforce:
<apex:page controller="AccConOppWrapperC" >
    <apex:form >
        <apex:pageBlock >
            <apex:pageBlockSection >
                <apex:pageBlockTable value="{!Accs}" var="A">
                    <apex:column headerValue="NAME OF THE ACCOUNT" > 
                        <apex:commandLink value="{!A.Name}" action="{!refresh}" reRender="CtcTable, OppTable">
                            <apex:param value="{!A.Id}" name="idForConts" assignTo="{!recid}"/>
                        </apex:commandLink>
                    </apex:column>  
                    <apex:column value="{!A.Phone}"/>
                    <apex:column value="{!A.Rating}"/>
                </apex:pageBlockTable>
            </apex:pageBlockSection>
            
            <apex:pageBlockSection >
                <apex:pageBlockTable value="{!ctclist}" id="CtcTable" var="con">
                    <apex:column headerValue="CONTACT NAME" value="{!con.c.Name}"/>
                    <apex:column value="{!con.c.Phone}"/>
                    <apex:column value="{!con.c.Email}"/>
                </apex:pageBlockTable>
            </apex:pageBlockSection>
            
            <apex:pageBlockSection >
                <apex:pageBlockTable value="{!Opplist}" id="OppTable" var="o">
                    <apex:column headerValue="OPPORTUNITY NAME" value="{!o.opp.Name}"/>
                    <apex:column value="{!o.opp.StageName}"/>
                    <apex:column value="{!o.opp.CloseDate}"/>
                </apex:pageBlockTable>
            </apex:pageBlockSection>
            
        </apex:pageBlock>
    </apex:form>
</apex:page>

Controller:
public class AccConOppWrapperC {
    
    public List<OppWrapper> Opplist = new List<OppWrapper>();
    public List<ContactWrapper> ctclist = new List<ContactWrapper>();
    public String SelectedValue { get; set; }
    public string recid{get;set;}
    
    public List<Account> Accs {
        get{
            List<Account> AccName = new List<Account>();
            for(Account a :[Select Id, name, phone, rating from Account limit 10]){
                AccName.add(a);
            }
            return AccName;
        }
    }
    public PageReference refresh(){
        Opplist.clear();
        ctclist.clear();
        for(Account a :[Select id,name, phone, rating,(Select name,stagename, closedate from opportunities), (Select Name, phone, email from contacts) from Account where Id =:recId]){
            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<ContactWrapper> 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;
        }
    }
}

I hope it helps you.

Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in the future. It will help to keep this community clean.

Thanks and Regards,
Khan Anas