+ Start a Discussion
TheRealistTheRealist 

Is it possible to display and delete selected Records with a checkbox beside every record By using wrapper class?

A checkbox besides every record when checked, selected reords should be displayed and with the help of a custom button those selected records should be deleted,Wondering if it is possible with the help of a wrapper class. 
William TranWilliam Tran
Yes, it is possible to create a class to mimic the object with the additional checkbox ( not sure if you consider this a wrapper class).  That said, if you can spare a field in the object model, you can just add a checkbox field in the object called "Selected" and not have to worry about wrapper classes and manipulating data back and forth between the original object and the  wrapper class when updating/deleting the original object.
Amit Chaudhary 8Amit Chaudhary 8
Hi TheRealist,

Please check below post. I hope that will help you
https://developer.salesforce.com/page/Wrapper_Class
http://blogforce9.blogspot.in/2013/06/wrapper-classes-wrap-it-up-with-wrapper.html


please create below controller class Name should be "WrapperDemo_Con"
public class WrapperDemo_Con {  
   /*List of wrappers*/  
   public List<sObjectWrapper> wrappers{get;set;}  
   /*Constructor*/  
   public WrapperDemo_Con(){  
     wrappers = getData();  
   }  
   /*method to delete the selected record*/   
   public void deleteRecords(){  
     List<Account> accToDel = new List<Account>();  
     for(sObjectWrapper wrap : wrappers){  
       /*Check if record is selected*/  
       if(wrap.isSelected){  
         accToDel.add(wrap.myAccount);  
       }  
     }  
     /*Delete the selected records*/  
     delete accToDel;  
     /*Referesh the data*/  
     wrappers = getData();  
   }  
   /*mehtod to query account and populate wrappers*/  
   private List<sObjectWrapper> getData(){  
     List<sObjectWrapper> wrappers = new List<sObjectWrapper>();  
     for(Account acc : [SELECT Name,Id,Phone, Description FROM Account]){  
       /*Create a new wrapper and add it to list*/  
       wrappers.add(new sObjectWrapper(acc,false));  
     }  
     return wrappers;  
   }  
   /*Wrapper class*/  
   public class sObjectWrapper{  
    public boolean isSelected{get;set;}  
    public Account myAccount{get;set;}  
    public sObjectWrapper(Account myAccount,Boolean isSelected){  
     this.myAccount = myAccount;  
     this.isSelected = isSelected;  
    }  
   }  
 }

Then please create below page
<apex:page controller="WrapperDemo_Con">  
   <apex:sectionHeader title="Demo" subtitle="Wrapper Class"/>  
   <apex:Form >  
     <apex:pageBlock title="Wrapper Demo">  
       <apex:pageBlockButtons >  
         <apex:commandButton value="Delete" action="{!deleteRecords}"/>  
       </apex:pageBlockButtons>  
       <apex:pageBlockTable value="{!wrappers}" var="wrap">  
         <apex:column headerValue="Select">            
           <apex:inputCheckbox value="{!wrap.isSelected}"/>  
         </apex:column>  
         <apex:column value="{!wrap.myAccount.name}"/>  
         <apex:column value="{!wrap.myAccount.phone}"/>  
       </apex:pageBlockTable>  
     </apex:pageBlock>  
   </apex:Form>  
  </apex:page>


Please let us know if this will help u

Thanks
Amit Chaudhary