Sunday, March 15, 2015

Wrapper class usage in apex and visualforce pages.

Wrapper class usage in apex and visualforce pages.

A common use case for Visualforce is to present a list of sObjects, allowing a user to select a number of these, and then choose an action to apply to the selected entries. Marking an sObject entry as selected presents a challenge, as it is associating transient information, the selected status, with a record persisted in the Salesforce database.

The solution is to use a wrapper class to encapsulate or wrap an sObject instance and some additional information associated with the sObject instance.

In the below example i developed a visualforce page with delete functionality based on the selected account records. when ever user select's particular account records to delete then wrapper object holds the selected account with check box value as true, then based on the check box value we can able to delete the particular selected account records.

Controller Class: 
Here i am using custom controller to develop the wrapper functionality, Here AccountRecordCls  is wrapper class to have multiple things like  holding the selected account record with check-box value and individual account record.

public class AccountDeleteWrapperCLs{
public List<AccountRecordCls> accWrapperRecordList {get;set;}

 public AccountDeleteWrapperCLs(){
 List<Account> accList= new List<Account>();
 accWrapperRecordList = new List<AccountRecordCls>();
 accList = [select id,Name,Phone,AccountNumber from Account];
  if(!accList.isEmpty()) {
    for(Account acc: accList){
     AccountRecordCls arcls = new AccountRecordCls();
     arcls.isSelected =  false;
     arcls.accObj = acc;
    } //end of for loop.
  } //end of if condition.
   Delete Account functionality based on the selected records.
  public PageReference DeleteAccount(){
   List<Account> accToDelete = new List<Account>();
   //To hold the unselected account records.
   List<AccountRecordCls> listUnSelectedRecords  = new List<AccountRecordCls>();  
    if(accWrapperRecordList !=null && accWrapperRecordList.size()>0) {
      for(AccountRecordCls wrapObj :  accWrapperRecordList){
        if(wrapObj.isSelected == true){
      }//end of for.
       checking the delete list size and assign the unselected values to 
       original wrapper list.
      if(accToDelete !=null && accToDelete.size()>0){
       delete accToDelete;
     ApexPages.Message  myMsg = new ApexPages.Message(, 'Records were not there to delete.');
    return null;


 /* Wrapper class with checkbox and account object. 
  this is also  called as inner class 

 public class AccountRecordCls{
  public boolean isSelected {get;set;}
  public Account accObj {get;set;}



Visualforce Page: 

<apex:page controller="AccountDeleteWrapperCLs">

 <!-- This i am using for action status message display with processing records information -->

    /* This is for the full screen DIV */
    .popupBackground {
        /* Background color */
        opacity: 0.20;
        filter: alpha(opacity = 20);
        /* Dimensions */
        width: 100%;
        height: 100%;
        top: 0;
        left: 0;
        z-index: 998;
        position: absolute;
        /* Mouse */

    /* This is for the message DIV */
    .PopupPanel {
        /* Background color */
        border: solid 2px blue;
        background-color: white;

        /* Dimensions */
        left: 50%;
        width: 300px;
        margin-left: -100px;
        top: 50%;
        height: 50px;
        margin-top: -25px;
        z-index: 999;
        position: fixed;
        /* Mouse */
<apex:actionStatus id="deleteStatus" stopText="">
    <apex:facet name="start">
            <div class="popupBackground" />
            <div class="PopupPanel">
                <table border="0" width="100%" height="100%">
                        <td align="center"><b>Please Wait</b></td>
                        <td align="center"><img src="{!$Resource.AJAXProgressBar}"/></td>

 <!-- end of Action Status --> 
<apex:pagemessages id="Msg"> </apex:pagemessages>
<apex:pagemessage summary="Your are doing Mass Delete On Account Object" Severity="Info" Strength="2"></apex:pagemessage>

 <apex:form id="theForm">
  <apex:commandButton value="Delete Account" action="{!DeleteAccount}" reRender="thePb,Msg" status="deleteStatus"/>
  <apex:pageblock id="thePb">
   <apex:pageblockTable value="{!accWrapperRecordList}" var="record">
   <apex:column headerValue="Select">
     <apex:inputCheckbox value="{!record.isSelected}"/>
    <apex:column value="{!record.accObj.Name}"/>
     <apex:column value="{!record.accObj.Phone}"/>
      <apex:column value="{!record.accObj.AccountNumber}"/>


Example of using Wrapper class:



That's it
Hope you enjoy........


Jodi Southwick said...

Do you have a test class for this wrapper? I was able to implement it in my Sandbox, but I don't know how to write the test class.

floretta threadgill said...

Practical writing , I was fascinated by the info , Does anyone know if I can locate a sample a form document to type on ?

Savani S said...

Hello Srini,

I am getting error for the below line.


Could you please add the respective code also ?

Farukh Blog On Salesforce said...

Nice post,Found good one here as well

Wrapper class in salesforce

Post a Comment

| ,