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
RaviKumarRaviKumar 

I want a Wrapper class to display Account Object's 100 records and Opportunity Object's 40 records. How to write the For loop?

Wrapper class is for displaying the 2 different objects records in One tableor Objects with Checkbox.Till here It's clear.
My Question is
For example: I am using 2 objects say Account, Opportunity
While displaying all the Account and Opportunity records, i got a doubt that If Account have 100 records and Opportunity have 40 records.
Then How to write the Loop for that requirement
?
Best Answer chosen by RaviKumar
Mudasir WaniMudasir Wani
Hi Ravi,

Use below code.

Page
 
<apex:page controller="wrapperAccountOpportunity" sidebar="false" action="{!fetchData}">
    <apex:form >
    <apex:pageBlock >
     <apex:pageblockTable value="{!wrapperList}" var="wrapRec">
       <apex:column value="{!wrapRec.acc.Name}" />
       <apex:column value="{!wrapRec.opp.Name}"/>
       </apex:pageblockTable>
    </apex:pageBlock>
    </apex:form>
</apex:page>

Controller 
public class wrapperAccountOpportunity {
    
    public wrapperAccountOpportunity(){
        //fetchData();
    }
    
    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){
        	for(Account parentRec :allAssocaiatedAccounts){
        		if(parentRec.Id == childRec.AccountId){
        			myWrapperClass wrapRec = new myWrapperClass();
        			wrapRec.acc = parentRec;
        			wrapRec.opp = childRec;
        			wrapperList.add(wrapRec);
        		}
        	}
        } 
    }
 
//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; 
      } 
}
}


Please mark this as solution if this solves your problem, So that if anyone has this issue this can help.
 

All Answers

sandeep sankhlasandeep sankhla
Hi Ravi,

could you please explain more what and how exactly you want to show these records on page ? how these should come as in structure ? You need to show in a same row or column or differnt sections for Account and Opportunity ?
Mudasir WaniMudasir Wani
Hi Ravi,

Here is a code you can use.
Also parents are always lessthan childs.
Say you have following records

Parent__c
Child__c

//Fetch all childs 
List<Child__c> allChilds = [Select name,Id,lookupName__c from Child__c ];
//Parent Id set
Set<id> parentIdSet = new Set<id>();
//Create parent Id set 
for(Child__c childs :allChilds){
	parentIdSet.add(childs.lookupName__c);
}

//Fetch all associated parents
List<Parent__c> allAssocaiatedParents = [Select name from Parent__c where Id IN : parentIdSet];

//For loop to set data
for(Child__c childRec : allChilds){
	for(Parent__c parentRec :allAssocaiatedParents){
		if(parentRec.Id == childRec.lookupName__c){
			myWrapperClass wrapRec = new myWrapperClass();
			wrapRec.parentsToFetch = parentRec;
			wrapRec.childsToFetch = childRec;
			wrapperList.add(wrapRec);
		}
	}
}
//Wrapper list 
public List<myWrapperClass> wrapperList {get; set;}
//Your wrapper 
public class myWrapperClass{
	public Parent__c parentsToFetch{get;set;}
	public Child__c childsToFetch{get;set;}
	public Boolean selected {get; set;} 
	public myWrapperClass() { 
         selected = false; 
      } 
}


Please mark this as solution if this solves your problem, So that if anyone has this issue this can help.
 
RaviKumarRaviKumar
Thanks for ur reply @Sandeeep and @Wani

My Requirement is to display All Account and All Opportunity Objects records in PageBlockTable. Here is my Code.

VF Page:
<apex:page controller="Pract2_2Objects">
    <apex:form >
    <apex:pageBlock >
     <apex:pageblockTable value="{!Wrap}" var="w">
       <apex:column value="{!w.aName}" />
       <apex:column value="{!w.oName}"/>
       </apex:pageblockTable>
    </apex:pageBlock>
    </apex:form>
</apex:page>

Controller:
public class Pract2_2Objects {

public List<Wrapper> wrap{set; get;}
public List<Account> acount{set; get;}
public List<Opportunity> oppor{set; get;}

public Pract2_2Objects(){
 acount = [select name from Account];
 oppor = [select name from opportunity];
 wrap = new List<Wrapper>();
 for(Integer i = 0 ; i< acount.size(); i++){
    wrap.add(new Wrapper(acount[i].name, oppor[i].name)) ;  
 }
}

 public Class Wrapper{
   public String aName{set; get;}
   public String oName{set; get;}
   public Wrapper(String s1, String s2){
     aName = s1;
     oName = s2;
   }
 }
}
Mudasir WaniMudasir Wani
Hi Ravi,

Use below code.

Page
 
<apex:page controller="wrapperAccountOpportunity" sidebar="false" action="{!fetchData}">
    <apex:form >
    <apex:pageBlock >
     <apex:pageblockTable value="{!wrapperList}" var="wrapRec">
       <apex:column value="{!wrapRec.acc.Name}" />
       <apex:column value="{!wrapRec.opp.Name}"/>
       </apex:pageblockTable>
    </apex:pageBlock>
    </apex:form>
</apex:page>

Controller 
public class wrapperAccountOpportunity {
    
    public wrapperAccountOpportunity(){
        //fetchData();
    }
    
    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){
        	for(Account parentRec :allAssocaiatedAccounts){
        		if(parentRec.Id == childRec.AccountId){
        			myWrapperClass wrapRec = new myWrapperClass();
        			wrapRec.acc = parentRec;
        			wrapRec.opp = childRec;
        			wrapperList.add(wrapRec);
        		}
        	}
        } 
    }
 
//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; 
      } 
}
}


Please mark this as solution if this solves your problem, So that if anyone has this issue this can help.
 
This was selected as the best answer
RaviKumarRaviKumar
Thank you for ur help.@Wani

It's working fine for my requirement. Above code displays the Opportunity which have the Account. If any Opportunity don't have Account it'll not display.
Am i Correct..?

=======>Doubt:So, is it possible to display all Opprtunity records even though they don't have the Account. If so......How?
Mudasir WaniMudasir Wani

Hi Ravi,

Speaking as per salesforce CRM the independent opportunity doesnot make any sense.
If you want to include them as well you may need to modify the controller code to be like.
//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.opp = childRec;
                    wrapperList.add(wrapRec);
            }
         
        }


Please mark this as solution if this solves your problem, So that if anyone has this issue this can help.
 
badrul Hassanbadrul Hassan
Hello Mudasir Wani,

Do you know  ?
How values automatic display on same page  when i selected checkbox values and then i click on Done button, the selected value display on ckEditor body which is parent page ? How is it possible ?

User-added image

Thanks.