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
kishore yakkalakishore yakkala 

write apex class to fetch all opportunity records related to accounts

write apex class to fetch all opportunity records related to accounts
like---
account1----having---opportunity 1
                                  opportunity 2
                                  opportunity 3
ANUTEJANUTEJ (Salesforce Developers) 
Hi Kishore,

You can check this below snippet that has an implementation of showing account and related opportunities in a visualforce 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);
        
    }
}

You can use the above apex class that fetches the accounts and related opportunities.

reference: https://developer.salesforce.com/forums/?id=9062I000000XuSyQAK

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.  

Thanks.
ravi soniravi soni
hy,
try following apex class and complete your requirment.
public class fetchAllOppRelatedAccount {

    @AuraEnabled
    public static void fetchAllRelatedOpp(){
        Map<Id,Opportunity> AccOppMap = new Map<Id,Opportunity>();
        for(Account acc : [SELECT Id,Name,(SELECT Id,Name From Opportunities) FROM Account]){
            for(opportunity opp : acc.opportunities){
                AccOppMap.put(acc.Id,opp);
            }
            system.debug('accId ===> ' + acc.Id);
            }
     
        system.debug('AccOppMap size() ===> ' + AccOppMap.size());
        system.debug('AccOppMap ===> ' + AccOppMap);
                     
    }
}
//How To Check This Apex Class
In your Anonymous window copy And paste following line.
fetchAllOppRelatedAccount.fetchAllRelatedOpp();

let me know if it helps you and close your query by marking it as best.
Thank you