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 * /
}
}
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 * /
}
}
Hai Author, Very Good informative blog post,
ReplyDeleteThanks
I am reading your post from the beginning, it was so interesting to read & I feel thanks to you for posting such a good blog, keep updates regularly..salesforce training
ReplyDeleteIt was awesome post keep on doing ...
ReplyDeletehttps://www.acte.in/angular-js-training-in-chennai
https://www.acte.in/angular-js-training-in-annanagar
https://www.acte.in/angular-js-training-in-omr
https://www.acte.in/angular-js-training-in-porur
https://www.acte.in/angular-js-training-in-tambaram
https://www.acte.in/angular-js-training-in-velachery