Wrapper Class in Salesforce - with example

What is a Wrapper class and why is it a developers best friend ?

It is an inner class or container in Salesforce. We define the properties of the wrapper class. We would be learning how to display a list of records on a visualforce page and performing DML operation on few of the selected records. Sounds fun ? But watch out closely for how data which is passed through our wrapper class.

We design the properties of a wrapper class. Say for example,  we have an empty carton box. And we would need to fill this box with Chicken wraps.
Steps to put wraps inside a box :
  • Fetch a carton box. Make sure its an empty one.
  • Prepare the wrap dough.
  • Cut the leaves,tomatoes and fry the chicken.
  • Wrap all of the above in a bubbled hot dough.
  • Place the crispy hot wrap in the carton box. (Repeat the above steps for making more wraps)



 Take the carton box as an OuterClass and the 5 wraps present inside the box are safely held by InnerClass 


Syntax :

Public Class OuterClass
{
    
    // Outer Class Code
    
    public class InnerClass
    {
         // Inner Class Code
     }
}


What we would be achieving by the end of this post ?

  • Creating a custom object
  • Fields in custom object
  • Records of a custom object
  • Apex Class containing a wrapper class
  • Visualforce displaying the records and a check box to select records to be deleted
Seems to look easy? Yes ,of course it is ! Lets take a dive.

Scenario :

Displaying records on the visualforce page, having a checkbox to select particular records and performing a DML on those selected records. 




DML - Data Manipulation Language

Custom Object - Student
Custom Fields - Name,Branch

Apex Class - 


public class showSpecificRecords
{
    public list<Student__c> studentRecds{get;set;}
    public ID currentPageID;
    
    public list<container> allrcsinContainer {get;set;}
    
    public showSpecificRecords(ApexPages.StandardController controller) {
    
        allrcsinContainer = new list<container>();
       
        //Querying the Student records
        studentRecds = [select id,name,branch__c from student__c];
        
        //Iterating through all the records of the student object
        for(integer i =0;i<studentRecds.size();i++)
        {
             //Creating a new Object of InnerClass
             container conObj = new container(false,studentRecds[i]);
             allrcsinContainer.add(conObj);
        }
        
    }

   //Method to delete the selected records
    public pagereference deletecheckRecs()
    {
        //Iterating through the list
        for(integer i=0;i<allrcsinContainer.size();i++)
        {
            //Fetching data of the selected records
            if(allrcsinContainer[i].checkbox == true)
            {
                //deleting student records based on lists index
                delete allrcsinContainer[i].stuObj;
            }
        }
        //Redirecting back to the same page after deleting
        pagereference ref = new pagereference('/apex/WrapperPage');
        ref.setredirect(true);
        return ref;
    }
    //Inner Class
    public class container
    {
        public boolean checkbox{get;set;}
        public student__c stuObj{get;set;}
        //passing arguments of boolean and object type
        public container(boolean fromMainclasscheckbox,student__c 
                        fromMainclassstuObj)
        {
            checkbox = fromMainclasscheckbox;
            stuObj   = fromMainclassstuObj;
        }
    }
}

Visualforce Page -

<apex:page standardController="Student__c" extensions="showSpecificRecords">
  <apex:form >
      <apex:pageBlock >
        <apex:pageBlockTable value="{!allrcsinContainer}" var="sturecs">
             <apex:column headerValue="select">
               <apex:inputCheckbox value="{!sturecs.checkbox}"/>
            </apex:column>
            <apex:column headerValue="id">
              <apex:outputField value="{!sturecs.stuObj.id}"/>
            </apex:column>
            <apex:column headerValue="Name of the Student">
               <apex:outputField value="{!sturecs.stuObj.name}"/>
            </apex:column>
             <apex:column headerValue="Branch">
               <apex:outputField value="{!sturecs.stuObj.Branch__c}"/>
            </apex:column>
        </apex:pageBlockTable>
        <apex:pageBlockButtons >
        <apex:commandButton value="delete" action="{!deletecheckRecs}"/>
        </apex:pageBlockButtons>
      </apex:pageBlock>
  </apex:form>
</apex:page>
NEXT EXAMPLE - USING BOOLEAN AND LIST DATA TYPES IN WRAPPER


That's its folks! You can share this post with your friends if you have
gained knowledge or lost your fear to wrapper class.Happy coding!!!
Wrapper Class in Salesforce - with example Wrapper Class in Salesforce - with example Reviewed by dasfrogpractice on 07:48 Rating: 5

2 comments:

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