Sunday, 26 June 2016

Storing the attachments in Local Folder from Salesforce.com through JAVA using enterprise WSDL

Hi,

We have a requirement to store attachmentsv in a local folder from salesforce.com.For that we have to download Enterprise.wsdl and convert it into Enterprise.jar by using wsc-23.jar then add them to your java project . After this process you just need to create the following class.


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.soap.enterprise.sobject.Doctor__c;
import com.sforce.ws.ConnectionException;
import com.sforce.ws.ConnectorConfig;
import java.util.ArrayList;
import java.util.List;

public class ExportDocstoFFolder {

public static void main(String[] args) throws NumberFormatException, IOException, URISyntaxException {

  //Create a new connectionconfig to your Salesforce Org
   ConnectorConfig sfconfig = new ConnectorConfig();
   //Use your salesforce username = email
   sfconfig.setUsername("XXXXX");
   //Use your saleforce password with your security token look like:
   sfconfig.setPassword("XXXXsecurityToken");
   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");   
       boolean done = false;
       while(!done)
       {
        for (int k = 0; k < describeGlobalResult.getRecords().length; k++)
        {
       
           Attachment a = (Attachment)describeGlobalResult.getRecords()[k];
           File path = new File("F://SFDCAttachments//Balaji");
           String mySubFolder=a.getId();          
           File newDir = new File(path + File.separator);        
           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) {      
        e.printStackTrace();
   }
}
}


out put :
---------
After running java class:



"F" folder which is recieved attachements:



References:
To generate Jar files for Enterprise.wsdl
http://salesforce-walker.blogspot.in/2011/12/to-access-salesforce-data-from-java-we.html

https://thysmichels.com/2013/01/11/java-code-export-salesforce-attachements-to-dropbox/

No comments:

Post a Comment