Visualforce page component for mass delete

A lot number of times we need mass delete functionality on various objects. 

We generally have requirement wherein we have button on related list object from where we need to delete the selected records.

How about creating a code which could be used for every such requirement in your org?

Keeping this requirement in mind i worked on a visualforce component :

You can directly copy this component as it is, you only need to pass the list of records and fields to be displayed in the pageblock table.

 --Component --
## Name = "MassDeleteComponent"

<apex:component controller="MassDeleteController" allowDML="true">
<script type="text/javascript">
     //check/uncheck the selected records in the selected list
    function checkAll(cb,cbid){
            var inputElem = document.getElementsByTagName("input");                    
            for(var i=0; i<inputElem.length; i++){            
                    inputElem[i].checked = cb.checked;

<apex:attribute name="ListRecs" description="list of records passed" type="sObject[]" assignTo="{!sObjList}"/>
<apex:attribute name="listofield" description="List of fields" type="string[]" required="false" assignTo="{!SobjFieldList}"/>
      <apex:pageBlock id="pgblck">
          <apex:pageBlockTable value="{!ListWrapperMethod}" var="wrapVar" >
              <apex:repeat value="{!FieldList}" var="fl">
                  <apex:column value="{!wrapVar.sObjRec[fl]}"/>
               <apex:column >
               <apex:facet name="header">
                      <apex:inputCheckbox id="selectAllChecks" value="{!wrapVar.SelectBox}" onclick="checkAll(this,'eachrow')"/>
               <apex:inputcheckbox value="{!wrapVar.SelectBox}" id="eachrow"/>
           <apex:commandButton value=" Delete Records " action="{!DeleteSelectedRecs}" reRender="pgblck"/>  

 -- Component controller --

public without sharing class MassDeleteController {
Public List<sObject> sObjList{get;set;}
Public List<String> SobjFieldList{get;set;}
Public List<wrapperClass> WrapperList{get;set;}

    Public List<wrapperClass> getListWrapperMethod(){
         WrapperList =new List<WrapperClass>();
             for(sObject sObj:sObjList){
                 WrapperList.add( new wrapperClass(false,sObj)) ;
             return WrapperList;
   Public List<String> FieldList{
         List<String> FieldList = New List<string>();
         FieldList = getSobjtFieldList();
         return FieldList;
   Public Class wrapperClass{
     Public Boolean SelectBox{get;set;}
     Public sObject sObjRec{get;set;}
     Public WrapperClass(Boolean checkbx, sObject sObjRecord){
        SelectBox = checkbx;
        sObjRec = sObjRecord;
    public List<string> getSobjtFieldList() {
       List<String> FieldList = SobjFieldList;
       return FieldList ;
    Public string DeleteSelectedRecs(){
      List<sObject> DeleteList = New List<sObject>();
      for(wrapperclass wc:WrapperList){
          if(wc.SelectBox  == true)
      delete DeleteList;
      return null;     

Lets use our component in an example below:
In this example i am passing fields as strings , we can pass this fields from field set as required(make sure you query the required fields appropriately).

 -- Visualforce page --

<apex:page standardcontroller="Account" extensions="MyController" tabStyle="Account">
       <c:MassDeleteComponent ListRecs="{!ListAcc}" listofield="{!fieldList}"/>

 -- Controller --

Public class MyController {
Public string MyString_From_Methode{get;set;}
Public List<sObject> accList{get;set;}
Public List<string> fieldstringList{get;set;}
    public MyController(ApexPages.StandardController controller) {

    Public List<String> getfieldList(){
        fieldstringList = new List<string>();
        return fieldstringList;
    Public List<sObject> getListAcc(){
      accList = new List<sObject>();
      accList = [select name,id,status__c from Opportunity  where status__c != null limit 5];
      return AccList;

1 comment:

  1. The blog gave me idea visual force page component My sincere thanks for sharing this post and please continue to share this post
    Saleesforce Training in Chennai