+ Start a Discussion
SalesforceLearnerSalesforceLearner 

How to display Accounts & its related opportunities of a login User in Vf page

I am able to display All accounts with opportunities but I want to display it based on the login User.
Does anyone know how to display currently login user Accounts & its opportunities?
Best Answer chosen by SalesforceLearner
Swayam@SalesforceGuySwayam@SalesforceGuy
Hey Just Update your code to : 

 
public void getData()
	{
   

ID contactId = [Select contactid from User where id =: Userinfo.getUserid()].contactId;

ID AccID  = [Select AccountID from Contact where id =: contactid].AccountId;

// Use Account Id directly to filter the opprtunity
List<Opportunity> Opps = [Select name,Id,AccountId,Amount from Opportunity where Account =: AccID];
        
        Set<id> parentIdSet = new Set<id>();
       
        for(Opportunity OppertunityRec :allOpps){
            parentIdSet.add(OppertunityRec.AccountId);
        }
        
        //get all associated parents
        List<Account> allAccounts = [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);
                }
            }
             
            
    }

--
Thanks,
Swayam 
@salesforceguy

 

All Answers

Swayam@SalesforceGuySwayam@SalesforceGuy
Hi Anuhitha,   Create a  extension controller for VF page, get the Account ID from below query

ID contactId = [Select contactid from User where id =: Userinfo.getUserid()].contactId;

ID AccID  = [Select AccountID from Contact where id =: contactid].AccountId;

then with this account id you can proceed, Let me know if you have any doubts



--
Thanks
Swayam
@salesforceguy
SalesforceLearnerSalesforceLearner
Hi Swayam thanks for the response  I am not understanding where should i add the contact id to the account
Here is my controller :
public void getData(){
    userid = Userinfo.getUserId();
       List<Opportunity> Opps = [Select name,Id,AccountId,Amount from Opportunity];
        
        Set<id> parentIdSet = new Set<id>();
       
        for(Opportunity OppertunityRec :allOpps){
            parentIdSet.add(OppertunityRec.AccountId);
        }
        
        //get all associated parents
        List<Account> allAccounts = [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);
                }
            }
             
            
     }
Swayam@SalesforceGuySwayam@SalesforceGuy
Hey Just Update your code to : 

 
public void getData()
	{
   

ID contactId = [Select contactid from User where id =: Userinfo.getUserid()].contactId;

ID AccID  = [Select AccountID from Contact where id =: contactid].AccountId;

// Use Account Id directly to filter the opprtunity
List<Opportunity> Opps = [Select name,Id,AccountId,Amount from Opportunity where Account =: AccID];
        
        Set<id> parentIdSet = new Set<id>();
       
        for(Opportunity OppertunityRec :allOpps){
            parentIdSet.add(OppertunityRec.AccountId);
        }
        
        //get all associated parents
        List<Account> allAccounts = [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);
                }
            }
             
            
    }

--
Thanks,
Swayam 
@salesforceguy

 
This was selected as the best answer
Swayam@SalesforceGuySwayam@SalesforceGuy
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;
        } 
    }    
}

Hope This Help

--
Thanks,
Swayam
@salesforceguy