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
Hanumantha Rao 6Hanumantha Rao 6 

How to display all related opportunity records for a particular account using a visual force page

Hi i have a requirement please find the below req and suugget me , or send my any related code is already developed .

Display all related opp records for a particualr account with the below conditions
1) Opportunities which are status= TRUE 
2) These Opportunities should have at least one Product.
3) This VF page should support Pagination, each page should have max of 25 record.
This VF page should have sorting in all the columns.
4) VF page should have i/p check box has to be present in front of every record.


i developed VF page for pagination for displaying account records but dont know how to to display one account related opportunities , please help me on this or send any code 

Regards,
Hanu
Best Answer chosen by Hanumantha Rao 6
Mudasir WaniMudasir Wani
If you want to display all the accounts and opportunites then I have a code for you.
In the below code I am displaying all accounts and opportunities and a checkbox we also have a functionality where I am displaying only the selected records.
Have a look on the code and modify as per your needs.

#PAGE
<apex:page controller="wrapperAccountOpportunity" sidebar="false">
    <apex:form >
    <apex:commandButton value="Proceed with Selected" action="{!ProceedWithSelected}" reRender="panelId"/>
    <apex:commandButton value="Proceed with Selected to Next Page" action="{!ProceedWithSelectedToNextPage}" />
    <apex:pageBlock >
        <apex:outputPanel id="panelId">
            <apex:pageblockTable value="{!wrapperList}" var="wrapRec" rendered="{!normalList}">
               <apex:column value="{!wrapRec.acc.Name}" />
               <apex:column value="{!wrapRec.opp.Name}"/>
               <apex:column >
                   <apex:inputCheckbox value="{!wrapRec.selected}"/>
               </apex:column>
          </apex:pageblockTable>
          
           <apex:pageblockTable value="{!selectedWrapperList}" var="wrapRec" rendered="{!selectedList}">
               <apex:column value="{!wrapRec.acc.Name}" />
               <apex:column value="{!wrapRec.opp.Name}"/>
               <apex:column >
                   <apex:inputCheckbox value="{!wrapRec.selected}"/>
               </apex:column>
          </apex:pageblockTable>
        </apex:outputPanel>      
    </apex:pageBlock>
    </apex:form>
</apex:page>

#CLASS
public class wrapperAccountOpportunity {
    
    public wrapperAccountOpportunity(){
        normalList = true;
        selectedList = false;
        fetchData();
    }
    public boolean normalList{get;set;}
    public boolean selectedList{get;set;}
    public void fetchData(){
       List<Opportunity> allOpps = [Select name,Id,AccountId from Opportunity ];
        //Parent Id set
        Set<id> parentIdSet = new Set<id>();
        //Create parent Id set 
        for(Opportunity OppertunityRec :allOpps){
            parentIdSet.add(OppertunityRec.AccountId);
        }
        
        //Fetch all associated parents
        List<Account> allAssocaiatedAccounts = [Select name,id 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> selectedWrapperList {get; set;}
    public PageReference ProceedWithSelected(){
        selectedWrapperList = new List<myWrapperClass>();
        normalList = false;
        selectedList = true;
        for(myWrapperClass selectedWrapObj: wrapperList){
            system.debug('selectedWrapObj.selected  ---------'+selectedWrapObj.selected);
            if(selectedWrapObj.selected == true)
            selectedWrapperList.add(selectedWrapObj);
        }
        system.debug('selectedWrapperList size ---------'+selectedWrapperList.size());
        return null;
    }
     public PageReference ProceedWithSelectedToNextPage(){
        selectedWrapperList = new List<myWrapperClass>();
        normalList = false;
        selectedList = true;
        for(myWrapperClass selectedWrapObj: wrapperList){
            system.debug('selectedWrapObj.selected  ---------'+selectedWrapObj.selected);
            if(selectedWrapObj.selected == true)
            selectedWrapperList.add(selectedWrapObj);
        }
        system.debug('selectedWrapperList size ---------'+selectedWrapperList.size());
        PageReference pageRef = new PageReference('/apex/AccountOpportunityTwoPage');
        pageRef.setRedirect(false);
        return pageRef;
    }
 
//Wrapper list 
public List<myWrapperClass> wrapperList {get; set;}
//Your wrapper 
public class myWrapperClass{
    public Account acc{get;set;}
    public Opportunity opp{get;set;}
    public Boolean selected {get; set;} 
    public myWrapperClass() { 
         selected = false; 
      } 
}
}
Let me know in case of any issue.

All Answers

Hanumantha Rao 6Hanumantha Rao 6
Hi Raza ,

I tried those things , but how i can display one one account record related opportunities in VF page.
based on the above conditions will dispaly those records, any more few suggestion ?
it would be helpfull for me .
Mudasir WaniMudasir Wani
Hi Hanumantha,

I feel you are not using the correct URL then 
your salesforce page URL should have Id appended at the end 
say you have url as 
Https://yoursalesforceURl/apex/pageName
Then you need to hit the URL as 
 
Https://yoursalesforceURl/apex/pageName?Id=yourAccountId

Say there is an account id '01123232323' with some opportunities then your URL should be like

Https://yoursalesforceURl/apex/pageName?Id=01123232323

Hope this helps
 
Krishna SambarajuKrishna Sambaraju
Hi Hanumantha,

Are you trying to display the list of accounts and the opportunities related to each of those accounts on the same visualforce page, and you want to display only the related opportunities of the selected account from the account list.

In that case you can create a visualforce component that takes the Account ID as the parameter and renders the related opportunities.

Regards,
Krishna.
Mudasir WaniMudasir Wani
If you want to display all the accounts and opportunites then I have a code for you.
In the below code I am displaying all accounts and opportunities and a checkbox we also have a functionality where I am displaying only the selected records.
Have a look on the code and modify as per your needs.

#PAGE
<apex:page controller="wrapperAccountOpportunity" sidebar="false">
    <apex:form >
    <apex:commandButton value="Proceed with Selected" action="{!ProceedWithSelected}" reRender="panelId"/>
    <apex:commandButton value="Proceed with Selected to Next Page" action="{!ProceedWithSelectedToNextPage}" />
    <apex:pageBlock >
        <apex:outputPanel id="panelId">
            <apex:pageblockTable value="{!wrapperList}" var="wrapRec" rendered="{!normalList}">
               <apex:column value="{!wrapRec.acc.Name}" />
               <apex:column value="{!wrapRec.opp.Name}"/>
               <apex:column >
                   <apex:inputCheckbox value="{!wrapRec.selected}"/>
               </apex:column>
          </apex:pageblockTable>
          
           <apex:pageblockTable value="{!selectedWrapperList}" var="wrapRec" rendered="{!selectedList}">
               <apex:column value="{!wrapRec.acc.Name}" />
               <apex:column value="{!wrapRec.opp.Name}"/>
               <apex:column >
                   <apex:inputCheckbox value="{!wrapRec.selected}"/>
               </apex:column>
          </apex:pageblockTable>
        </apex:outputPanel>      
    </apex:pageBlock>
    </apex:form>
</apex:page>

#CLASS
public class wrapperAccountOpportunity {
    
    public wrapperAccountOpportunity(){
        normalList = true;
        selectedList = false;
        fetchData();
    }
    public boolean normalList{get;set;}
    public boolean selectedList{get;set;}
    public void fetchData(){
       List<Opportunity> allOpps = [Select name,Id,AccountId from Opportunity ];
        //Parent Id set
        Set<id> parentIdSet = new Set<id>();
        //Create parent Id set 
        for(Opportunity OppertunityRec :allOpps){
            parentIdSet.add(OppertunityRec.AccountId);
        }
        
        //Fetch all associated parents
        List<Account> allAssocaiatedAccounts = [Select name,id 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> selectedWrapperList {get; set;}
    public PageReference ProceedWithSelected(){
        selectedWrapperList = new List<myWrapperClass>();
        normalList = false;
        selectedList = true;
        for(myWrapperClass selectedWrapObj: wrapperList){
            system.debug('selectedWrapObj.selected  ---------'+selectedWrapObj.selected);
            if(selectedWrapObj.selected == true)
            selectedWrapperList.add(selectedWrapObj);
        }
        system.debug('selectedWrapperList size ---------'+selectedWrapperList.size());
        return null;
    }
     public PageReference ProceedWithSelectedToNextPage(){
        selectedWrapperList = new List<myWrapperClass>();
        normalList = false;
        selectedList = true;
        for(myWrapperClass selectedWrapObj: wrapperList){
            system.debug('selectedWrapObj.selected  ---------'+selectedWrapObj.selected);
            if(selectedWrapObj.selected == true)
            selectedWrapperList.add(selectedWrapObj);
        }
        system.debug('selectedWrapperList size ---------'+selectedWrapperList.size());
        PageReference pageRef = new PageReference('/apex/AccountOpportunityTwoPage');
        pageRef.setRedirect(false);
        return pageRef;
    }
 
//Wrapper list 
public List<myWrapperClass> wrapperList {get; set;}
//Your wrapper 
public class myWrapperClass{
    public Account acc{get;set;}
    public Opportunity opp{get;set;}
    public Boolean selected {get; set;} 
    public myWrapperClass() { 
         selected = false; 
      } 
}
}
Let me know in case of any issue.
This was selected as the best answer