We can delete record in two ways
1. By using custom button
2. Visualforce page
By using custom button:
Go to account setup->Account -> Buttons, Links, and Actions->New Button or link
Choose display type list view
Behavior type execute java script
Content Source onclick Java script
{!REQUIRESCRIPT("/soap/ajax/10.0/connection.js")}
var isdelete = {!GETRECORDIDS( $ObjectType.Account)};
var deleteRecord= 'Are you sure to delete ' +isdelete.length+ ' Records?';
if(isdelete.length&& (window.confirm(deleteRecord)))
{
sforce.connection.deleteIds(isdelete,function()
{navigateToUrl(window.location.href);
});
}
else if (isdelete.length == 0)
{
alert("Please select Account record to delete");
}
Then go to search layout add button to list view
3. Visualforce page
To fulfill this requirement you need a wrapper class, main class and visualforce page
Class
public class AccountDelete {
public list<Account> Acclist{get;set;}
public ID currentPageID;
public list<DeleteExample> accWrap {get;set;}
public AccountDelete(ApexPages.StandardController controller) {
accWrap = new list<DeleteExample>();
//Querying the Student records
Acclist = [select id,name,industry from account];
//Iterating through all the records of the student object
for(integer i =0;i<Acclist.size();i++){
//Creating a new Object of InnerClass
DeleteExample conObj = new DeleteExample(false,Acclist[i]);
accWrap.add(conObj);
}
}
//Method to delete the selected records
public pagereference deletecheckedRecs (){
//Iterating through the list
for(integer i=0;i<accWrap.size();i++){
//Fetching data of the selected records
if(accWrap[i].checkbox == true){
//deleting student records based on lists index
delete accWrap[i].accObj;
}
}
// create visualforce page with name deleteWrapperExample
pagereference ref = new pagereference('/apex/deleteWrapperExample');
ref.setredirect(true);
return ref;
}
//Inner Class
public class DeleteExample{
public boolean checkbox{get;set;}
public account accObj{get;set;}
public DeleteExample(boolean checkbox,account acc)
{
checkbox = checkbox;
accObj = acc;
}
}
}
Visualforce page
<apex:page standardController="Account" extensions="AccountDelete">
<apex:form >
<apex:pageBlock >
<apex:pageBlockTable value="{!accWrap}" var="acc">
<apex:column headerValue="select">
<apex:inputCheckbox value="{!acc.checkbox}"/>
</apex:column>
<apex:column headerValue="id">
<apex:outputField value="{!acc.accObj.id}"/>
</apex:column>
<apex:column headerValue="Name">
<apex:outputField value="{!acc.accObj.name}"/>
</apex:column>
<apex:column headerValue="Industry">
<apex:outputField value="{!acc.accObj.Industry}"/>
</apex:column>
</apex:pageBlockTable>
<apex:pageBlockButtons >
<apex:commandButton value="delete" action="{! deletecheckedRecs}"/>
</apex:pageBlockButtons>
</apex:pageBlock>
</apex:form>
</apex:page>