---
schema_version: "1"
id: 02392af34f7a0f67d79b8c98bf21e9ad1f59e9a6c3b7140f109dc2123d7f097b
title: "File with Dynamic Content: Example Apex Class"
collection: admin
version: current
locale: en-us
portal_url: https://docs.salesforce.com/admin/current/en-us/service/264-0-0/bots_service_enhanced_dynamic_file_apex.md
canonical_url: https://help.salesforce.com/s/articleView?id=service.bots_service_enhanced_dynamic_file_apex.htm&release=264.0.0&type=5
source_site: help.salesforce.com
content_type: documentation
content_hash: d2ac6b737866b22fbbd31b25ddd6c7b9446d1c5f267e22e29906de07ba404d1e
description: To send a dynamic file with your enhanced bot, create an Apex class that generates a file and returns the ContentVersionId variable as output.
products:
  - Service Cloud
  - Service Intelligence
release: "264"
---
# File with Dynamic Content: Example Apex Class

To send a dynamic file with your enhanced bot, create an Apex class that generates a file and returns the `ContentVersionId` variable as output.

### Required Editions

<table class="slds-table slds-table_bordered slds-max-medium-table_stacked slds-m-bottom_small edition"><colgroup><col></colgroup><tbody><tr class=""><td class="slds-cell-wrap" style="vertical-align:top;"><a href="https://help.salesforce.com/s/articleView?id=service.einstein_service_intro.htm&amp;language=en_US" target="_blank">View supported editions</a> for Einstein Bots.</td></tr><tr class=""><td class="slds-cell-wrap" style="vertical-align:top;"><a href="https://help.salesforce.com/s/articleView?id=service.messaging_editions.htm&amp;language=en_US" target="_blank">View supported editions</a> for Messaging.</td></tr></tbody></table>

This Apex class creates a file, uploads the file to Files home, and returns the file `ContentVersionId`.

```java
//GeneratePdf Class

public with sharing class Bot_GeneratePdf {

    @InvocableMethod(label='GeneratePdf')
    public static List<Id> generatePdf() {
        PageReference myPdf = new PageReference('/apex/CreateOutboundAttachmentPage');
        
        myPdf.getParameters().put('id', '001OG000003070ZYAQ');
        
        ContentVersion version = new ContentVersion();
        version.Title = 'Account Information';
        version.PathOnClient = 'AccountInfo.pdf';
        version.VersionData = myPdf.getContentAsPDF();
        version.IsMajorVersion = true;
        insert version;
         
        Id contentDocumentId = [SELECT ContentDocumentId FROM ContentVersion WHERE Id =:version.Id].ContentDocumentId;
        
        ContentDistribution cd = new ContentDistribution();
        cd.Name = 'Account Information Document';
        cd.ContentVersionId = version.id;
        cd.PreferencesAllowViewInBrowser=true;
        cd.PreferencesLinkLatestVersion=true;
        cd.PreferencesNotifyOnVisit=false;
        cd.PreferencesPasswordRequired=false;
        cd.PreferencesAllowOriginalDownload=true;
        cd.PreferencesExpires=true;
        datetime expiryTime = datetime.now().addMinutes(5);
        cd.ExpiryDate = expiryTime;
        insert cd;
        
        List<Id> contentVersionId = new List<Id>();
        contentVersionId.add(version.Id);
        
        return contentVersionId;
    }
}
```

After you create an Apex class, in a bot dialog, add an Apex action dialog step that references your Apex class and store the output in an ID list bot variable. In the associated File dialog step, reference the same ID bot variable.

#### See Also

- [Upload Files](https://help.salesforce.com/s/articleView?id=experience.collab_files_uploading.htm&type=5&language=en_US)
- [_Object Reference for the Salesforce Platform_: ContentDistribution](https://developer.salesforce.com/docs/atlas.en-us.object_reference.meta/object_reference/sforce_api_objects_contentdistribution.htm)