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:
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.
Streamscript > record
output
Save the Flow:
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
Create a Named Credential that provides your API authorization bearer token:
Create the Remote Site settings, one for the OAuth, another for file downloads:
Box has three setup areas. The links below can verify if you are on the correct area:
Log into Box > Dev Console > My Apps:
Carefully choose the app settings:
In the role of Developer, make the app privately available to the role of Administrator.
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
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.
Return back to your Box workspace (the blue area)
Your app will notify Salesforce about every file uploaded to the Proposals folder. Return to the Developer Console to configure the Box webhook.
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:
client_id
(paste your Client ID from Box)client_secret
(also paste your Client Secret from Box)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.
Getting started with Streamscript
Install from the Salesforce AppExchange
Package install link: /packaging/installPackage.apexp?p0=04tGA000005NDq5
Salesforce/Box V1 Webhooks |
Stripe Integration |
HTTP Callouts |