Welcome to the new Box Support website. Check out all the details here on what’s changed.

Salesforce auto-create folders?

New post

Comments

11 comments

  • Brad

     Hey there! That totally makes sense -- while I don't have a specific script on hand for you, have you checked out our Developer Toolkit for the Box for Salesforce integration to investigate automating this out in your environment?

    0
    Comment actions Permalink
  • Darrin

    There are 2 specific Apex examples in the link that  offered.  Used that myself to go instantiate Box folders for Salesforce records - took all of about 2 minutes.

     

    Since it is considered a callout by Salesforce, you'll want to do some batching of mass updates.

     

    In our Account insert trigger:

    NOTE: You could put this code in an Anon window if you have catch up to do.  Just make necessary accomodations to the batch.query for the records you want to seed w/ Box folders.

     

    if (Trigger.newMap.size() == 1) {
            BoxHandler.createFolderFuture(Trigger.new[0].Id);
    } else {
    	BoxFolderCreateBatch batch = new BoxFolderCreateBatch();
            batch.records = new List(trigger.newMap.keySet());
            batch.query = 'SELECT Id FROM Account WHERE Id IN :records';
                
            Database.executeBatch(batch);
    }

    Then the BoxHandler class:

    public class BoxHandler {
         @future (callout=true)
         public static void createFolderFuture(Id record) {
             
            box.Toolkit boxToolkit = new box.Toolkit();
            
            Account account = [SELECT Id FROM Account WHERE Id=:record];
            
            System.debug(LoggingLevel.FINE, 'Creating a workspace for Account(' + account.Id + ')');
            account.BoxFolder__c = boxToolkit.createFolderForRecordId(account.Id, null, true);
            update account;
            
            boxToolkit.commitChanges();
        }
        
        public static void createWorkspaces(List objects) {
            
            box.Toolkit boxToolkit = new box.Toolkit();
            
            List accounts = [SELECT Id FROM Account WHERE Id IN :objects];
    
            for (Account account : accounts) {
                System.debug(LoggingLevel.FINE, 'Creating a workspace for Account(' + account.Id + ')');
                account.BoxFolder__c = boxToolkit.createFolderForRecordId(account.Id, null, true);
            }
    						
            boxToolkit.commitChanges();
            
            update accounts;
        }
    }
    0
    Comment actions Permalink
  • Box Product Support

    Hi Darrin,

    A lot of great info here and I think its the right start for my team to automatically create new Box folders when one of our custom objects creates a new record. I have a few questions about the code, myself and my team are new to Apex coding. Obviously a copy paste won't make this code work as I need to customize it to my custom object, instead of the Account object you have written.

     

    My custom object API name is Financing__c, where I see 'account' in the code below do I change it to 'Financing__c'?

     

    Where it says 'Id', do I need those changed to the record ID of the object?

     

    Where you have 'createFolderFuture' is the 'Future' part the Apex Annotation? (To execute the code asynchronously)

     

    My Box.com API name is not 'BoxFolder__c' as you have in the code, should this be changed to the API name of mine?

     

    Thank you for your help!

     

    Ryan

    0
    Comment actions Permalink
  • themayorpete

    Follow up question here.... I have the trigger working where the Box.com folder is being created on creation of a custom object.  However the Box.com Display Widget on each record still prompts the user to "Create a Folder" even though I can see the folder when I log into app.box.com.   When I click on create a folder it creates with _1 appended.

     

    Am I missing something to have the SFDC display wdiget just go to the proper folder?

     

    Thanks all for your help.

    Pete Dirksen

    @themayorpete 

     

    0
    Comment actions Permalink
  • pcgcurtis

    Thanks for this snippet Darrin.  Would you be willing to share the BoxFolderCreateBatch code too?  

    0
    Comment actions Permalink
  • JAS2018

    does anyone have Batch processing code

    0
    Comment actions Permalink
  • manish2

    Hi Ryan,

     

    Were you able to use the above code for auto create folders in your custom object? 

     

    Please guide me through this, I also need to auto create a box folder when a custom object record is created.

     

    Regards,

    Manish 

    0
    Comment actions Permalink
  • lamundsen

    I am also in need of an explanation for the overall solution to auto-create Box folders upon creation of a standard or custom Salesforce objects with that folder then appearing in the Box widget.

    0
    Comment actions Permalink
  • jessicapasley

    Has anyone been able to complete this and willing to share the trigger and class used?

     

    We too, are in need of apex code that will auto-create the Salesforce folder for box when the record is created linking the folder back to the record so that the user does not see the "create folder" button and assumes they must click it. 

     

    The ask seems simple. An apex trigger (and corresponding class) to create a box folder for the record upon creation of the Salesforce record with a link back to the record so that the user sees the created folder in the box embed widget. 

     

    Is the volume of users who need this so low that box won't provide for us?

     

    Thanks in Advance to anyone willing to help!

    0
    Comment actions Permalink
  • MortgageGroup

    Is there any update on this solution? I have the same requirement.

    0
    Comment actions Permalink
  • AdminGTE

    Hello,

     

    This is a very old thread but I just got my code to work and figured I can maybe save some people a lot of trouble. It autocreates a folder for new Project__c objects and gives the Owner EDITOR access.

     

    Create class with actual folder creation code. (Setup search Apex Classes).

    public without sharing class BoxHelper {
        @future (callout=true)
        public static void createProjectFolder(List ids) {
            List projects = [SELECT Id, Name, OwnerId FROM Project__c WHERE Id IN :ids];

            box.Toolkit boxToolkit = new box.Toolkit();
            
            for (Project__c project : projects) {
                String folderName = project.Name;
                if (GlobalHelper.inSandbox()) folderName = 'TestFolder_' + folderName;
                
                boxToolkit.createFolderForRecordId(project.Id, folderName, true);
                boxToolkit.createCollaborationOnRecord(project.OwnerId, project.Id, box.Toolkit.CollaborationType.EDITOR, false);
            }
            
            boxToolkit.commitChanges();
        }

    }

    Create class with helper function.

    public class GlobalHelper {
        public static Boolean inSandbox() {
            return [SELECT IsSandbox, InstanceName FROM Organization LIMIT 1].IsSandbox;
        }

    }

    Create trigger on your object (Setup search Apex Triggers).

    trigger BoxFolderCreateOnProjectCreate on Project__c (after insert) {
        BoxHelper.createProjectFolder(new List(Trigger.newMap.keySet()));
    }

     

    Of course replace Project__c with whatever object you're using.

    0
    Comment actions Permalink

Please sign in to leave a comment.