Saturday, July 25, 2015

Salesforce: Mass Delete Records for Custom Object

Mass delete records in Salesforce should be an easy job for experience Salesforce admin. Sometimes you need to delete all data after performing sanity test, integration test, and stress test in Production for deployment of new functionalities, which may include create new records in new custom objects.

In standard record such as: Lead, Account, Contact, Case admin can easy mass delete records by navigate to the object tab, and click the Mass Delete object link below the Tools heading. The same can be achieve by navigate to Setup | Data Management | Mass Delete Records. While, this functionality is not exist for custom object.

It would be just a few clicks in Data Loader to extract the record Id, then use the record Id from data extracted to delete the whole record. But, if we are working with hundred thousands or millions of records, it will take you hours to extract the Id and mass delete the records.

For custom object, Salesforce have Truncate function to perform this activity in few clicks and very fast. This action will permanently remove all data, but all the fields and rules are not touched.

If you do not see Truncate button in the custom object, check SetupCustomize | User Interface | Enable Custom Object Truncate

  1. From Setup, click Create | Objects.
  2. Click an object name to go to the object’s detail page and click Truncate.
  3. In the Confirm Custom Object Truncate window, review the warning and then enter the object name of the object to truncate in the text box provided.
  4. Click Truncate.

Records deleted is not send to Recycle Bin, but Salesforce will create a new object end with _trunc. This object appears in the Deleted Objects list for 15 days, and contain all fields and records deleted from the original object, so this would be like a temporary backup.

Notes related to Truncate object
  • Truncating a custom object permanently removes all of its records
  • You can’t recover the records from the Recycle Bin
  • A copy of the truncated object appears in the Deleted Objects list for 15 days—during this period the object and its records continue to count toward your organization’s limits—and then the copied object and its records are permanently deleted.
  • You cannot truncate object with one or more child objects as containing data, this include Master-Detail and Lookup relationship.

Behind the screen : when you do object truncate:
  • Salesforce will delete existing object and create new object with the same structure
  • If you have auto-number field, the number will be continue from truncated object

Restore the object : when you restore the deleted objects, some attributes of deleted custom objects are not automatically restored and you need to restore these manually:
  • The custom object was set to In Development mode. Only users with the “Customize Application” permission can see the object tab, search results, related lists, and report data types.
  • Recreate a custom tab and any list views for the object.
  • Rebuild any workflow rules on the object.
  • Reactivate any custom validation rules for the object.
  • Reactivate any approval processes for the object.
  • Open and save any custom formula fields on the custom object to enable them.
  • Rebuild and reactivate any matching rules or duplicate rules for the object.
  • On the page layouts of other objects, add the custom object related list, button, or link to any page layouts that have been edited while the object is deleted. 
  • Related lists, buttons, or links to this object are automatically restored if the page layout is not edited while the object is deleted.
  • For custom report types where the object is not the main object, add the reference to the custom object back to the custom report types. Existing field will not point the parent object, but just as a text field.
  • Reports based on the custom report type are automatically restored If not edited while the object is deleted. 
  • Recreate any reports that have been edited.
  • It may take several hours before you can search for records in the object.

You can’t truncate standard objects or custom objects that:
  • Are referenced by another object through a lookup field or that are on the master side of a master-detail relationship
  • Are referenced in a reporting snapshot
  • Have a custom index or an external ID
  • Have activated skinny tables
  • When your organization has reached its limit of the allowed number of custom objects.