Skip to main content

wrapper Class


Wrapper Class is a class whose instances are the collection of other class objects.
                                     "OR"
Wrapper Class can be defined as the collection of different kind of datatypes into single datatype i.e. class type.
                                      "OR"
Wrapping up different datatypes into single type.In other words, we can wrap a primitive  or collection type value into a wrapper class object.
...............................................................................


Sample Code:-
-------------------->>>


Class Student{                                                          
String name;
String Age;
public Student(String name, String Age){
    this.name = name;
    this.Age = Age;
}

List<StudentWrapper> liststdwrap = new List<StudentWrapper>();
....                                                                            
....
Class StudentWrapper{
    String classname;
    String yearSubjects;
    Student std;
public StudentWrapper(string cname,strring yrsub,student s){
    this.classname = cname;
    this.yearSubject = yrsub;
    this.std = s; 
}

new StudentWrapper('8B','maths,science,English' ,new Student('Deepak','24'));

Here we are wrapping "Student" class object in "StudentWrapper" class Object.

....................................................................................

QUE:-
Show Contact records on VF page with check boxes and show checked record on another VF page after button click. 
                                       "OR"
How can we share list of object from one vf page to another vf page.

Solution:-

****MyViewSelectedList1 VF Page****

<apex:page controller="wrapperClassController" > 
<apex:form > 
<apex:pageBlock > 
<apex:pageBlockButtons > 
<apex:commandButton value="Process Selected" action="{!processSelected}"/> 
</apex:pageBlockButtons> 
<!-- In our table we are displaying the cContact records --> 
<apex:pageBlockTable value="{!contacts}" var="c" id="table"> 
<apex:column > 
<!-- This is our selected Boolean property in our wrapper class --> 
<apex:inputCheckbox value="{!c.selected}"/> </apex:column> 
<!-- This is how we access the contact values within our cContact container/wrapper --> 
<apex:column value="{!c.con.Name}" /> <apex:column value="{!c.con.Email}" /> 
<apex:column value="{!c.con.Phone}" /> 
</apex:pageBlockTable> 
</apex:pageBlock> 
</apex:form> 

</apex:page>


*****WrapperClassController Controller*****

public class wrapperClassController {

    public String val { get; set; }
    
    //Our collection of the class/wrapper objects cContact

    public List<cContact> contactList {get; set;}
    public List<Contact> contactLists {get; set;}
    
    public void SetselectedContacts(List<Contact> c){
        contactLists =c;
    }
    
    public List<Contact> getcontactLists(){
        return contactLists;
    }
    
    List<Contact> selectedCon = new List<Contact>();
    //This method uses a simple SOQL query to return a List of Contacts
    
    public List<cContact> getContacts() {
        if(contactList == null) {
            contactList = new List<cContact>();
            for(Contact c: [select Id, Name, Email, Phone from Contact limit 10]) {
                // As each contact is processed we create a new cContact object and add it to the contactList
                contactList.add(new cContact(c));
            }
        }
        return contactList;
    }
    
    public PageReference processSelected() {
        //We create a new list of Contacts that we be populated only with Contacts if they are selected
        //We will cycle through our list of cContacts and will check to see if the selected property is set to true, if it is we add the Contact to the selectedContacts list
        
       for(cContact cCon: contactList) {
            if(cCon.selected == true) {
                selectedCon.add(cCon.con);
            }
        }
        
        this.SetselectedContacts(selectedCon);
        // Now we have our list of selected contacts and can perform any type of logic we want, sending emails, updating a field on the Contact, etc
        System.debug('These are the selected Contacts...');
        
        PageReference pr = new PageReference('/apex/MyViewSelectedList2');
        pr.setRedirect(false);
        return pr;
    }
    
    public string getval(){
        return 'contaact';
    }

    // This is our wrapper/container class. A container class is a class, a data structure, or an abstract data type whose instances are collections of other objects. In this example a wrapper class contains both the standard salesforce object Contact and a Boolean value
    public class cContact {
        public Contact con {get; set;}
        public Boolean selected {get; set;}
        //This is the contructor method. When we create a new cContact object we pass a Contact that is set to the con property. We also set the selected value to false
        public cContact(Contact c) {
            con = c;
            selected = false;
        }
    }

}


***MyViewSelectedList2 VF Page***

<apex:page controller="wrapperClassController" >
<apex:form > 
The contacts you selected are!! 
<apex:pageBlock > 
<apex:pageBlockTable value="{!contactLists}" var="c" id="table"> 
<apex:column value="{!c.Name}" /> 
<apex:column value="{!c.Email}" /> 
<apex:column value="{!c.Phone}" /> 
</apex:pageBlockTable> 
</apex:pageBlock> 
</apex:form>

</apex:page>
..............................................................................


Interview Que:-


1) How can we access wrapper class in Lightning and Visualforce Component as an attribute? 
                                    "OR"
Can we access wrapper class attribute via:-

<apex:attribute name="testwrapper" type="Student.StudentWrapper"> (in vf component)

<aura:attribute name="testwrapper" type="Student.StudentWrapper"> (in lightning component)

Sol:-

NO, we can't access wrapper class as an attribute via dot operator or as an inner class . we can access wrapper class either by defining wrapper as separate class or by defining the interface that is implemented by outer class.

like :- Interface ABC{

          }

         Class Student implements ABC {
              ....
              ....
              Class StudentWrapper{
                  ...
                  ...
               }
          }

then <aura:attribute name="testwrapper" type="ABC"> 





Comments

Post a Comment

If you need any information just let me know.