Apex
Examples
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 pre requisites for using cloudfiles apex 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 docid\ fv1sn qekhvsae njyp6y and steps 1 & 2 mentioned in the connect external folder to a record docid\ inrzc0sn0m0qq8mqxnywg as well to make sure you are setup step 1 create an apex trigger on account we will create a trigger that executes after an account record is inserted this trigger will call a helper class that handles folder creation trigger accounttrigger on account (after insert) { // call the helper class and pass the newly created account records cloudfileshelper execute(trigger new); } 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 step 2 create the helper class 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 public with sharing class cloudfileshelper { public static void execute(list\<account> accounts) { set\<id> accountids = new set\<id>(); for (account acc accounts) { accountids add(acc id); } // call future method to create folders createfoldersforaccounts(accountids); } // method to create folders for accounts @future(callout=true) public static void createfoldersforaccounts(set\<id> accountids) { list\<account> accounts = \[select name from account where id in \ accountids]; string libraryid = 'sharepoint'; // change this based on your storage type string runas = 'integrationuser'; list\<cldfs types createfolderinput> parentinputs = new list\<cldfs types createfolderinput>(); for (account acc accounts) { parentinputs add(new cldfs types createfolderinput( '01kehakur56vlaeu3oujhlnaqqq67t36bj', acc name, // folder name same as account name 'b!njbtpq z5uejtfrj f24xeqqlpud8vtfsg7gbcrxjepiun9bkb 7qbdptqhvd cp', // drive id null )); } // create parent folders list\<cldfs resource> parentfolders = cldfs client createfolders(parentinputs, libraryid, runas); map\<string, id> folderidtorecordidmap = new map\<string, id>(); integer index = 0; for (cldfs resource parentfolder parentfolders) { folderidtorecordidmap put(parentfolder id, accounts\[index] id); index++; } list\<cldfs types createattachmentinput> attachmentinputs = new list\<cldfs types createattachmentinput>(); for (cldfs resource parentfolder parentfolders) { attachmentinputs add(new cldfs types createattachmentinput( parentfolder id, // parent folder id 'folder', // file or folder folderidtorecordidmap get(parentfolder id), // correct salesforce record id 'b!njbtpq z5uejtfrj f24xeqqlpud8vtfsg7gbcrxjepiun9bkb 7qbdptqhvd cp' // drive id for google drive or sharepoint )); } // create attachments list\<cldfs attachment> attachments = cldfs client createattachments(attachmentinputs, libraryid, runas); list\<cldfs types createfolderinput> childinputs = new list\<cldfs types createfolderinput>(); for (cldfs resource parentfolder parentfolders) { childinputs add(new cldfs types createfolderinput( parentfolder id, // parent folder id 'documents', // subfolder name 'b!njbtpq z5uejtfrj f24xeqqlpud8vtfsg7gbcrxjepiun9bkb 7qbdptqhvd cp', null )); childinputs add(new cldfs types createfolderinput( parentfolder id, // parent folder id 'contracts', // another subfolder 'b!njbtpq z5uejtfrj f24xeqqlpud8vtfsg7gbcrxjepiun9bkb 7qbdptqhvd cp', null )); } // create subfolders inside each parent folder list\<cldfs resource> subfolders = cldfs client createfolders(childinputs, libraryid, runas); } } 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 parameters cldfs client createfolders parentinputs / childinputs types createfolderinput docid\ boecl uvwqylalrg1e6r8 libraryid possible values are sharepoint , google , azure , onedrive , dropbox , box , s3 , sftp or cloudfiles runas integrationuser cldfs client createattachments attachmentinputs types createattachmentinput docid 18imxnekouenqgggsb92h libraryid possible values are sharepoint , google , azure , onedrive , dropbox , box , s3 , sftp or cloudfiles runas integrationuser next steps expand the logic to create additional subfolders automatically adjust the trigger to run only for certain account type s if necessary now you have an automated folder creation system in salesforce using apex & cloudfiles!