Loading
Salesforce Enforces New Security Requirements in Summer 2026Read More
Build AI Solutions for Service
File with Dynamic Content: Example Apex Class

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

View supported editions for Einstein Bots.
View supported editions for Messaging.

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

//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.

 
Loading
Salesforce Help | Article