Archiving ContentDocument records with filtering criteria from the parent object

     

    Before archiving, it is important to understand the difference between Content Document and Content version (two different objects in Salesforce):

    Whenever a file is uploaded to the Salesforce CRM library, or while uploading a file for any record in its record page, a ContentDocument is created, reflecting the record details and relations to the parent object. For more information on how the Content Version object is structured, please refer to the following Salesforce knowledge article.

    Querying ContentVersions by Parent Record

    The following query results in a list of all ContentDocuments that have an Opportunity parent record

    SELECT Id, ContentDocumentId, LinkedEntityId FROM ContentDocumentLink WHERE LinkedEntityId IN (SELECT Id FROM Opportunity)


    A ContentDocument’s fields and relations are described under the ContentVersion object in Salesforce Setup.

    Run the following query to extract record IDs:

    SELECT ContentDocumentId, Id FROM ContentVersion

    The query extracts the list of IDs of ContentVersion for the related contentDocument. 

    This article outlines how to archive ContentDocument records with filtering criteria from the parent object. For example, archiving files that are related to Opportunities when the opportunity stage is ‘Closed Lost’. This is a business use case where the organization would like to archive only the files where the Opportunity record for example needs to be kept in the environment in order to identify duplicate records in the future. 

    Archiving ContentDocument records with filtering criteria from the parent object

    For example, archiving files related to Opportunities when the opportunity stage is ‘Closed Lost’. This is a situation where an organization only wants to archive files e.g. an Opportunity record, needs to be kept in the environment so that duplicate records can be identified in the future. 

    1. A lookup field must be created on the Content Version object in Salesforce:
    1. The lookup must refer to the object that you will base the criteria on. 
    2. In the example the field name is ‘ContentDocLookupOppty’.

     

    1. A Formula Checkbox field must be created in the Content Version object:
       
      1. This checkbox contains the criteria for archiving the files.
      2. In the eample the formula checkbox is named: ‘OpptyClosedLost’.
      3. The formula in this example is:
     ISPICKVAL( ‘ContentDocLookupOppty’__r.StageName ,'Closed Lost')

    1. The lookup field must be updated with the correct related records:
    1. You can do this using SF inspector/Workbench or any other 3rd party tool to extract the related records.
    2. To extract the related records you can use the following query with Developer Console, Salesforce Inspector or other third-party tools:

    1. SELECT Id, ContentDocumentId, LinkedEntityId FROM ContentDocumentLink WHERE LinkedEntityId IN (SELECT Id FROM Opportunity)
    2. LinkedEntityId = The Id of the related parent record.

     

    1. Perform an update on the Content Version object by matching the ContentDocumentId and mapping the LinkedEntityId to the lookup field. You may refer to the following spreadsheet example to assist with your data update operations.

     

    1. Copy-Paste your ContentDocumentLink’ query resultsin the firstTab “ContentDocumentLink” (Only in the highlighted columns).
       

    1. Copy-Paste your ‘ContentVersion’ query results in the secondTab “ContentVersion” (Only in the highlighted columns).


    1. 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 Salesforce org (Upsert - field mapping should include id & ContentDocLookupOppty ).

    1. To ensure that the lookup field populates in the future you must create a flow that forces the field to update going forward.

    Creating 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)

    « Previous ArticleNext Article »


    Contact Us

    Sometimes you just want to talk to someone. Our customer support team is available by phone:
    Request a Technical Support Call Back