Salesforce auto-create folders?
We are migrating from a legacy system that connected to Salesforce and using Box primarily with Salesforce. With Box, you have to go to each Salesforce record and manually click "Create new folder" - is there some way to automate this? Anyone have some batch apex to accomplish this or something? We could speed up our migration if we could match the old folder structure to the new, since both systems will have the same folder names once the folders are created via Box for Salesforce.
-
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?
-
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; } }
-
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
-
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
-
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!
-
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.
サインインしてコメントを残してください。
コメント
11件のコメント