You need to sign in to do that
Don't have an account?
Vallabha
How to download attachments?
Hello All,
I looking for small help in regards to downloading attachments through SalesForce APIs.
I have few "Account" objects and some of these account objects have file attached with them. When I call "getUpdated' for "Account" object type, I get all the changed objects (for the given start and end time). However I am not able to retrieve the associated attachment.
I tried making use of the following call:
qr = binding.query("select Name, Body, ContentType from Attachment where ParentId='0018000000M34fSCCS");
Not sure if this is the correct way to download the file attachments. Does anyone knows what is the right way to download the file attachments using SalesForce API. Btw, I am using Partner WSDL for all my development.
Any help in this regard is greatly appreciated.
Thanks
-Vallabha
My code looks like this:
qr = binding.query("select Name, Body, ContentType from Attachment where ParentId='0018000000M34fSARC'");
for(int i=0; i < qr.getSize(); i++){
SObject acc = qr.getRecords(i);
org.apache.axis.message.MessageElement[] fileds = acc.get_any();
String fName = fileds[0].getValue();
String fileContents = fileds[1].getValue();
byte[] bName = new BASE64Decoder().decodeBuffer(fileContents);
fileContents = new String(bName);
String body = fileds[2].getValue();
// Lets try to write it to a file
DataOutputStream out = new DataOutputStream(new FileOutputStream(System.getenv("TEMP")+ "\\" + fName));
out.writeBytes(fileContents);
out.close();
}
Any help on how to handle GIF, .doc, other files would be of great help.
Thanks again
-Vallabha
String fileContents = fileds[1].getValue();
byte[] bName = new BASE64Decoder().decodeBuffer(fileContents);
fileContents = new String(bName);
This will certainly be corrupting your data, you just need.
String fileContents = fileds[1].getValue();
byte[] bName = new BASE64Decoder().decodeBuffer(fileContents);
OuputStream os = new FileOutputStream(System.getenv("TEMP")+ "\\" + fName);
os.write(bName);
os.Close();
I downloaded some pdf attachments and tried to decode that attachment body using some online decoder, but it failed, got an error - Invalid length for a Base-64 char array.
Any suggestion???