Sunday, June 21, 2015

Box and Salesforce Integration

In this blog, I am going to explain how to integrate and Salesforce using Apex and VF in order to upload document to Box and to view files present in Box in VF page.

First of all, Create a Box Application

Create Box content application which will help us to upload files directly from Salesforce to  After creating app, copy client_id and client_secret values and specify your VF page URL as redirect_uri in Oauth 2 parameters section.

Now create another application and select Box view application. This will help us to view files in VF page. View API allow to convert files in HTML format for easy display in web pages. Copy view API Key from General information section.

Now Create a custom setting in Salesforce to store client_id,client_secret, access token, refresh token and View API key.

Now Create Remote site setting in order to do call out to

Now create a VF page "boxAuthPage" and apex class "boxAuthPageController". Code is provided in end of this blog. Please scroll down to copy code and paste in your VF and apex class.

Snapshot of VF page:

Store client_id, client_secret, view api key in custom setting record. While creating custom setting record, I have specified "Box_sunil02kumar' as name for custom setting. If you want to use other name, then replace "Box_sunil02kumar' in apex class with your custom setting name.

Open VF page and click on Authorize Box button, system will redirect you to login page where you need to enter you box login credential and then a click grant access to box button.

All folder present in box will be listed in Folders drop down. When you select any particular folder, all files present in that folder will appear in page block table. When you click on "View" link, file will open on right side of page.

If you have box account, then use below url to test the functionality. Create folders in your box account and upload pdf and doc files in it. If you upload files with already existing file name in Box folder, then you will get error.

I have built this page for demo. So every time you refresh this page, you need to again authorize it by clicking on Authorize box button. Actually I am not storing access token or refresh token or any user credentials and also scope of these variables are same as that of VF page. Also client id, client secret and access tokens are not displayed in demo page but if you follow all steps mentioned above including custom setting and remote site setting, then you will get complete UI.

Important information which will help you to understand apex class:
  • Uploading data into uses content type as multipart/form-data.
  • You can not send a file to server which is expecting a binary. So you need to encode the base64 in binary supported format.
  • In order to view document, first you need to send request to get temporary download URL by sending file id to Temporary download URL is returned in response Location header. This URL can be used to download file.
  • Once you get temporary download URL, sent request to to get temporary document id. In HTTPRequest, you need to pass temporary download URL in body.
  • Now this document id which you get in response can be used to generate session id for file which will be used to view file in iframe in VF page. When you do call out to get session id, pass temporary document id in request body.
  • If file is processed by and is ready to view, then in response you will get session id. If your request is accepted but file is not yet converted to HTML format then in response header, you will time duration after which file will be ready. This information is present in 'retry' header in response.
  • Once you get file session id from, then you use it view file in iframe in your VF page by using  URL:'+fileSessionId+'/view

  • You can upload any type of file to box folder.
  • Before uploading any file, please select folder in which you want to upload file.
  • As per my knowledge, View API doesn't support jpg and png images. 
  • You can easily view any pdf or word file from Box in SFDC using view api.
  • You can also specify download URL. Please refer Box content view API for this.
  • In apex, code is specified to get file download URL in FindFileDownloadUrl method in apex class.
This blog will help you to basic understanding of how to achieve integration between Box and SFDC by using apex code. After going through this code, you can easily modify the code as per your requirements.

Code for VF and apex class:


  1. We have mastered the integration of box and salesforce! You can now include functionality that allows an individual to associate any folder in with a corresponding record in Click on the link for more information, and we will be glad to assist you.

  2. This comment has been removed by the author.

  3. Why we used symcsk02 as a state?

    1. Hi Dilip,

      It is just a place holder. You can pass any value as per your wish. What ever you specify here will be returned in callback URL.

  4. How did you create a Box View app? the option was not included that's why we are having trouble viewing the docs in Visualforce. Do you have other source for this? Thanks!


  5. I like this query i was stuck in a problem but this query help me a lot. thank thanks

    Google App Integration Chennai

  6. Thanks for offering a great information on Salesforce Integration Services. It is very good information to read.

  7. Hi you done a good job, This is very valuable information.
    I got struck at viewing files from box in iframe i'm not able to see the selected files .

  8. How do i get Authcode if i am calling box api from class. i dont want to use VF page. is there any wahy to do this

    1. Did you ever answer this query? I am trying to do the same...

  9. How did you create a Box View app? the option was not included that's why we are having trouble viewing the docs in Visualforce. Do you have other source for this? Thanks!

  10. It’s too informative blog and I am getting conglomerations of info’s. Thanks for sharing; I would like to see your updates regularly so keep blogging.
    salesforce form builder and integration

  11. Positive site, where did u come up with the information on this posting? I'm pleased I discovered it though, ill be checking back soon to find out what additional posts you include.
    box file

  12. HI, it's everything working fine but we have getting the "read timeout error" when we are viewing file. Please give me any suggestions.

  13. my emailid:

  14. We need to create a lightning app which should contain a button that uploads files from our PC to Box App directly.
    Can you please help on this?

    1. This is quite urgent .can anyone help on this?

    2. You can use Content Uploader Box UI elements for this. Please refer below URL:

  15. Thanks for your suggestion. We started by installing box sdk package.
    Any idea on how can we get the file Id?

  16. once you click click on Authorize Box button, system will redirect you to inside box application instead of putting user name and pwd in login page where you can see file folders . How can we achieve ?

  17. This is very helpful. Thanks.

  18. I do agree with all the ideas you have presented in your post. They’re really convincing and will certainly work. Still, the posts are very short for newbies.
    Document Management Software Dubai
    Document Management Software
    Document Management Software UAE
    Best Document Management Software