How To: Add CSS Style to the Clone, Edit, and Copy pages
June 2, 2019

How To: Clone files and documents in Salesforce

Documents and files are stored two different ways in Salesforce. Traditionally, files were saved in an object called “Attachments”. They were a direct child of the object they related to. Now Salesforce stores fines in a much more complex set of objects. Benefits of the new structure include versioning and the ability for one copy of a document to be referenced by multiple objects.

Super Clone Pro has the ability to clone the file’s reference to the document. This is called the ContentDocumentLink. After cloning, both the original records and new records will be linked to the same document stored on the system. This is great for saving storage space, but you may have a use case that requires the newly cloned records reference their own version of the documents.

Super Clone Pro does not clone the document, but with an additional class and process builder you can clone the document.

Summary

  1. Create the Apex class and test class below that will clone a document/file.
  2. Create a new field on the object to receive the source record’s Id value.
  3. Update the Clone Configuration to assign the record Id value to the new source field.
  4. Create a Process Builder flow to only run on insert and will execute the Apex class created in step 1.

1. Apex Class & Test Class

The Apex class can be run from Process Builder. It expects parameters of a source record Id that has related documents and a record Id that will receive copies of the documents.

If you would like to only clone specific record types, change the ContentDocumentLink query to include FileExtension filtering in the where clause. This is around line 47.

 

Create a class named “PbCloneDocuments“, and paste in the code below.

Create a class named “PbCloneDocumentsTest“, and paste in the code below.

2. New Object Field

Add a field to the object to store the original record’s Id value. This can be a lookup or an 18 position text field. This example uses a text field, so it doesn’t add the overhead of an index that comes with lookup fields.

3. Super Clone Pro Configuration

We do not need to clone the “Content Document Link” relationship because the file cloning will occur in our custom class. Remove this relationship if it is currently in the configuration.

Change the field configuration settings for the new field. We will use the “Action Formula List” option, and set the Value text to “FIELD(Id)”.  This will tell the cloning process to set the field to the Id of the record being cloned.

4. Process Builder

Create a new Process Builder flow for your object. This flow should only run on Insert. The flow condition should make sure the new source record Id field has a value, and then the Action should call Apex class action to “Clone Documents” that we created in the first step.

The flow condition below references the new source field checks if it is not equal to the global constant of Null.

Set the “Destination Record Id” as a Field Reference to the object’s Id. Set the “Source Record Id” as a Field Reference to the new source field that was created.

 

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.