Wrapper Class in Salesforce with example

Displaying the selected records in Next page

Continued from ... Basics of a wrapper







If you have read the previous example on wrapper class, this is the next step you need to look into.

Displaying of Boolean and list Datatypes



This scenario mainly  describes the usage of Wrapper class.Wrapper class is used to display different types of datatypes in a single table. In the following scenario we are displaying boolean and list datatypes.


When no records are available in the Testing object.











  • Data is fed to our Testing Object
  • Selecting our specific records
  •  Passing the selecting data to the next controller and displaying the passed data on our VF page








  WrapTestClass :

public with sharing class WrapTestClass {

    List<string> lstselectedNames = new List<string>();
    public PageReference selRecDisplay() {
        for(wrapper w: lstwrap){
            if(w.ischecked==true){
                lstselectedNames.add(w.Tname);
            }
        }        
        List<Testing__c> lstselectedrecords = [select Id,name,city__c from 
           Testing__c where name in : lstselectedNames];
        List<String> lstselectedRecordIds = new List<String>();
        for(Integer  i=0;i<lstselectedrecords.size();i++){
            lstselectedRecordIds.add(lstselectedrecords[i].Id);
        }
        string s='';
        for(Integer i=0;i<lstselectedRecordIds.size();i++)
        {
            if(i<lstselectedRecordIds.size()-1)
               s=s+lstselectedRecordIds[i]+':';
            else
               s=s+lstselectedRecordIds[i];
        }
        pagereference ref  = new pagereference('/apex/t123?value='+s);
        ref.setredirect(true);
        return ref;
    }

    List<wrapper> lstwrap = new List<wrapper>();
    List<Testing__c> lsttest = new List<Testing__c>();
    public List<wrapper> getTestingrecords() {
        lsttest = [select Id,name,city__c from Testing__c];
        for(Integer i=0;i<lsttest.size();i++)
        {
            lstwrap.add(new wrapper(lsttest[i].name,lsttest[i].city__c));
        }
        return lstwrap;
    } 
    
    public class wrapper{
        public String Tname{get;set;}
        public String Tcity{get;set;}
        public boolean ischecked{get;set;}
        public wrapper(String Tname,String Tcity)
        {
            this.Tname=Tname;
            this.Tcity=Tcity;
            this.ischecked=false;
        }
    }
}




Visualforce page 1 :

<apex:page sidebar="false" controller="WrapTestClass">
<apex:form >
                  <apex:pageblock >
                  <apex:pageblocksection >
                      <apex:pageblocktable value="{!testingrecords}" 
                         var="tr">
                          <apex:column headervalue="Action">
                              <apex:inputcheckbox value="{!tr.ischecked}"/>
                          </apex:column>
                          <apex:column headerValue="Name">
                              {!tr.TName}
                          </apex:column>
                          <apex:column headerValue="City">
                              {!tr.TCity}
                          </apex:column>
                      </apex:pageblocktable>
                  </apex:pageblocksection>
              </apex:pageblock>
              <apex:commandButton value="SELECT" 
                   action="{!selRecDisplay}"/>
</apex:form>
</apex:page>


Class DClass :

public with sharing class DClass {
    List<Testing__c> lsttest = new List<Testing__c>();
    public List<Testing__c> getRecords() {
        List<String> ids = url.split(':');
        lsttest = [select Id,name,city__c from 
                  Testing__c where id in : ids];
        return lsttest;
    }

    String url = 
   apexpages.currentpage().getparameters().get('value');        
}
Visualforce Page 2

<apex:page controller="DClass">
  <apex:form >
      <apex:pageBlock >
          <apex:pageBlockSection >
              <apex:pageBlockTable value="{!records}" 
                var="r">
                  <apex:column headerValue="Name">
                      {!r.Name}
                  </apex:column>
                  <apex:column headerValue="City">
                      {!r.City__c}
                  </apex:column>
              </apex:pageBlockTable>
          </apex:pageBlockSection>
      </apex:pageBlock>
  </apex:form>
</apex:page>

You can see the transition in development of a wrapper class.
Happy Coding...!!!
Wrapper Class in Salesforce with example Wrapper Class in Salesforce with example Reviewed by dasfrogpractice on 10:42 Rating: 5

No comments:

Theme images by mariusFM77. Powered by Blogger.
Youtube Channel Image
Dasfrog Subscribe To watch more Salesforce Training
Subscribe