Tutorials
Document Management Tutorials
Delete attached folder when Account record is deleted
in this tutorial, we’ll demonstrate how to automatically delete an attached folder from your external storage system (google drive / sharepoint / box / etc ) when a salesforce record is deleted, using the cloudfiles–salesforce integration this setup is useful when you want to ensure folders do not remain in external storage after the related salesforce record is removed for example, when an account , opportunity , or case is deleted, the corresponding folder and its contents can also be deleted automatically what to expect by the end of this tutorial, you can expect to — automatically trigger cloudfiles automation to delete the attached folder from the connected external storage system when an account is deleted keep external storage clean and consistent by removing folders tied to deleted salesforce records pre requisites before you start with the set up, please ensure these steps are completed install cloudfiles in salesforce cloudfiles is available on the salesforce appexchange and can be installed using the standard app installation process check out https //help cloudfiles io/salesforce/installation guide for cloudfiles salesforce integration to know more connect your cloud storage to salesforce using cloudfiles all automations in cloudfiles are activated through salesforce flow builder, utilizing account wide authentication for external storage libraries through a service (admin) user to connect your service user, access the setup, click on the external storage of choice and log into your account for detailed steps, follow https //help cloudfiles io/salesforce/automations or refer to the gif below for a clearer understanding add cloudfiles widget to account page add the cloudfiles widget to account page in order to view the files and folders within salesforce follow the steps below go to any account record and click “edit page” button lightning builder for this page will open insert a new tab and drag and drop the "cloudfiles" widget save and exit the builder refresh the salesforce record page the widget should now show up follow the above steps and add cloudfiles widget to the lead record page as well refer to the gif below to get a visual understanding of this step the inserted widget may show a blank screen but you don't need to worry about it move on to the next step automatic folder creation for accounts for this tutorial, we require automated folders to be created upon the creation of new account to accomplish this, we will create a record triggered flow using cloudfiles actions for a comprehensive guide on the process of creating automated folders, please refer to our tutorial at docid\ ynzb pjpdnjkkttmq1p t this is how the flow would look like flow folder deletion on record deletion in this section, we will create two salesforce flows flow 1 captures the account deletion event and creates a cloudfiles event (cldfs cloudfilesevent c) record with the type set to custom account deleted flow 2 listens for this event type and triggers cloudfiles automation to delete the folder attached to the deleted account in your external storage system why two flows? salesforce record triggered flows that run “a record is deleted” trigger cannot reliably perform external actions (like deleting a folder in google drive/sharepoint) that’s why we capture the deletion as an event first, then process the deletion safely using a separate event driven flow flow 1 capture account deletion this flow runs when an account is deleted and creates a cloudfiles event record containing the deleted account’s record id this event is later used by flow 2 to trigger folder deletion 1 configure a record triggered flow go to setup → flows → new flow choose record triggered flow configure the start element object account trigger the flow when a record is deleted 2 create cloudfilesevent record create a new record of object cldfs cloudfilesevent c and use the following field values cldfs type c = custom account deleted cldfs request id c = {!$record id} flow 2 – process account folder deletion in this flow, we process the cloudfiles event record created by flow 1 and delete the folder attached to the deleted account this flow triggers when a cloudfiles event (cldfs cloudfilesevent c) record is created and the event type equals custom account deleted 1 configure a record triggered flow create a new record triggered flow select the object as cloudfiles event (cldfs cloudfilesevent c) configure the trigger trigger the flow when a record is created entry condition type equals custom account deleted set optimize the flow for actions and related records enable run asynchronously , since cloudfiles actions are executed asynchronously 2 get account folder once the flow is triggered, we need to identify the folder connected to the deleted account to do this, we use the cloudfiles apex action cloudfiles get connected folder add an action element and select cloudfiles get connected folder set the input value parent id = request id from the cloudfiles event ({!$record cldfs requestid c}) 3 delete account folder now that we have the folder details from the previous step, we can delete the folder from external storage using the cloudfiles apex action cloudfiles delete resource add an action element and select cloudfiles delete resource map the required inputs using outputs from get account folder drive id = outputs from get account folder → drive id library = outputs from get account folder → library resource id = outputs from get account folder → resource id resource type = folder save and activate the flows save and activate both flows once they are active, whenever an account is deleted, flow 1 creates a cloudfiles event (cldfs cloudfilesevent c) record with the type set to custom account deleted and stores the deleted account’s record id in the event this triggers flow 2 , which retrieves the connected folder using the cloudfiles event information and deletes the folder asynchronously from the configured external storage system test the flows in action now that everything is set up, let’s test the flows follow the steps below to confirm the folder deletion automation is working as expected create a new account record this should create and attach a folder that is visible in the cloudfiles widget on the account record page open the account record and confirm the folder is connected correctly in the cloudfiles widget delete the account record navigate to cloudfiles events (cldfs cloudfilesevent c) and confirm that a new event is created with type custom account deleted verify that the connected account folder has been deleted from your external storage system (google drive / sharepoint / box)