
Request is to create a custom button “Delete” and add it on a List View. (Scenario below is for the Account Object): Steps are:
- Go to Setup | Create | Objects
- Click on the name of the Object
- Scroll down to the Buttons, Links & Actions section.
- Click on New Button or Link
- Name it as Delete
- Select the Display Type: List Button
- Do check the option: Display Checkboxes (for Multi-Record Selection)
- Behaviour: Execute JavaScript
- Content Source: OnClick JavaScript
- Following code:
{!REQUIRESCRIPT('/soap/ajax/29.0/connection.js')} try { var selectedRecords = {!GETRECORDIDS( $ObjectType.account )}; if(selectedRecords.length<1) alert('Please Select at Least One Row !'); else { userConsent = confirm( selectedRecords.length + ' Record(s) will be Deleted. Continue ? ' ); if(userConsent == true) { delResult = sforce.connection.deleteIds(selectedRecords); if (delResult[0].getBoolean("success")) { alert('The Record(s) were Deleted Successfully.'); window.location.reload(); } else alert( 'The Record(s) Could Not be Deleted. Error Message: ' + delResult[0].errors.message ); } } } catch(e) { alert('The Action Could not be Completed. Error Message: ' + e); }
- Select Save
Now, we need to add it to the List View. Here you go:
- Go to Setup | Create | Objects
- Click on the Name of the Object
- Scroll down to Search Layouts section
- Click Edit next to List View Layout
- Move our new button from the Available Buttons to the Selected Buttons List
- Select Save
- Navigate to the Account tab and select a List View.
Note: As mentioned the above scenario refers to the Account Object. To add a delete button in the Opportunity object change the code from:
var selectedRecords = {!GETRECORDIDS( $ObjectType.account )}; to "var selectedRecords = {!GETRECORDIDS( $ObjectType.aopportunity )};"