Saturday 28 May 2016

Visualforce page component example 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++){            
                 if(inputElem[i].id.indexOf(cbid)!=-1){                                       
                    inputElem[i].checked = cb.checked;
                 }
            }
    }
</script>

<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:form>
      <apex:pageBlock id="pgblck">
          <apex:pageBlockTable value="{!ListWrapperMethod}" var="wrapVar" >
              <apex:repeat value="{!FieldList}" var="fl">
                  <apex:column value="{!wrapVar.sObjRec[fl]}"/>
               </apex:repeat>
               <apex:column >
               <apex:facet name="header">
                      <apex:inputCheckbox id="selectAllChecks" value="{!wrapVar.SelectBox}" onclick="checkAll(this,'eachrow')"/>
               </apex:facet>
               <apex:inputcheckbox value="{!wrapVar.SelectBox}" id="eachrow"/>
               </apex:column>
             </apex:pageBlockTable>
           <apex:commandButton value=" Delete Records " action="{!DeleteSelectedRecs}" reRender="pgblck"/>  
        </apex:pageBlock>
  </apex:form>
</apex:component>

 -- 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{
      get{
         List<String> FieldList = New List<string>();
         FieldList = getSobjtFieldList();
         return FieldList;
      }set;
   }
   
   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)
             DeleteList.add(wc.sObjRec); 
      }
      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}"/>
</apex:page>

 -- 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>();
        fieldstringList.add('name');
        fieldstringList.add('Status__c');
        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;
    }  
}

No comments:

Post a Comment