Setting Up S-Docs Box Integration
S-Docs integrates seamlessly with cloud storage solution Box, meaning that you can generate documents securely on the Salesforce platform and then store them with Box -- all without ever leaving Salesforce. This article will provide you with step-by-step instructions for configuring the S-Docs Box integration. For the purposes of this guide, we'll assume that you have already installed the Box for Salesforce application from the AppExchange. If not, you can download it here.
Create a New Apex Class
To begin, create a new Apex class. In the setup menu, type "Apex classes" into the Quick Find bar, then click Apex classes from the options that drop down, and click New.
Paste in the Apex class listed below, then click Save.
public class SDBoxUploadController { public PageReference uploadToBox() { String attachmentId = ApexPages.currentPage().getParameters().get('attId'); String sdocId = ApexPages.currentPage().getParameters().get('sdocId'); SDOC__SDoc__c sdoc = [SELECT SDOC__GD_Status_Text__c FROM SDOC__SDoc__c WHERE Id=:sdocId FOR UPDATE]; Attachment att = [SELECT Name, Body, ParentId FROM Attachment WHERE Id=:attachmentId FOR UPDATE]; Box.Toolkit boxToolkit = new Box.Toolkit(); boxToolkit.createFileFromAttachment(att, null, null, null); boxToolkit.commitChanges(); String mostRecentError = boxToolkit.mostRecentError; if (!Test.isRunningTest() && mostRecentError != null && mostRecentError != '') { throw new SDException(mostRecentError + ' (some uploads fail because a file with this filename already exists on Box)'); } sdoc.SDOC__GD_Status_Text__c = 'Linked to Box'; update sdoc; return new PageReference('/' + ApexPages.currentPage().getParameters().get('redirectId')); } public class SDException extends Exception {} }
If you require test coverage for the SDBoxUploadController class, you can use the following apex class:
@isTest private class SDBoxUploadTest { @isTest private static void testSDBoxUpload() { SDOC__SDoc__c sdoc = new SDOC__SDoc__c(); insert sdoc; Attachment att = new Attachment( Name='test.txt', Body=Blob.valueOf('test'), ParentId=sdoc.Id ); insert att; Test.setCurrentPage(Page.SDOC__SDCreate3); ApexPages.currentPage().getParameters().put('sdocId', sdoc.Id); ApexPages.currentPage().getParameters().put('attId', att.Id); SDBoxUploadController sdbuc = new SDBoxUploadController(); sdbuc.uploadToBox(); } }
Create A New Visualforce Page
Next, create a new Visualforce page. In the setup menu, type "Visualforce" into the Quick Find bar, click Visualforce Pages from the options that drop down, then click New.
Enter the following values for your Visualforce page, then click Save.
Label: SDBoxUpload
Name: SDBoxUpload
Visualforce Markup:
<apex:page controller="SDBoxUploadController" action="{!uploadToBox}"> </apex:page>
Add The "Box Enabled" Field To Your S-Docs Templates
In order to enable the Box storage option for your S-Docs templates, you need to add the Box Enabled field to the template record layout. Navigate to the Object Manager in the Setup menu and find the SDoc Template object.
Navigate to the Page Layouts tab and click Edit.
Navigate to the Fields tab in the top menu, then find the Box Enabled field. Drag it down onto the SDoc Template Detail layout, and click Save.
To enable Box storage for a template, check the Box Enabled box when creating the template, or edit the template record detail page to check the box for templates that you've already created.
You'll also need to enable the Create Salesforce Attachment and link to record feature. This checkbox is found under the Document Options tab of the template editor, and is enabled by default.
Configure Box Settings
Next, you'll need to configure your admin and user settings for the Box application for Salesforce. Navigate to the Box application and configure your settings under the Box Settings tab. Once your settings are configured, click Sync Now.
You'll also need to enable Box for the objects that you're using with S-Docs. Click here for detailed instructions on installing and configuring Box for Salesforce.
Uploading Documents To Box
Once you've configured S-Docs and Box, navigate to an object record that you've enabled for both apps. Click the S-Docs button and select a box-enabled template. Then, click Next Step to generate the document.
You'll now see an Upload Selected Documents to Box button. Click the button to upload your generated documents to Box. Once complete, you can then email, view, download, or print your documents like normal.
When you view the record that you generated this document from, your document will be viewable within the Box iframe:
And you'll now be able to view and access your documents from box.
Automatically Upload Documents To Box
Although the default S-Docs & Box integration allows you to choose whether or not a document should be uploaded to Box, you can also modify this configuration so that documents are automatically uploaded to Box whenever they are generated. To do so, simply modify your S-Docs button URL and add the autoBox='1' parameter to the end. For example, an S-Docs Opportunity button with the automatic Box upload feature would look like this:
{!URLFOR('/apex/SDOC__SDCreate1', null,[id=Opportunity.Id, Object='Opportunity', autoBox='1'])}
Box Error Handling
By default, errors that occur during document upload to Box will throw an exception and roll back any DML occurring in the Box package.
To prevent this behavior, you can opt to instead handle error messages at the UI layer, which will not roll back DML occurring in the Box package. This requires adding a parameter to your S-Docs button and creating a new Apex class.
1. Add New S-Docs Button Parameter
From the Setup menu, navigate to your object in the Object Manager. Then, navigate to the Buttons, Links, and Actions tab, find your S-Docs button, click the dropdown arrow, and click Edit.
Add the following parameter to your S-Docs button URL:
boxUploadClass='SDBoxUploadControllerNew'
Click Save.
2. Add New Apex Class
Navigate back to the Setup menu. Type "Apex" into the Quick Find bar, click Apex Classes in the dropdown menu, then click New.
Paste in the following Apex class.
Name: SDBoxUploadControllerNew
Class:
global class SDBoxUploadControllerNew implements Callable { String uploadToBox(String attId, String sdocId) { SDoc__c sdoc = [SELECT GD_Status_Text__c FROM SDoc__c WHERE Id=:sdocId FOR UPDATE]; Attachment att = [SELECT Name, Body, ParentId FROM Attachment WHERE Id=:attId FOR UPDATE]; Box.Toolkit boxToolkit = new Box.Toolkit(); boxToolkit.createFileFromAttachment(att, null, null, null); boxToolkit.commitChanges(); String mostRecentError = boxToolkit.mostRecentError; if (mostRecentError != null && mostRecentError != '') { sdoc.GD_Status_Text__c = 'Error When Linking to Box'; return (mostRecentError + ' (some uploads fail because a file with this filename already exists on Box)'); } sdoc.GD_Status_Text__c = 'Linked to Box'; update sdoc; return ''; } // Dispatch actual methods public Object call(String action, Map<String, Object> args) { switch on action { when 'uploadToBox' { return this.uploadToBox((String)args.get('attId'), (String)args.get('sdocId')); } when else { throw new ExtensionMalformedCallException('Method "uploadToBox" not implemented'); } } } public class ExtensionMalformedCallException extends Exception {} }
Once you click Save, you're all set!
Additional Resources
If you want to update your Box integration folder configurations, you can update the createFileFromAttachment function in the controller SDBoxUploadController mentioned above. For more information, please consult Box's Salesforce Toolkit documentation.