Java Code: Export Salesforce Attachements to Dropbox

I wrote a class the exports Salesforce Attachements to be exported to your local Machine. I put the exported attachement files in a dropbox folder which is automatically the shared with others in the organization.

Note: make sure the export directory is shared on Dropbox.

package com.thys.michels.sfdc.doc;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.URISyntaxException;

import com.sforce.soap.enterprise.Connector;
import com.sforce.soap.enterprise.EnterpriseConnection;
import com.sforce.soap.enterprise.QueryResult;
import com.sforce.soap.enterprise.sobject.Attachment;
import com.sforce.ws.ConnectionException;
import com.sforce.ws.ConnectorConfig;

public class SFDCDoc {

	/**
	 * @param args
	 * @throws NumberFormatException 
	 * @throws IOException 
	 * @throws URISyntaxException 
	 * @throws DOMException 
	 */
	public static void main(String[] args) throws NumberFormatException, IOException, URISyntaxException {
		// TODO Auto-generated method stub
		

	   //Create a new connectionconfig to your Salesforce Org
	    ConnectorConfig sfconfig = new ConnectorConfig();
	    //Use your salesforce username = email
	    sfconfig.setUsername("yourusername");
	    //Use your saleforce password with your security token look like: passwordjeIzBAQKkR6FBW8bw5HbVkkkk
	    sfconfig.setPassword("passwordwithsecuritytoken");
	 
	    EnterpriseConnection partnercon = null;
	    
	    try {
	    	 
	        // create a salesforce connection object with the credentials supplied in your connectionconfig
	        partnercon = Connector.newConnection(sfconfig);
	        QueryResult describeGlobalResult = partnercon.query("select Id, Name, Body from Attachment where ContentType='application/pdf'");
	        System.out.println(describeGlobalResult.getRecords().length);
	        boolean done = false;
	     
	        while(!done)
	        {
	        	for (int k = 0; k < describeGlobalResult.getRecords().length; k++)
	        	{
	        		
	        	    Attachment a = (Attachment)describeGlobalResult.getRecords()[k];
	        	    File path = new File("//Users//tmichels//Dropbox//SFDCAttachments");
	        	    String mySubFolder = a.getId();
	        	    File newDir = new File(path + File.separator + mySubFolder);
	        	    System.out.println(newDir);
	        	    boolean success = newDir.mkdirs();
	        	    FileOutputStream fos = new FileOutputStream(newDir + File.separator+ a.getName());
	        	    fos.write(a.getBody());
	        	    fos.close();
	        	}
	        	if (describeGlobalResult.isDone()) {
	                done = true;
	             } else {
	            	 describeGlobalResult = partnercon.queryMore(describeGlobalResult.getQueryLocator());
	             }
	        }
	        
	        
	    } catch (ConnectionException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
	    }
	}
}
Advertisements

One Comment on “Java Code: Export Salesforce Attachements to Dropbox

  1. Hello,

    Thanks a lot for you code, we are veryt interested by that.

    i just have a question, i’m not sure what it does?

    We are looking to automatically upload to dropbox attachment in emails-2-cases in Salesforce.
    We have a folder in Dropbox for each of our contact and we want each attachment sent to us by these contacts to be uploaded automatically in their Dropbox folder

    Thanks a lot if you can give us more information abou that

    best
    olivier

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: