Lightning Flows
ContentVersion(Files) Creation Using Lightning Flows

ContentVersion(Files) Creation Using Lightning Flows

What is ContentVersion?

ContentVersion represents a specific version of a document in Salesforce CRM Content or Salesforce Files. This object is available in versions 17.0 and later for Salesforce CRM Content documents. This object is available in versions 20.0 and later for Salesforce Files.

The maximum number of versions that can be published in a 24-hour period is 200,000.

Note: Depending on how files are shared, queries on ContentDocument and ContentVersion without specifying an ID won’t return all files a user can access. For example, if a user only has access to a file because they have access to a record that the file is shared with, it won’t be returned in a query such as “SELECT Id FROM ContentDocument.”

ContentDocument parentId - Salesforce Stack Exchange

ContentDocument: This object record you don’t have to create. It gets created when you create ContentVersion, which is the child of ContentDocument.

ContentVersion: This object stores document information similar to Attachment.

ContentDocumentLink: This object will share the files with Users, Records, Groups, etc. You can create multiple records to attach the same files under multiple records.

Reference guide to learn more : https://developer.salesforce.com/docs/atlas.en-us.sfFieldRef.meta/sfFieldRef/salesforce_field_reference_ContentVersion.htm

Create Lightning Flow to Create ContentVersion

1. Flow Action

Create an Apex class to be called in Flow Core Action

public class ContentVersionExample {
@InvocableMethod(label = 'Create ContentVersion')
public static FlowOutput[] createFile(FlowInput[] inputs) {
ContentVersion[] createfiles= new ContentVersion[]{};
for(FlowInput input : inputs) {
ContentVersion file = new ContentVersion();
String data;
if(input.fileExtension == 'INPUT') {
data = input.data.escapeXML().replace('\r\n', '
').replace('\r', '
').replace('\n', '
').replace(''', ''');
} else {
data = input.data;
}        
file.Title = input.title;
        file.PathOnClient = file.Title + '.' + input.fileExtension;
        file.VersionData = Blob.valueOf(data);
        if(String.isNotEmpty(input.parentId)) {
            file.FirstPublishLocationId = input.parentId;
        }
        createfiles.add(file);
    }
    insert createfiles;
    FlowOutput[] outputs = new FlowOutput[]{};

    for(ContentVersion cv : [SELECT Id,ContentDocumentId FROM ContentVersion WHERE Id IN :createfiles]) {
        FlowOutput output = new FlowOutput();
        output.contentDocumentId = cv.ContentDocumentId;
        outputs.add(output);
    }

    return outputs;
}

public class FlowInput {
    @InvocableVariable(label='ContentVersion Title' required='true')
    public String title;
    @InvocableVariable(label='ContentVersion Data' required='true')
    public String data;
    @InvocableVariable(label='ContentVersion Extension' required='true')
    public String fileExtension;
    @InvocableVariable(label='Related Record ID')
    public String parentId;
}
public class FlowOutput {
    @InvocableVariable(label='ContentDocument Id')
    public String contentDocumentId;
}
}

2. Lightning Flow

We are going to consider below main parameters inside Flow

ParameterUse for InputUse for OutputDescription
File DatayesString input that’ll be stored as VersionData. For images, the string input should be base64 encoded string.
File ExtenstionyesType of the file. For notes, it should be ‘SNOTE’.
File TitleyesTitle of the file.
Related Record IdyesID of the record where the file should be added.
ContentDocument IDyesContentDocument ID of the created file(ContentVersion).

Lets create a screen Flow.

Go to setup –> Flows –> Click on New Flow Button –> Select Screen Flow Type.

Drag and Drop ‘Action’ and Select Apex class created.

You will see all flow input variables we added in apex.

I am creating 2 resources here one for contentversionData as one template

and one for RecordId

This image has an empty alt attribute; its file name is image-58.png

now the flow looks like below after connected to screen element.

Activate the Flow and Debug.

before debugging i created a contact. And Notes and Attachments is zero

This image has an empty alt attribute; its file name is image-59.png

I used contactid as my record id ‘inputid’ variable for input.

Debug Looks like this, and the content document is created.

This image has an empty alt attribute; its file name is image-60.png

Result:

This image has an empty alt attribute; its file name is image-17.png
This image has an empty alt attribute; its file name is image-62.png

Thanks for Reading…

Leave a Reply

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