Automatically Creating Folders and Sub-Folders in External Storage Using Apex
In this tutorial, we’ll walk you through setting up an Apex Trigger on the Account object in Salesforce. This trigger will automatically create a folder in external storage whenever a new Account is created. We will use CloudFiles to handle folder creation and allow multiple subfolders inside each parent folder.
In order to use CloudFiles Apex, make sure you have CloudFiles installed in the correct sandbox / production environment. Also make sure you have authenticated your Cloud Drive using CloudFiles atleast once.
Simply follow Steps 1 & 2 mentioned in the Installation guide and Steps 1 & 2 mentioned in the Connect external folder to a record as well to make sure you are setup.

We will create a trigger that executes after an Account record is inserted. This trigger will call a helper class that handles folder creation.
What this does:
- The trigger runs after an Account is created.
- It calls the CloudFilesHelper.execute() method.
- This method will create a folder and subfolders in external storage for each new account and attach the parent folder to the respective records.
The helper class will:
- Create a main folder with the account's name
- Create subfolders inside the main folder, like Documents and Contracts
- Manage multiple account records efficiently
NOTE: The method below makes a callout to CloudFiles' server, which then creates and attaches the folders. By default, you cannot make a callout synchronously in Salesforce; you must wrap this method in some form of asynchronous Apex.
We at CloudFiles have built an AppExchange app to work with files easily in Salesforce. Using CloudFiles, you can avoid setting up an API server, setting up authentications or building integrations with different cloud storage platforms.
Below is an Apex code snippet demonstrating how to create folders automatically:
What this does:
- Loops through all new Accounts and creates a Parent Folder named after the Account.
- Attaches the Account Folder to the respective record.
- Creates "Documents" and "Contracts" subfolders inside each parent folder.
- Handles multiple Account records efficiently.
This approach allows you to seamlessly automate folder creation within any external storage system using Apex and CloudFiles. Additionally, you can create any number of subfolders within the parent folder by repeating the subfolder creation process and using the parent folder ID dynamically. This makes it easy to manage complex folder structures programmatically.
cldfs.Client.createFolders
- libraryId - Possible values are sharepoint, google, azure ,onedrive, dropbox, box, s3, sftp or cloudfiles
- runAs - integrationUser
cldfs.Client.createAttachments
- libraryId - Possible values are sharepoint, google, azure ,onedrive, dropbox, box, s3, sftp or cloudfiles
- runAs - integrationUser
- Expand the logic to create additional subfolders automatically.
- Adjust the trigger to run only for certain Account types if necessary.
Now you have an automated folder creation system in Salesforce using Apex & CloudFiles!