Salesforce/Box V2 Webhooks

Following Box V1 Webhooks, this is an enhanced Salesforce/Box integration using V2 Webhooks. In this recipe, we go beyond the upload notification, and use Box OAuth to sync entire files from Box directly into Salesforce using the ContentVersion object.

Recipe
Step 1 - Create a webhook receiver in Salesforce
Step 2 - Create a V2 Webhooks app in Box.com
Step 3 - Upload files to test the Box integration

Step 1 - Create a webhook receiver in Salesforce

Log into Salesforce and prepare the Flow to handle the webhook.

# Streamscript

# Read the Box webhook
$enterpriseId = 'YOUR-ENTERPRISE-ID-HERE'
$request = Json-Decode $Webhook.request
$type       = $request.trigger          # "FILE.UPLOADED"
$fileName   = $request.source.name      # "Proposal-01.png"
$fileId     = $request.source.id        # "1234567890"
$name       = $request.created_by.name  # "John Smith"

# Obtain bearer token
$url = 'callout:https_api_box_com/oauth2/token'
$headers = {}
$headers.Content-Type = 'application/x-www-form-urlencoded'
$payload = ''
$payload += '&client_id={!$Credential.Username}'
$payload += '&client_secret={!$Credential.Password}'
$payload += '&grant_type=client_credentials'
$payload += '&box_subject_type=enterprise'
$payload += '&box_subject_id=' + $enterpriseId
$response = Http-Post $url $headers $payload
$result = Json-Decode $response.body
$accessToken = $result.access_token

# Obtain download url
$url = `https://api.box.com/2.0/files/$fileId/content`
$headers   = {authorization: `Bearer $accessToken`}
$response  = Http-Get $url $headers  
$fileUrl   = $response.headers.location

# Retrieve b64 data
$base64  = Http-Get $fileUrl -base64

# Prepare new record
$ContentVersion = New-ContentVersion {
    Title = $fileName
    PathOnClient = $fileName
    VersionData = $base64.body
}

# Return as SObject 
return $ContentVersion

Add a flow element to save the Content Version.

Save the Flow:

Note that the Flow API Name follows this three-part naming convention: Webhook_SiteName_PathSuffix

Named Credential

Create a Named Credential that provides your API authorization bearer token:

Remote Site Settings x 2

Create the Remote Site settings, one for the OAuth, another for file downloads:

Step 2 - Create a V2 Webhooks app in Box.com

Box has three setup areas. Use the links below to ensure you are in the right area:

Log into Box > Dev Console > My Apps:

Carefully choose the app settings:

Submit App for Authorization

In the role of Developer, make the app privately available to the role of Administrator.

Approve the App

In the role of Administrator, approve the app so that it can be used by box users in the enterprise. Log into your Box workspace. From the left menu bar, select the Admin Console:

Click Authorize

Grant Access to your Proposals folder

Your app's service account must have specific access to your enterprise artefacts (files, folders, etc). For this recipe, grant the app full access to the 'Proposals' folder.

Return back to your Box workspace (the blue area)

Configure a FILE.UPLOADED listener

Your app will notify Salesforce about every file uploaded to the Proposals folder. Return to the Developer Console to configure the Box webhook.

Step 3 - Upload files to test the Box integration

Before testing, enter the three parts of your Box credential in Salesforce.

In Box, open the Dev Console and navigate to your app:

Log into Salesforce using a second browser tab.

Update your Box Named Credential:

Update your Script with your Enterprise ID:

# Streamscript
# Read the Box webhook
$enterpriseId = 'YOUR-ENTERPRISE-ID-HERE'

Return to your Box workspace. From the blue navigation menu, click Files then the Proposals folder.

Drag and drop a test file into the folder to upload it.

Return to Salesforce.

Go to the Files tab. You will find a copy of the file that was uploaded to Box.

The recipe is complete! You now have a realtime integration, it automatically syncs files from Box into Salesforce. It works without user interaction and needs no periodic token refresh.