Salesforce/Box V2 Webhooks

Following 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. We'll cover the following steps:

 

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  # "Neil Reid"

# 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.

img

 

Save the Flow:

img

 

Notice how the API Name of the Webhook Flow consists of three parts. This is a
convention and it always has the format: Webhook _ Site Name _ URL Suffix

img

 

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:

img

 

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

Box has three setup areas. The links below can verify if you are on the correct area:

img img img

 

Log into Box > Dev Console > My Apps:

img

 

Carefully choose the app settings:

img img

 

Submit App for Authorization

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

img

 

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:

img

Click Authorize

img

 

Grant Access to your Proposals folder

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

img

Return back to your Box workspace (the blue area)

img

img

 

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.

img

img

 

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:

img

img

 

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.

img

img

 

Return to Salesforce.

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

img

 

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.

 

 

Getting started with Streamscript
Install from the Salesforce AppExchange
Package install link: /packaging/installPackage.apexp?p0=04tGA000005R8Ny