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.
- Go to Setup | Lead
- Go to section Buttons, Links, and Actions
- Click New Button or Link to create a new button.
- Provide suitable Name and Label, e.g. Delete Lead and Emails
- Select the Display Type: Detail Page Button
- To make a button work on multiple selected records, please check Display Checkboxes (for Multi-Record Selection)
- 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):
- Enter the API name of your object on the first bold place in the code
- Enter the API name of the lookup field of your object on e2a email, on second bold place in the code
- Enter the API name of your object on the third bold place in the code
{!REQUIRESCRIPT('/soap/ajax/29.0/connection.js')}
try
{
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]);
console.info('result :'+result);
if(result[0].getBoolean("success"))
{
console.info('Email has been deleted successfully');
}
else
{
console.info('Error deleting email : '+result);
}
}
var idsForDeletion = [];
idsForDeletion.push(selectedRecords);
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;
}
else
alert('The Record 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);
}
|
- Press Save
- Now, go to section Page Layouts of Leads and press Edit to edit the layout
- 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):
- Press Save
The delete button will now be added on the Detail Page of the Lead object.
Please contact us at support@ortooapps.com for any questions.
★★★★★ - EXCELLENT
★★★★☆ - GOOD
★★★☆☆ - OK
★★☆☆☆ - POOR
★☆☆☆☆ - RUBBISH