You are here:
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.
You are here:
To send a dynamic file with your enhanced bot, create an Apex class that generates a file
and returns the ContentVersionId variable as
output.
| 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.

We use three kinds of cookies on our websites: required, functional, and advertising. You can choose whether functional and advertising cookies apply. Click on the different cookie categories to find out more about each category and to change the default settings.
Privacy Statement
Required cookies are necessary for basic website functionality. Some examples include: session cookies needed to transmit the website, authentication cookies, and security cookies.
Functional cookies enhance functions, performance, and services on the website. Some examples include: cookies used to analyze site traffic, cookies used for market research, and cookies used to display advertising that is not directed to a particular individual.
Advertising cookies track activity across websites in order to understand a viewer’s interests, and direct them specific marketing. Some examples include: cookies used for remarketing, or interest-based advertising.