Create Simple Mass Delete Button for ListView using Salesforce Ajax Toolkit

In Many situation, I needed a Mass Delete Kind of button. Where, i can select records in ListView and simply click “Delete” button.

In this example, we will create a simple List View button and add on “Search Layout” of that object.

Salesforce Mass Delete Button - List View
Salesforce Mass Delete Button – List View

So, create a new Custom Button with following property:

  1. Display Type :  List Button
  2. Behavior : Execute JavaScript
  3. Content Source :  OnClick JavaScript

and use below Source code:

{!REQUIRESCRIPT("/soap/ajax/21.0/connection.js")}

//Below ObjectType can be replaced by anyObject. I have used "Test Name"
var records = {!GETRECORDIDS($ObjectType.Test_Name__c)};

if (records[0] == null) {
    alert("Please select at least one record.")
} else {

    var opt = confirm("Are you sure you want to delete selected records ?");
    if (opt == true) {
        var errors = [];
        var result = sforce.connection.deleteIds(records);
        if (result && result.length) {
            var numFailed = 0;
            var numSucceeded = 0;
            for (var i = 0; i < result.length; i++) {                 var res = result[i];                 if (res && res.success == 'true') {                     numSucceeded++;                 } else {                     var es = res.getArray("errors");                     if (es.length > 0) {
                        errors.push(es[0].message);
                    }
                    numFailed++;
                }
            }
            if (numFailed > 0) {
                alert("Failed: " + numFailed + "nSucceeded: " + numSucceeded + " n Due to: " + errors.join("n"));
            } else {
                alert("Number of records deleted: " + numSucceeded);
            }
        }
        window.location.reload();
    }
}

In above code, we only need to change line number 4 for Object Type.
Once you are done with above button, add button in Search Layout’s List View as shown in below Image.

Salesforce Search Layout
Salesforce Search Layout

Code taken from Salesforce Cookbook

Posted

in

by


Related Posts

Comments

6 responses to “Create Simple Mass Delete Button for ListView using Salesforce Ajax Toolkit”

  1. Sudheera Avatar
    Sudheera

    Hi Jitendra,
    This method will not work for the records more than 200. Because it will violate the governor limit of for loop. Do you have a solution of Mass Deletion for more than 200 records?

    1. Jitendra Zaa Avatar

      Did you tried ? DML is performed on line 13 and its not inside loop.

      1. Sujay Surendranath Avatar
        Sujay Surendranath

        I tried to delete more than 200 rows from the list View
        1. Salesforce will not allow us to select more than 200 rows
        2. Script runs fine to delete all the 200 records.

        Thanks for the Script, that solved my problem.

  2. Hemalatha paruchuri Avatar
    Hemalatha paruchuri

    {!REQUIRESCRIPT(“/soap/ajax/37.0/connection.js”)}
    {!REQUIRESCRIPT(“/soap/ajax/37.0/apex.js”)}

    var accnt = new sforce.SObject(“Account”);
    var recType=sforce.connection.query(“SELECT name, id FROM RecordType” );
    //alert(recType);
    var records = recType.getArray(“records”);
    //alert(records);
    accnt.Name='{!My_Leads__c.Company__c}’;
    if(accnt.Name !=””)
    {
    accnt.RecordTypeId = records[0].Id;
    //alert(records[0].Id);

    accnt.Id = ‘{!Account.Id}’;
    accnt.Name = prompt(”,'{!My_Leads__c.Company__c}’);
    accnt.OwnerId='{!My_Leads__c.OwnerId}’;

    var result = sforce.connection.create([accnt]);
    if(result[0].getBoolean(“success”))
    {
    alert(‘Account created successfully’);

    }
    var cnt= new sforce.SObject(“Contact”);
    cnt.Id = ‘{!Contact.Id}’;
    cnt.OwnerId='{!My_Leads__c.OwnerId}’;
    cnt.AccountId=result[0].id;
    cnt.lastName = prompt(”,'{!My_Leads__c.Lead_Name__c}’);

    var result = sforce.connection.create([cnt]);
    if(result[0].getBoolean(“success”))
    {
    alert(‘contact created successfully’);
    window.location.reload();

    }

    else{
    alert(‘Error : ‘+result);
    }
    }
    else
    {
    accnt.RecordTypeId=records[1].Id;
    //alert(records[1].Id);
    accnt.Id = ‘{!Account.Id}’;
    accnt.LastName = prompt(”,'{!My_Leads__c.Lead_Name__c}’);
    accnt.OwnerId='{!My_Leads__c.OwnerId}’;

    var result = sforce.connection.create([accnt]);
    if(result[0].getBoolean(“success”))
    {
    alert(‘Account created successfully’);

    }
    else
    {
    alert(‘Error : ‘+result);
    }

    }

    After convert my lead object record.i need to delete this record automatically

  3. Tom Avatar
    Tom

    Any one know how to achieve the same function in visualforce – so it’s lightning compatible?

  4. Ajay Avatar
    Ajay

    Please help on how to implement mass delete in lightning.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Discover more from Jitendra Zaa

Subscribe now to keep reading and get access to the full archive.

Continue Reading