function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Jonathan Wolff 7Jonathan Wolff 7 

Created Pdf is not working with flow and apex class

Hello,

I want to create a pdf through a flow. I made a flow with an apex action that creates a pdf in files. The problem I have is, that when I click on the pdf i do not see the text template but an error message that something worked wrong. Could you tell me what to chamge to make it work?


The apex class:

public class ContactContentVersion {
    @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 == 'PDF') {
                data = input.data.escapeXML().replace('\r\n', '<br>').replace('\r', '<br>').replace('\n', '<br>');
            } 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;
    }
}