Salesforce Integration with Dropbox?

  • Dropbox access allows the user to store and access the files and share with anyone on the fly. Once you have saved files on Dropbox, you can access them from anywhere from your account with the help of Internet. 
  • It also allows us to access to the authorized customer’s files via our Mobile Apps / Web application through REST API.

Prerequisites for Dropbox Integration :

  • Create a Dropbox Account .
  • Create an app of Dropbox, give required access to that app.
  • Copy access token and secret key (we would use it later on in our apex code)
  • For more details refer this link .

Once we’ve set up the Dropbox app, it’s time to jump into coding. Here is a quick overview of what we are doing in our code.

  • Our first step is to perform a query to get accounts information, then convert it to a blob, the set a csv file name
  • Then, Add an endpoint for dropbox integration.
  • If you need a downloadable link for your attachment, then you can collect the path_lower attribute from the response that came from the first API hit and use it accordingly.
  • Please refer to the Apex class below for more information.
public class dropBox {

    public dropBox(){
        String strCSVFileHeader  = 'Unique Id, Name, Website'+'\n';
        String strCSVFileValue;
        For(Account acc: [SELECT name, id, Website from account limit 5]){
            if(strCSVFileValue==null || strCSVFileValue=='')
                strCSVFileValue = acc.Id+','+ acc.Name+','+acc.Website+'\n';
            else
                strCSVFileValue = strCSVFileValue +acc.Id+',' + acc.Name+','+acc.Website+'\n';
            
        }
        strCSVFileHeader = strCSVFileHeader+strCSVFileValue;  
        blob csvBlob = Blob.valueOf(strCSVFileHeader);
        string csvname= 'AccountFile' + system.now() + '.csv';
        String tokenuri = 'https://content.dropboxapi.com/2/files/upload';
        String attachmentName = csvname;
        
        try{
            // to add file into dropbox
            String inputPath = '{"path":"/SF Folder/'+attachmentName+'","mode": "add","autorename": true,"mute": false}';
            HttpRequest request = new HttpRequest();
            request.setEndpoint(tokenuri);
            request.setMethod('POST');
            request.setHeader('Authorization','Bearer <Token generated from Dropbox App>');
            request.setHeader('Content-Type', 'application/octet-stream');
            request.setHeader('Dropbox-API-Arg',inputPath);
            request.setBodyAsBlob(csvBlob);        
            request.setTimeout(12000);
            Http hpPut = new Http();
            HttpResponse responseFile;
            String jsonFileRes;
            if(!test.isRunningTest()){
                responseFile = hpPut.send(request);
                jsonFileRes = responseFile.getBody();
            }else{
                jsonFileRes = '{"url": "http://www.google.com", "path":"dropbox/salesforce"}';
            }
            String filePath;
            Map<String, Object> jsonMap = (Map<String, Object>)JSON.deserializeUntyped(jsonFileRes);
            if(jsonMap.get('path_lower')!= null){
                filePath = (String)jsonMap.get('path_lower');
            }
            // Step done to make the dropbox link downloadable (if needed by business user)
            String imageuri = 'https://api.dropboxapi.com/2/sharing/create_shared_link_with_settings';
            HttpRequest requestForUrl = new HttpRequest();
            requestForUrl.setEndpoint(imageuri);
            requestForUrl.setMethod('POST');
            requestForUrl.setHeader('Authorization','Bearer <Token generated from Dropbox App>');
            requestForUrl.setHeader('Content-Type','application/json');        
            inputPath = '{"path":"'+filePath+'" ,"settings": {"requested_visibility": "public"}}';
            requestForUrl.setBody(inputPath);        
            requestForUrl.setTimeout(12000);
            Http hp = new Http();
            HttpResponse response;
            String jsonRes;
            if(!test.isRunningTest()){
                response = hp.send(requestForUrl);
                jsonRes = response.getBody();
            }else{
                jsonRes = '{"url": "http://www.google.com", "path":"dropbox/salesforce"}';
            }
            System.debug('response body -->> '+jsonRes);
            
            String jsonURL;
            Map<String, Object> jsonURLMap = (Map<String, Object>)JSON.deserializeUntyped(jsonRes);
            if(jsonURLMap.get('url')!= null){
                jsonURL = (String)jsonURLMap.get('url');
                jsonURL = jsonURL.replace('?dl=0', '?dl=1');
            }
        }catch(Exception ex){
            system.debug('ex --> 1 ' + ex.getLineNumber());
            system.debug('ex --> 2 ' + ex.getStackTraceString());
            system.debug('ex --> 3 ' + ex);
        }
    }
}

Please execute the following code in an anonymous window to call the above.

dropBox dp = new dropBox();

voilà!

Leave a Reply

Your email address will not be published. Required fields are marked *