You are here:
Archive ContentDocuments Using Parent Object Filtering Criteria
Archive ContentDocument records using filtering criteria based on parent objects in the Archive managed package. For example, you can archive files associated with Opportunities when the opportunity stage is Closed Lost. You can use this business case to archive specific files while retaining essential Opportunity records in the system. This approach identifies duplicate records.
Key Concepts
Before proceeding with archiving, it's essential to understand the distinction between ContentDocument and ContentVersion—two different objects in Salesforce:
- ContentDocument: Created when a file is uploaded to Salesforce, reflecting the file’s relationship to the parent object.
- ContentVersion: Represents the specific version of a document. For more details on ContentVersion, refer to this Salesforce knowledge article.
Querying ContentVersions by Parent Record
To retrieve a list of ContentDocument records linked to an Opportunity:
SELECT Id, ContentDocumentId, LinkedEntityId FROM ContentDocumentLink
WHERE LinkedEntityId IN (SELECT Id FROM Opportunity)You can then extract ContentVersion record IDs for the related ContentDocument.
SELECT ContentDocumentId, Id FROM ContentVersion- Configure Lookup Fields and Archiving Criteria
Create lookup fields and apply archiving criteria to your Archive ContentDocuments in the Archive managed package.
Configure Lookup Fields and Archiving Criteria
Create lookup fields and apply archiving criteria to your Archive ContentDocuments in the Archive managed package.
-
Create a lookup field on
ContentVersion.You need to create a lookup field on the
ContentVersionobject that references the parent object used for filtering. For example, the field name can beContentDocLookupOppty. -
Create a formula checkbox field.
Next, create a formula checkbox on the
ContentVersionobject that contains the archiving criteria. For example, the checkbox could be calledOpptyClosedLostwith this formula.ISPICKVAL(ContentDocLookupOppty__r.StageName ,'Closed Lost') -
Update lookup field with related records.
Use Salesforce Inspector, Workbench, or another third-party tool to update the lookup field with the correct related records. You can extract the related records with this query.
SELECT Id, ContentDocumentId, LinkedEntityId FROM ContentDocumentLink WHERE LinkedEntityId IN (SELECT Id FROM Opportunity)LinkedEntityId: The ID of the related parent record. -
Perform data update.
Match the
ContentDocumentIdwith the correspondingLinkedEntityIdand update the lookup field. Use a spreadsheet to help with this data update.- Copy-paste your
ContentDocumentLinkquery results in the first tab,ContentDocumentLink. (Only in the highlighted columns.) - Copy-paste your
ContentVersionquery results in the second tab,ContentVersion. (Only in the highlighted columns.) - The third tab,
ContentVersion_For_Upsert, is your output datasheet for upserting.You can download this specific sheet as a CSV file and import the records to your org. (Upsert - field mapping must include
IdandContentDocLookupOppty.)
- Copy-paste your
- To ensure that the lookup field populates in the future, you must create a flow that forces the field to update going forward.
To create the Archive policy, select ContentDocuments as the object and configure the fields.
Example Query:
SELECT Id FROM ContentDocument WHERE Id IN (SELECT ContentDocumentId
FROM ContentVersion WHERE Formula_checkbox_API_Name = TRUE)
