How to add Delete button to delete related e2a emails along with the record on detail page?

Custom Delete button for deleting emails along with the record on Detail Page

There is often a need to delete the records when they are no longer required or have served their purpose. After deleting the records their related emails get orphaned (if they have any related e2a emails). When it is required to delete the record and all of its related emails (if any) as well, we can set up a custom button to serve the purpose. 


Steps to configure the Custom Button

Note: The object used in these steps is 'Lead'. You can configure it according to your requirement either for a Standard or a custom object. 

  1. Go to Setup | Lead
  2. Go to section Buttons, Links, and Actions
  3. Click New Button or Link to create a new button.

  1. Provide suitable Name and Label, e.g. Delete Lead and Emails
  2. Select the Display Type: Detail Page Button
  3. To make a button work on multiple selected records, please check Display Checkboxes (for Multi-Record Selection)
  4. Select Behaviour: Execute JavaScript and Content Source: OnClick JavaScript
    Please see the following screenshot for further clarification

8. Add following code/JS in the code area and make the following three changes in the code according to your object (please make the changes on the areas which are in bold):

  1. Enter the API name of your object on the first bold place in the code
  2. Enter the API name of the lookup field of your object on e2a email, on second bold place in the code
  3. Enter the API name of your object on the third bold place in the code
   var selectedRecords = '{!Lead.Id}';
userConsent = confirm('Record and its related Emails will be Deleted. Continue ? ');
        if(userConsent == true)
var relatedEmail = sforce.connection.query("SELECT Id FROM ortoo_e2a__EmailMessage__c WHERE ortoo_e2a__Lead_Id__c = '"+ selectedRecords + "'" );
var records = relatedEmail.getArray("records");
var result;
for (i = 0; i < records.length; i++) 
result = sforce.connection.deleteIds([records[i].Id]);
      'result :'+result);
if(result[0].getBoolean("success"))'Email has been deleted successfully');
else'Error deleting email : '+result); 
var idsForDeletion = [];
var delResult = sforce.connection.deleteIds(idsForDeletion);
if (delResult[0].getBoolean("success"))
alert('The Record Deleted Successfully.');
var res = sforce.connection.describeSObjects(['Lead']);
                var newId = res[0].keyPrefix;
                window.location.href = "/"+newId;
alert('The Record Could Not be Deleted. Error Message: ' +delResult[0].errors.message);
  alert('Error : The Action Could not be Completed. ');'Error while deleting :'+e);


  1. Press Save
  1. Now, go to section Page Layouts of Leads and press Edit to edit the layout 
  2. Go to section Buttons and drag down the custom delete button, to add into the detail page layout (please see the following screenshot for details):

  1. Press Save

The delete button will now be added on the Detail Page of the Lead object.


Please contact us at for any questions.


Was this information helpful?
Quick survey: How do you rate Email-to-anything?
★★★★ - GOOD
☆☆☆☆ - RUBBISH