Sunday, 7 May 2017

Reading Attachment ID's from CSV,retrieve from Salesforce.com and Stores into the local folder using java

Hi

Here in the following example

We tried to explain how to read csv file which has salesforce.com attachment id's and then retrieving the related documents to local folder.
Use Case :
Developer gets one csv file with  attachment id's and those needs to be retrieved from salesforce.com and stores into local folder.

Please look into the comments you can uderstand the what is the code for reading id's from CSV file and qurying the same from Salesforce.com and stores into local folder.


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.Opportunity;
Import com.sforce.ws.ConnectionException;
Import com.sforce.ws.ConnectorConfig;
Import java.util.ArrayList;
Import java.util.List;
Import java.io. *;
Public class Retrieve3 {
Public static void main (String [] args) throws NumberFormatException, IOException, URISyntaxException {
/ * Reading CSV File which has attachement record Id's * /
List <String> documentIdSet = new ArrayList <String> ();
                        String csvFile = "D: /DemoFOlder/AttachmentIdSheet.csv";
String line = "";
                        String cvsSplitBy = ",";
                        Try (BufferedReader br = new BufferedReader (new FileReader (csvFile))) {
                            While ((line = br.readLine ())! = Null) {
                                // use comma as separator
                                String [] attachmentId = line.split (cvsSplitBy);

                                System.out.println ("attachmentId =" + attachmentId [0]);                            
documentIdSet.add (attachmentID [0]);
                            }
                            documentIdSet.remove (0);                        
                        } Catch (IOException e) {
                            e.printStackTrace ();
                        }
                             
/ * End of Reading CSV File which has attachement record Id's * /
/ * Start of Retrieving the Documents from Salesforce and put them into LocalFolder * /
                    // Create a new connectionconfig to your Salesforce Org
                     ConnectorConfig sfconfig = new ConnectorConfig ();
                     // Use your salesforce username
                     sfconfig.setUsername ( "test@test.com");                  
                     // Use your salesforce password with your security token look like:
                     sfconfig.setPassword ( "testXXXXXXX");                  
                     EnterpriseConnection partnercon = null;  
Try
// create a salesforce connection object with the credentials supplied in your connectionconfig
Partnercon = Connector.newConnection (sfconfig);  
String strIds = "";
For (String ids: documentIdSet) {
((StrIds.equals "")) {if
StrIds = "'" + ids + "'";
}
else {
StrIds + = ", '" + ids + "'";
}
}
For (String ids: documentIdSet) {
QueryResult describeGlobalResult = partnercon.query ("select Id, Name, Body from Attachment where Id =" "+ ids +" '");
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];                    
 String fpath123 = "D: // DemoFOlder // Attachments";

 File path = new File (fpath123);
 String mySubFolder = a.getId ();          
 File newDir = new File (path + File.separator);        
 Boolean success = newDir.mkdirs ();          
 FileOutputStream fos = new FileOutputStream (newDir + File.separator + a.getId () + "-" + a.getName ());
 Fos.write (a.getBody ());
 Fos.close ();
 }
 If (describeGlobalResult.isDone ()) {
Done = true;
 } Else {
  DescribeGlobalResult = partnercon.queryMore (describeGlobalResult.getQueryLocator ());
 }
}
}
                       
                     } Catch (ConnectionException e) {    
                        E.printStackTrace ();
                     }
/ * Endo of Retrieving the Documents from Salesforce and put them into LocalFolder * /
                }
}



3 comments:

  1. The information shared gave idea to Read Attachment ID's from CSV,retrieve from Salesforce.com and Stores into the local folder using java My sincere Thanks for sharing this post and please Continue to share This post
    Salesforce Training in Chennai

    ReplyDelete
  2. Great Article… I love to read your articles because your writing style is too good, its is very very helpful for all of us and I never get bored while reading your article because, they are becomes a more and more interesting from the starting lines until the end.
    SalesForce online training

    ReplyDelete
  3. Hai Author, Very Good informative blog post,
    Thanks

    ReplyDelete