It is not possible to successfully create a folder within an Apex trigger(After Insert). The return value is NULL

We are currently implementing a functionality to automatically create a Box folder upon record creation in Salesforce.

However, we are facing an issue where the folder creation process does not occur (no folder is created, and the return value is NULL) when using the createFolderForRecordId method from the Salesforce Developer Toolkit within an after insert trigger on record creation.

We have confirmed that the process works correctly and the folder is created when manually triggered from the Developer Console.

Could you please provide guidance on the possible cause of this issue? If it is not possible to achieve automatic folder creation within the trigger, could you please suggest an alternative solution for resolving this requirement?

I got a error message as below.

18:18:41:463 USER_DEBUG [572]|DEBUG|Unable to create folder for record id a0E1m000007S0gcEAC - Details: Unable to use default credentials to make a callout to Box (maybe this is in a trigger, constructor, or after a DML statement):

Hi @peifengyuan!

I think there are two potential ways to solve your problem:

  1. If you would like to continue to use an Apex trigger, you will need to use the createFolderForRecordId method call in a separate class. The Box Developer Toolkit leverages future callouts which is why you’re receiving this error.

  2. Alternatively, you can leverage the Salesforce Flow invocable actions that we ship with the Box for Salesforce managed package: https://developer.box.com/guides/tooling/salesforce-toolkit/flow-actions/

1 Like

Hi @kadams
Thank you so much!!
I will try it out right away. Have a great day!

Thank you & Best Regards,
peifeng yuan

Hi @kadams
Sorry to disturb you again.
I tried separating the class and calling it from the trigger, but I still encountered the same error. Here is my code. Could you please tell me what is wrong with it?

BoxCommonUtil class

public class BoxCommonUtil {
    public BoxCommonUtil() {
    }

    public static void creatBoxFolderNew(Project__c [] newObjs) {

        System.debug('run create box folder');
        // IdList
        List<Id> projectIdList = new List<Id>();
        for(Project__c project : newObjs) {
            projectIdList.add(project.Id);
        }

        List<Project__c> boxProjectList = [SELECT Id,
                                                  Name,
                                                  PJ_ProjectKeyNo__c,                   
                                                  PJ_ProjectManagementDepartment__c,
                                                  FROM Project__c 
                                                  WHERE Id IN :projectIdList];

        // get Folder Details info
        Map<String, box__Folder_Details__c> bfdMap = box__Folder_Details__c.getAll();
        //Map<Folder Name, ForderId>
        Map<String, String> bfdNameIdMap = new Map<String, String>();
        for(box__Folder_Details__c bfd : bfdMap.values()) {
            bfdNameIdMap.put(bfd.box__Folder_Name__c, bfd.box__FolderId__c);
        }

        List<Project__c> updateProjectList = new List<Project__c>();

        box.Toolkit boxToolkit = new box.Toolkit();

        for(Project__c project : boxProjectList) {

            String folderNameOverride = project.PJ_ProjectKeyNo__c + '_' + project.Name;
            System.debug(folderNameOverride);
            System.debug(project.Id);
            String projectFolderId;
            try{
                // create box Folder
                projectFolderId = boxToolkit.createFolderForRecordId(project.Id, folderNameOverride, true);
                // get recent error
                System.debug(boxToolkit.mostRecentError);
            } catch(Exception e) {
                System.debug(e.getMessage());
            }

            // move folder
            String newParentFolderId = bfdNameIdMap.get(project.PJ_ProjectManagementDepartment__c);
            // run move folder
            Boolean isMoved = boxToolkit.moveFolder(projectFolderId, newParentFolderId, null);
            System.debug(isMoved);

            // 
            project.PJ_ProjectFolderId__c = projectFolderId;
            project.PJ_BoxLink__c = System.Label.CON_BOX_Url + projectFolderId;
            updateProjectList.add(project);
        }
        boxToolkit.commitChanges();

        update updateProjectList;

    }
}

Trigger Handler class

    public void onAfterInsert(Project__c [] newObjs, Map<ID, Project__c> newObjMap) {

        // Create Box Folder
        try {
            // creatBoxFolder(newObjs);
            BoxCommonUtil.creatBoxFolderNew(newObjs);
        } catch(Exception e) {
        }
        
    }

Hi @kadams

The issue has been resolved! I forgot to include the @future annotation…
I made the following modifications to the source code.
thank you so much!!

BoxCommonUtil class

public class BoxCommonUtil {
public BoxCommonUtil() {
}
@future (callout=true)
// public static void creatBoxFolderNew(Project__c [] newObjs) {
public static void creatBoxFolderNew(List<ID> projectIdList) {
1 Like