Tuesday, May 21, 2013

Delete Custom Button in List Views

Salesforce.com does not provide the functionality to delete records in batch / bulk size in List Views. Like in Salesforce.com we have standard / out-of-the box buttons "Change Owner", "Accept" and "Change Status". Unfortunately we do not have "Delete Lead" button in List Views but you can add it in List Views by just writing a small code snippet in JavaScript.

Sample Code:


//To show the alert pop up on the page
function log(message) {

//string for the URL of the current page
var url = parent.location.href;
//grabs the Lead records that the user is requesting to delete
var records = {!GETRECORDIDS($ObjectType.Lead)};
//if the button was clicked but there was no record selected
if (records[0] == null) {
//alert the user that they didn't make a selection
alert("Please select at least one record to update.");
} else {
//otherwise, there was a record selection
var delResult = sforce.connection.deleteIds(records);
if (delResult[0].getBoolean("success")) {
log(records.length+" record(s) have been deleted.");
log("Failed to delete records.");
//refresh the page
parent.location.href = url;