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
Sumanjit BiswalSumanjit Biswal 

how to add serial no in wrapper class using for loop?

Best Answer chosen by Sumanjit Biswal
v varaprasadv varaprasad
Hi Sumanjit,

Please check sample code below : 
 
<apex:page controller="WrapperClassExample" sidebar="false">
    <apex:form > 
        <apex:pageBlock title="Wrapperclass example" id="refer">
            
            <apex:outputPanel id="pgrefresh">
                <apex:pageBlockTable value="{!allRecords}" var="allrcs" id="Refresh">
                    <apex:column headerValue="S.No" value="{!allrcs.RowNo}"/>
                    <apex:column headerValue="select">
                        <apex:inputCheckbox value="{!allrcs.chk}"/>
                    </apex:column>
                    <apex:column headerValue="Account">
                        <apex:inputText value="{!allrcs.acc.name}"/>
                    </apex:column>
                    <apex:column headerValue="Contact lastname" >
                        <apex:inputText value="{!allrcs.con.lastname}"/>
                    </apex:column>
                    <apex:column headerValue="Contact firstname" >
                        <apex:inputText value="{!allrcs.con.firstname}"/>
                    </apex:column>
                </apex:pageBlockTable>
            </apex:outputPanel>
            <apex:commandButton value="Update Records" action="{!updateRecords}" rerender="pgrefresh"/>
        </apex:pageBlock>        
    </apex:form>
</apex:page>



==================
public class WrapperClassExample {
    public list<innerwrapper> allRecords{get;set;}
    public integer RowNo{set;get;}
    public WrapperClassExample(){
        //RowNo = 1;
        allRecords = new list<innerwrapper>();
        list<contact> lstContacts = [select id,firstname,lastname,account.name from contact where account.name != null limit 10];
        for(integer i=0;i<lstContacts.size();i++){
            RowNo = i+1;
            innerwrapper iw = new innerwrapper(lstContacts[i].account,lstContacts[i],false,RowNo);
            allRecords.add(iw);
        }
        
    }
    public PageReference updateRecords(){ 
        boolean updflag = false;
         PageReference curPage;
        if(allRecords.size()>0){
            for(integer i=0;i<allRecords.size();i++){
                if(allRecords[i].chk == True){
                    system.debug('==allRecords[i]=='+allRecords[i]);
                    update allRecords[i].acc;
                    system.debug('==allRecords[i].acc=='+allRecords[i].acc);
                    update allRecords[i].con;
                     system.debug('==allRecords[i].con=='+allRecords[i].con);
                    updflag = true;
                }
            } 
        }
        if(updflag == true){
            curPage = page.WrapperClasExample; //new PageReference('/apex/Wrapperclasexample');
            curPage.setRedirect(true);            
        }
        return curPage;  
        
    }
    
    public class innerwrapper{
        public account acc{get;set;}
        public contact con{get;set;}
        public boolean chk{get;set;}
        public integer rowNo{get;set;}
        
        public innerwrapper(account a,contact c,boolean b,integer row){
            acc = a;
            con = c;
            chk = b;
            rowNo = row;
        }
        
    }
    
}

Hope this helps you.

If the above information helps you Please mark it as the best answer.  


Thanks
Varaprasad




 

All Answers

v varaprasadv varaprasad
Hi Sumanjit,

Please check sample code below : 
 
<apex:page controller="WrapperClassExample" sidebar="false">
    <apex:form > 
        <apex:pageBlock title="Wrapperclass example" id="refer">
            
            <apex:outputPanel id="pgrefresh">
                <apex:pageBlockTable value="{!allRecords}" var="allrcs" id="Refresh">
                    <apex:column headerValue="S.No" value="{!allrcs.RowNo}"/>
                    <apex:column headerValue="select">
                        <apex:inputCheckbox value="{!allrcs.chk}"/>
                    </apex:column>
                    <apex:column headerValue="Account">
                        <apex:inputText value="{!allrcs.acc.name}"/>
                    </apex:column>
                    <apex:column headerValue="Contact lastname" >
                        <apex:inputText value="{!allrcs.con.lastname}"/>
                    </apex:column>
                    <apex:column headerValue="Contact firstname" >
                        <apex:inputText value="{!allrcs.con.firstname}"/>
                    </apex:column>
                </apex:pageBlockTable>
            </apex:outputPanel>
            <apex:commandButton value="Update Records" action="{!updateRecords}" rerender="pgrefresh"/>
        </apex:pageBlock>        
    </apex:form>
</apex:page>



==================
public class WrapperClassExample {
    public list<innerwrapper> allRecords{get;set;}
    public integer RowNo{set;get;}
    public WrapperClassExample(){
        //RowNo = 1;
        allRecords = new list<innerwrapper>();
        list<contact> lstContacts = [select id,firstname,lastname,account.name from contact where account.name != null limit 10];
        for(integer i=0;i<lstContacts.size();i++){
            RowNo = i+1;
            innerwrapper iw = new innerwrapper(lstContacts[i].account,lstContacts[i],false,RowNo);
            allRecords.add(iw);
        }
        
    }
    public PageReference updateRecords(){ 
        boolean updflag = false;
         PageReference curPage;
        if(allRecords.size()>0){
            for(integer i=0;i<allRecords.size();i++){
                if(allRecords[i].chk == True){
                    system.debug('==allRecords[i]=='+allRecords[i]);
                    update allRecords[i].acc;
                    system.debug('==allRecords[i].acc=='+allRecords[i].acc);
                    update allRecords[i].con;
                     system.debug('==allRecords[i].con=='+allRecords[i].con);
                    updflag = true;
                }
            } 
        }
        if(updflag == true){
            curPage = page.WrapperClasExample; //new PageReference('/apex/Wrapperclasexample');
            curPage.setRedirect(true);            
        }
        return curPage;  
        
    }
    
    public class innerwrapper{
        public account acc{get;set;}
        public contact con{get;set;}
        public boolean chk{get;set;}
        public integer rowNo{get;set;}
        
        public innerwrapper(account a,contact c,boolean b,integer row){
            acc = a;
            con = c;
            chk = b;
            rowNo = row;
        }
        
    }
    
}

Hope this helps you.

If the above information helps you Please mark it as the best answer.  


Thanks
Varaprasad




 
This was selected as the best answer
Sumanjit BiswalSumanjit Biswal
this is my program and ia m geeting this error






<apex:page controller="wrapperclassv1">
  <apex:form >
      <apex:pageBlock >
      <apex:commandButton value="show" action="{!displayselected}" reRender="s"/>
     
          <apex:pageBlockSection >
          
          <apex:pageBlockTable value="{!wlist}" var="cc" >
                <apex:column value="{!cc.sr}" headerValue="serial no"/>
                 <apex:column value="{!cc.cw.name}"/>
                
                 <apex:column headerValue="select">
                 <apex:inputCheckbox value="{!cc.selected}" />
                 </apex:column>     
                 
                     
          </apex:pageBlockTable>
      </apex:pageBlockSection>
     
      <apex:pageBlockSection title="output">
      
          <apex:pageBlockTable value="{!wlistshow}" var="cc" id="s">
                  <apex:column value="{!cc.sr}" headerValue="serial number"/> 
                 <apex:column value="{!cc.cw.name}"/>
                           
                 <apex:column headerValue="select">
                     <apex:inputCheckbox value="{!cc.selected}" />
                 </apex:column>                
               
          
          </apex:pageBlockTable>
    
      </apex:pageBlockSection>
      </apex:pageBlock>
  
  </apex:form>
</apex:page>
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

public class wrapperclassv1 {

        public list<account> cl{set;get;}
        
        public list<account> clshow{set;get;}
        
        public boolean check{set;get;}
        
         public integer sr{set;get;}
        
        public list<wrapinner> wlist{set;get;} 
        
        
        public list<wrapinner> wlistshow{set;get;} 
        

public wrapperclassv1(){

                clshow = new list<account>();
                
                 cl = [select id ,name from account limit 10 ];
                   
                for(integer i=0;i<cl.size();i++){
                sr=i+1;
                
               wlist = new list<wrapinner>();
            
            for(account a: cl){     
                  
                 wlist.add(new wrapinner(a,false,sr));
                     
 }
}
}

public void displayselected(){

     wlistshow = new list<wrapinner>();
    
        for(wrapinner wi : wlist){
        
            if(wi.selected == true){
            
                wlistshow.add(wi);  }
                          }
      
}

public class wrapinner{

public account cw{set;get;}
public boolean selected{set;get;}
public integer sr{set;get;}

public wrapinner(account cw,boolean selected,integer sr){

    this.cw = cw;
    this.selected= selected;
    this.sr=sr;
      }


}


}


User-added image