function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Chaitanya paraskarChaitanya paraskar 

How to display Account related Opportunities in vf page

Best Answer chosen by Chaitanya paraskar
sachinarorasfsachinarorasf
Hi Chaitanya,

Here is apex code and VF page. Please use the below code: 

---------------------------VF page-----------------------------

<apex:page controller="AccContact">
    <apex:form >
        <apex:pageBlock >
            <apex:pageBlockTable value="{!acclst}" var="a">
                
                <apex:column headerValue="Name">
                    <apex:commandLink action="{!selacc}">
                        <apex:param name="conlst" value="{!a.id}"/>
                        <apex:outputText value="{!a.name}"/>
                    </apex:commandlink>
                </apex:column>
                <!--accounts-->
            </apex:pageBlockTable>
            <apex:pageBlockTable value="{!accconts1}" var="a">
                <apex:column headerValue="AccountName" value="{!a.Name}"/>
                <!--contacts-->
                        <!--opportunities-->
                        <apex:column headerValue="Opportunity list">
                            <apex:pageBlockTable value="{!a.opportunities}" var="p">
                                <apex:column headerValue="Opportunity Name">
                                    <apex:outputText value="{!p.name}">
                                    </apex:outputText>
                                </apex:column>
                            </apex:pageBlockTable><!-- End opportunity-->
                        </apex:column>
            </apex:pageBlockTable><!--end account-->
        </apex:pageBlock>
    </apex:form>
</apex:page>


------------------------------Apex Class----------------------------
public with sharing class AccContact {
    
    public list<contact> conlst { get; set; }
    public list<account> accconts1 { get; set; }
    
    public PageReference selacc() {
        string getid=apexpages.currentpage().getparameters().get('conlst');
        accconts1=[select id,name, (select name from Opportunities ) from account where id=:getid ];
        system.debug(accconts1);
        return null;
    }
    
    
    public List<Account> acclst { get; set; }
    public  AccContact (){
        acclst=[select id,name  from account ];
        system.debug(acclst);
        
    }
}

Click the Account name on vf page and you will get a related opportunity.
I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Sachin Arora
www.sachinsf.com

All Answers

ManojjenaManojjena
HI Chaitantya ,

Please follow below code and remove contact details .

let me know incase any issue .

https://developer.salesforce.com/forums/?id=906F0000000g0WzIAI

Thanks ,
Manoj
sachinarorasfsachinarorasf
Hi Chaitanya,

Here is apex code and VF page. Please use the below code: 

---------------------------VF page-----------------------------

<apex:page controller="AccContact">
    <apex:form >
        <apex:pageBlock >
            <apex:pageBlockTable value="{!acclst}" var="a">
                
                <apex:column headerValue="Name">
                    <apex:commandLink action="{!selacc}">
                        <apex:param name="conlst" value="{!a.id}"/>
                        <apex:outputText value="{!a.name}"/>
                    </apex:commandlink>
                </apex:column>
                <!--accounts-->
            </apex:pageBlockTable>
            <apex:pageBlockTable value="{!accconts1}" var="a">
                <apex:column headerValue="AccountName" value="{!a.Name}"/>
                <!--contacts-->
                        <!--opportunities-->
                        <apex:column headerValue="Opportunity list">
                            <apex:pageBlockTable value="{!a.opportunities}" var="p">
                                <apex:column headerValue="Opportunity Name">
                                    <apex:outputText value="{!p.name}">
                                    </apex:outputText>
                                </apex:column>
                            </apex:pageBlockTable><!-- End opportunity-->
                        </apex:column>
            </apex:pageBlockTable><!--end account-->
        </apex:pageBlock>
    </apex:form>
</apex:page>


------------------------------Apex Class----------------------------
public with sharing class AccContact {
    
    public list<contact> conlst { get; set; }
    public list<account> accconts1 { get; set; }
    
    public PageReference selacc() {
        string getid=apexpages.currentpage().getparameters().get('conlst');
        accconts1=[select id,name, (select name from Opportunities ) from account where id=:getid ];
        system.debug(accconts1);
        return null;
    }
    
    
    public List<Account> acclst { get; set; }
    public  AccContact (){
        acclst=[select id,name  from account ];
        system.debug(acclst);
        
    }
}

Click the Account name on vf page and you will get a related opportunity.
I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Sachin Arora
www.sachinsf.com
This was selected as the best answer
Chaitanya paraskarChaitanya paraskar
Thankyou this was so helpful for me sachin
Chaitanya paraskarChaitanya paraskar
Thankyou Sachin Arora this was helpful for me