top of page

Recovering Deleted Records using Apex Triggers in Salesforce

Salesforce is a powerful customer relationship management (CRM) platform that allows organizations to manage their sales, customer service, and marketing processes in one place. While the platform provides several ways to recover deleted records, such as the Recycle Bin and the data recovery feature, sometimes it may not be possible to restore data using these methods. In such scenarios, one option is to use Apex triggers to recover deleted records in Salesforce.


Apex triggers are custom logic that execute before or after a specific event, such as creating, updating, or deleting a record. In this blog post, we’ll explore how to use an Apex trigger to recover deleted records in Salesforce.


Step 1: Create a Custom Field


Before creating the trigger, we need to create a custom field on the object for which we want to recover deleted records. This custom field will be used to track whether a record has been deleted or not.


To create a custom field:

  • Go to the object for which you want to recover deleted records.

  • Click on the Object Manager tab.

  • Click on the object name, and then click on the Fields & Relationships section.

  • Click on the New button, and then select Checkbox as the field data type.

  • Provide a name for the field, such as “Is Deleted”.

  • Click on the Save button.

Step 2: Create an Apex Trigger


Once the custom field is created, we can now create the Apex trigger. The trigger will run before a record is deleted, and it will update the custom field to indicate that the record has been deleted. This way, when we want to recover the deleted record, we can simply update the custom field back to its original value.

To create an Apex trigger:

  • Go to the Developer Console in Salesforce.

  • Click on the File menu, and then click on New > Apex Trigger.

  • Provide a name for the trigger, such as “UndeleteRecord”.

  • Select the object for which you want to recover deleted records.

  • Select the “before delete” option, as we want the trigger to run before a record is deleted.

  • Click on the Create button.

  • Copy and paste the following code into the Apex trigger:

trigger UndeleteRecord on Contact (before delete) {
  for (Contact c : Trigger.old) {
    c.Is_Deleted__c = true;
  }
}
  • Click on the Save button.

Step 3: Test the Apex Trigger


Now that the Apex trigger is created, we can test it to ensure that it works as expected.

To test the Apex trigger:

  • Go to the object for which you want to recover deleted records.

  • Create a new record, and then delete it.

  • Go to the Recycle Bin, and then restore the deleted record.

  • Verify that the custom field on the restored record is set to “False”, which indicates that the record has not been deleted.

Conclusion


Apex triggers provide a flexible and powerful way to recover deleted records in Salesforce. By creating a custom field to track whether a record has been deleted or not, and then using an Apex trigger to update the field before a record is deleted, organizations can easily recover deleted records when needed. With this approach, organizations can ensure that their data is always up-to-date and available, even if it was accidentally deleted.

bottom of page