Tuesday, June 18, 2013

Salesforce: How to undelete records

Salesforce keep record you delete to Recycle Bin for 15 days with maximum record of 25 times the Megabytes (MBs) in your storage. For example, if your organization has 1 GB of storage then your limit is 25 times 1000 MB or 25,000 records. If your organization reaches its Recycle Bin limit, Salesforce automatically removes the oldest records if they have been in the Recycle Bin for at least two hours.

You can manually undelete records by click Recycle Bin icon in bottom left menu. Undelete account will include contact, case, opportunity tagged to that Account. But, if you have many records or you have specific record Id or you want to undelete records with some conditions, manually delete is not an option, use Apex code for it.

You not really need to be good developer to utilize Apex code. Here we go:

1. Open Developer Console
From your name in top right, look for Developer Console

2.From Developer Console
Go to Debug menu and select Open Execute Anonymous Window

3. Enter SOQL 
Account[] a = [SELECT Id FROM Account WHERE name = 'singtel' ALL ROWS];
undelete a;
Important to add ALL ROWS, otherwise deleted records will be not return on query.

4. Monitor Apex Debug Log
Go to Monitor - Logs - Debug Logs (in Salesforce menu, not Developer console)
Add user to monitor, default it will capture 20 log request for the users

5. Execute 
Click Execute button in Enter Apex Code window

6. Back to Debug Log
Select latest debug log and click View to monitor


  1. how to undelete multiple records

    1. you can play around with the SOQL as sample in this blog

  2. Hi I get this error when I try to undelete contacts:

    Line: 1, Column: 1
    System.DmlException: Undelete failed. First exception on row 1 with id 0034H00001pXw3lQAC; first error: DUPLICATE_VALUE, duplicate value found: TR1__External_Candidate_Id__c duplicates value on record with id: 0034L00000MhYRH: []

    how can I workaround this? I have 260k contacts to restore

    1. can you remove Unique flag in TR1__External_Candidate_Id__c?


Page-level ad