Skip to main content

Restoring files and attachments from a backup job

Gearset can restore deleted files (ContentDocument) and attachments from a data backup job

Alex Walter avatar
Written by Alex Walter
Updated over 2 weeks ago

Working with files in Salesforce can sometimes be a little tricky. There are a couple of different ways Salesforce can add files to a record: they can either be Attachment records (legacy format from pre-Lightning days) or they can be added as ContentDocument records (modern format). These have a few different layers and are linked up in the following way:

The files will be typically be shown under the Notes & Attachments section for a record such as an Account or a Contact:

For the ContentDocument type files, they are stored in the following way: The underlying data for the file is stored in a ContentVersion record, further information on who created the file etc., are stored in the ContentDocument record. This is linked to the underlying record (such as an Account, Contact, etc.) via a ContentDocumentLink junction object record.

If a record with an attached file has been deleted, the easiest way to restore this is via Restore <Object> with dependencies. We will walk through an example of a deleted Account record (which has files attached to it).

Initially we notice that the Account record has been picked up has been deleted when we view the run:

Once we click on Restore or Restore Account with Dependencies:

Gearset will show you a dependency tree of records that have been deleted:

Select the Files (ContentDocument) - this will include all the related records for restoration in a single-click: the associated ContentDocument, ContentVersion, ContentDocumentLink records. Click continue to build up a restoration plan. This will also automatically take you through the problem analyzer steps as well as the comparison for disabling validation rules, flows and triggers:
​

Then Deploy Data to restore the data to your Salesforce org. Once this completes, it will show a summary like this:

The contact record along with the attached file are now restored to Salesforce:

If you are looking to deploy attached files to another org, please see this documentation.

Troubleshooting

INVALID_CROSS_REFERENCE_KEY on ContentDocumentLink

Salesforce has a complex permission and settings model and some of the behaviour can be unexpected. One example of this is the Query non-vetoed files permission. We have seen examples when this permission is enabled & you try and restore a file, it gets automatically deleted, resulting in this error INVALID_CROSS_REFERENCE_KEY on ContentDocumentLink. Our recommendation is to disable this setting at least for the duration of the restoration.

Please reach out to us in the chat widget if you would like any further help with this or any other issues.

Did this answer your question?