Automatic Folder Creation


You can automatically create a set of folders each time a parent object record is created.

This can be done via a trigger as shown below, or using Flows, which is documented in the Admin Guide.

We will use Account and cg_AccountFile_c object for our example. Each time an Account is created, S-Drive folders will be created for that Account.

  1. For the object you have, create an Apex Trigger.

  2. If you want this folder creation to take place after you create a new record, the trigger should work with an "after insert" statement.

  3. In the Apex Trigger, paste the following lines:

    trigger AccountFoldersTrigger on Account (after insert) { List<cg__AccountFile__c> folders = new List<cg__AccountFile__c>(); for(Account c : Trigger.new{ cg__AccountFile__c folder = new cg__AccountFile__c(); folder.cg__WIP__c = false; folder.cg__Content_Type__c = 'Folder'; folder.cg__File_Size_in_Bytes__c = 0; folder.cg__File_Name__c = 'Test Folder 1'; folder.cg__Description__c = 'Some description for folder'; //use a description field if needed. folder.cg__Account__c = c.Id; folders.add(folder); cg__AccountFile__c folder2 = new cg__AccountFile__c(); folder2.cg__WIP__c = false; folder2.cg__Content_Type__c = 'Folder'; folder2.cg__File_Size_in_Bytes__c = 0; folder2.cg__File_Name__c = 'Test Folder 2'; //use name as appropriate folder2.cg__Description__c = 'Some description for folder'; //use a description field if needed. folder2.cg__Account__c = c.Id; folders.add(folder2); if(folders.size() > 0){ insert folders; } }
  4. Add up to your requirement the number of folders created.

  5. Change the Folder Names by changing the “File Name” section.
    For our example, the field is written as “cg__File_Name__c”.

  6. You can add descriptions to your folders by changing the “Description” field.
    For our example, the field is written as “cg__Description__c”.

  7. Make sure your Apex Trigger is active.

  8. Test by inserting a new test record and you can use these folders according to your needs