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);
            String mostRecentError = boxToolkit.mostRecentError;
            if (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 {}

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}">

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 Auto-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'])}

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.