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


Custom Delete button for deleting emails along with the record in List View

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 setup 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 new button.

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

8. Add following code/JS in the code area and press Save (Please change the object name i.e. Lead, with the API name of the object for which you want to configure this button and the API name of its lookup field on e2a email object. For further clarification, we have bolded the areas which need to be edited)

{!REQUIRESCRIPT('/soap/ajax/29.0/connection.js')}
try
{
   var selectedRecords = {!GETRECORDIDS( $ObjectType.Lead )};
   if(selectedRecords.length<1)
alert('Please Select at Least One Row !');
   else
   {
userConsent = confirm(selectedRecords.length +' Record(s) and there 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 IN('"+ selectedRecords.join("','")+ "')" );
var records = relatedEmail.getArray("records");
var result;
for (i = 0; i < records.length; i++) 
result = sforce.connection.deleteIds([records[i].Id]); 
if(result[0].getBoolean("success")) 
console.info('Email has been deleted successfully');
}
else
console.info('Error deleting email : '+result); 
}
}
var 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('Error : The Action Could not be Completed. ');
  console.info('Error while deleting :'+e);
}

 

  1. Now, on the same Lead's page, go to Search Layout section and Edit the Layout "Lead List View"

  1. Under Custom Buttons section, add 'Delete Both' to Selected Buttons column from Available Buttons and press Save.

Delete button will now be added on the list view pages of Lead object.

 

Please contact us at support@ortooapps.com for any questions.

 

 

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

-