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 com.sforce.soap.enterprise.Connector;
import com.sforce.soap.enterprise.EnterpriseConnection;
import com.sforce.soap.enterprise.QueryResult;
import com.sforce.soap.enterprise.sobject.Attachment;

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
	    //Use your saleforce password with your security token look like: passwordjeIzBAQKkR6FBW8bw5HbVkkkk
	    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'");
	        boolean done = false;
	        	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);
	        	    boolean success = newDir.mkdirs();
	        	    FileOutputStream fos = new FileOutputStream(newDir + File.separator+ a.getName());
	        	if (describeGlobalResult.isDone()) {
	                done = true;
	             } else {
	            	 describeGlobalResult = partnercon.queryMore(describeGlobalResult.getQueryLocator());
	    } catch (ConnectionException e) {
        // TODO Auto-generated catch block

One thought 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


Leave a Reply

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

You are commenting using your 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 )

Connecting to %s