Custom Button Example: Mass Delete
This example creates a JavaScript custom button for Salesforce Classic that can be added to activity-related lists and list views and allows users to delete selected records at the same time.
Required Editions
| Available in: Salesforce Classic |
Custom buttons and links are available in: All Editions Visualforce pages and s-controls are available in: Contact Manager, Group, Professional, Enterprise, Performance, Unlimited, and Developer Editions |
| User Permissions Needed | |
|---|---|
| To create or change custom buttons or links: | Customize Application |
JavaScript custom buttons are supported in all editions, in Salesforce Classic only. The mass delete function described here doesn’t work in editions where the API isn’t enabled.
-
Define a button for cases with these attributes.
Display Type List Button Select Display Checkboxes (for Multi-Record Selection) so users can select multiple records in the list before clicking the button.
Behavior Execute JavaScript Content Source OnClick JavaScript Use this sample code:{!REQUIRESCRIPT("/soap/ajax/9.0/connection.js")} var records = {!GETRECORDIDS( $ObjectType.Event )}; var taskRecords = {!GETRECORDIDS( $ObjectType.Task)}; records = records.concat(taskRecords); if (records[0] == null) { alert("Please select at least one record.") } else { 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(); } - Add the button to your activity list views.
- Add the button to any page layout that contains an activity-related list. The button deletes any selected task or event in the list.
You can install custom buttons from the Mass Delete app at https://appexchange.salesforce.com/.

