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
SalesforceLearnerSalesforceLearner 

I want to display Accounts & its Related Opportunities of a currently login Comunity User on VF page

My Controller :
public class AccountOpportunity {

    public String userid{get;set;}


    public AccountOpportunity (){
    userid = Userinfo.getUserId();

        fetchData();
    }
   
    public void fetchData(){
  
       List<Opportunity> op = [Select name,Id,AccountId,Amount from Opportunity ];
        //Parent Id set
        Set<id> parentIdSet = new Set<id>();
        //Create parent Id set 
        for(Opportunity OppRec :op){
            parentIdSet.add(OppRec.AccountId);
        }
        
        //Fetch all associated parents
        
        
        List<Account> allAssocaiatedAccounts = [Select name,id,AnnualRevenue,Industry from Account where Id IN : parentIdSet ];
        
        wrapperList = new list<myWrapperClass>();
        //For loop to set data
        for(Opportunity childRec : allOpps){
            //myWrapperClass wrapRec;
            for(Account parentRec :allAssocaiatedAccounts){
                if(parentRec.Id == childRec.AccountId){
                    myWrapperClass wrapRec = new myWrapperClass();
                    wrapRec.acc = parentRec;
                    wrapRec.opp = childRec;
                    wrapperList.add(wrapRec);
                }
            }
             
            //Adding Opportunities without account
            if(childRec.AccountId == null){
                    myWrapperClass wrapRec = new myWrapperClass();
                    //wrapRec.acc = null;
                    wrapRec.opp = childRec;
                    wrapperList.add(wrapRec);
            }
         
        } 
    }
   
 

public List<myWrapperClass> wrapperList {get; set;}

public class myWrapperClass{
    public Account acc{get;set;}
    public Opportunity opp{get;set;}
    public Boolean selected {get; set;} 
    public myWrapperClass() { 
         selected = false; 
      } 
}
}
Swayam@SalesforceGuySwayam@SalesforceGuy
Hi,

Hope Below Code Will Help :-
 
public class AccountOpportunity {
    List<myWrapperClass> wrapperList{get;set;}
    public Id AccId{get;set;}
    public AccountOpportunity()
    {
        if(Userinfo.getUserid() != null)
        {
            ID contactId = [Select contactid from User where id =: Userinfo.getUserid()].contactId;
            AccId  = [Select AccountID from Contact where id =: contactid].AccountId; 
        }
        fetchData();
    }
    public void fetchData(){
        wrapperList = new  List<myWrapperClass>();
        List<Account> accountList = [Select Id,Name, AnnualRevenue,Industry from Account where Id =: AccID] ; 
        List<Opportunity> opportunityList = [Select name,Id,AccountId,Amount from Opportunity where AccountId =: AccID  ];
        for(Account acc : accountList)
        {
            for(Opportunity opp : [Select name,Id,AccountId,Amount from Opportunity where AccountId =: acc.Id])
            {
                wrapperList.add(new myWrapperClass(acc,opp));
            }
        }
    }
    public class myWrapperClass{
        public Account acc{get;set;}
        public Opportunity opp{get;set;}
        public myWrapperClass(Account acc, Opportunity opp) {
            this.acc = acc;
            this.opp = opp;
        } 
    }    
}

--
Thanks,
Swayam 
@Salesforceguy