0

Does anyone know how to get the currently logged in community User Accounts & related opportunitites.

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

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

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

        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; 
      } 
}
}
0
public class AccountOpportunity {

public String userid{get;set;}
public list<myWrapperClass>  wrapperList {get;set;}

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

    fetchData();
}

public void fetchData(){

    //Fetch all associated parents

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

    ID AccID  = [Select AccountID from Contact where id =: contactid ].AccountId;
    List<Opportunity> allOpps = [Select name,Id,AccountId,Amount from Opportunity Where AccountId =:AccID ];
    wrapperList = new list<myWrapperClass>();
    //For loop to set data
    for(Opportunity childRec : allOpps){

                myWrapperClass wrapRec = new myWrapperClass();
                wrapRec.acc = parentRec;
                wrapRec.opp = childRec;
                wrapperList.add(wrapRec);



    } 
}



public class myWrapperClass{
    public Account acc{get;set;}
    public Opportunity opp{get;set;}
    public Boolean selected {get; set;} 
    public myWrapperClass() { 
         selected = false; 
      } 
}
}

VF page

<apex:page controller="AccountOpportunity">
   <apex:form>
   <apex:pageBlockTable value="{!wrapperList}" var="wrpr">

    <apex:column  value="{!wrpr.acc.Name}"/>
   <apex:column  value="{!wrpr.opp.Name}"/>
   <apex:column  value="{!wrpr.selected}"/>
    </apex:pageBlockTable>  
   <apex:form>  
</apex:page>

is this you want?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.