Five Stars

[resolved] Upload Attachment into Salesforce

Hi. Im trying to upload set of files into Salesforce using tSalesforceOutput. I understand I need to use file where following columns are required

ParentId - the Salesforce ID of the parent record.
Name - the name of the attachment file, such as myattachment.jpg.
Body - the absolute path to the attachment on your local drive
I upload the file and check the salesforce record, there is indeed the file loaded with the name specified in the sheet but when I try to open it.. it is just nothing (check the second picture attached). On the first picture you can see the job in Talend, last picture is the file specifying the attachment path and details. Can anyone help please? Many thanks
1 ACCEPTED SOLUTION

Accepted Solutions
One Star

Re: [resolved] Upload Attachment into Salesforce

Hi,
Yes, you need to code a little for such a case.
Here is an example of what the tJavaRow should look like to accomplish the file conversion:
output_row.Parent_ID = input_row.Parent_ID; // Related object's Id
output_row.Name = input_row.Name; // The document's name such as xxxxx.docx or yyyyyy.xlsx or anything else
output_row.ContentType = input_row.ContentType; // Object's name such as Account, Task and so on
output_row.Body = null; // The place to put the conversion result
// In this case, I iterate over the input directory using a tFileList
File file = new File((String)globalMap.get("tFileList_1_CURRENT_FILEPATH"));
try {
// Read file
documentInFile = new FileInputStream(file);
byte documentData[] = new byte;
documentInFile.read(documentData);
// Convert bytes array to Base64 string to fill Body field
output_row.Body = new String(Base64.encodeBase64(documentData));
System.out.println("+++ File converted "+(String)globalMap.get("tFileList_1_CURRENT_FILEPATH"));
} catch (FileNotFoundException e) {
output_row.conversionCode = "FILE_NOT_FOUND";
output_row.conversionMessage = "File not found "+(String)globalMap.get("tFileList_1_CURRENT_FILEPATH");
System.out.println("*** File not found "+(String)globalMap.get("tFileList_1_CURRENT_FILEPATH")+"\n"+e);
} catch (IOException ioe) {
output_row.conversionCode = "CONVERSION_ERROR";
output_row.conversionMessage = "Error converting file "+(String)globalMap.get("tFileList_1_CURRENT_FILEPATH");
System.out.println("*** Error converting file "+(String)globalMap.get("tFileList_1_CURRENT_FILEPATH")+"\n"+ioe);
} finally {
try {
// Close and delete file after it has been successfully converted
if (documentInFile != null) {
documentInFile.close();
file.delete();
}
} catch (IOException e) {
output_row.conversionCode = "DELETE_ERROR";
output_row.conversionMessage = "Error deleting file "+(String)globalMap.get("tFileList_1_CURRENT_FILEPATH");
System.out.println("*** Error deleting file "+(String)globalMap.get("tFileList_1_CURRENT_FILEPATH")+"\n"+e);
e.printStackTrace();
}
}

In the Advanced setting of tJavaRow, you must import required librairies:
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
// Import de Apache Commons Codec for Base64 conversion of documents
import org.apache.commons.codec.binary.Base64;

And a minimalistic job should look like this one:

Regards,
TRF
6 REPLIES
One Star

Re: [resolved] Upload Attachment into Salesforce

Hi,
To load attchments to Salesforce, you need to convert the file to Base64 using a tJavaRow component.
The conversion result must be used to populates the Body field.
Google is your friend to find examples for the conversion routine.
Regards,
TRF
Five Stars

Re: [resolved] Upload Attachment into Salesforce

Thanks. Unfortunately Im no coder and my friend Google returns some code stuff only.. could you guide me through please? from what I understand I need to use tJavaRow and write something like this below?
output_row.ParentId = input_row.ParentId;
output_row.Name = input_row.Name;
output_row.Body = input_row.Body;
String s = new Base64.encodeBase64(input_row.Body);
            output_row.Body = s;
It doesnt work however, Im getting error "Base64 cannot be resolved to a type" and I guess I need to import some library as well? dont know how but thats what I see on forums
One Star

Re: [resolved] Upload Attachment into Salesforce

Hi,
Yes, you need to code a little for such a case.
Here is an example of what the tJavaRow should look like to accomplish the file conversion:
output_row.Parent_ID = input_row.Parent_ID; // Related object's Id
output_row.Name = input_row.Name; // The document's name such as xxxxx.docx or yyyyyy.xlsx or anything else
output_row.ContentType = input_row.ContentType; // Object's name such as Account, Task and so on
output_row.Body = null; // The place to put the conversion result
// In this case, I iterate over the input directory using a tFileList
File file = new File((String)globalMap.get("tFileList_1_CURRENT_FILEPATH"));
try {
// Read file
documentInFile = new FileInputStream(file);
byte documentData[] = new byte;
documentInFile.read(documentData);
// Convert bytes array to Base64 string to fill Body field
output_row.Body = new String(Base64.encodeBase64(documentData));
System.out.println("+++ File converted "+(String)globalMap.get("tFileList_1_CURRENT_FILEPATH"));
} catch (FileNotFoundException e) {
output_row.conversionCode = "FILE_NOT_FOUND";
output_row.conversionMessage = "File not found "+(String)globalMap.get("tFileList_1_CURRENT_FILEPATH");
System.out.println("*** File not found "+(String)globalMap.get("tFileList_1_CURRENT_FILEPATH")+"\n"+e);
} catch (IOException ioe) {
output_row.conversionCode = "CONVERSION_ERROR";
output_row.conversionMessage = "Error converting file "+(String)globalMap.get("tFileList_1_CURRENT_FILEPATH");
System.out.println("*** Error converting file "+(String)globalMap.get("tFileList_1_CURRENT_FILEPATH")+"\n"+ioe);
} finally {
try {
// Close and delete file after it has been successfully converted
if (documentInFile != null) {
documentInFile.close();
file.delete();
}
} catch (IOException e) {
output_row.conversionCode = "DELETE_ERROR";
output_row.conversionMessage = "Error deleting file "+(String)globalMap.get("tFileList_1_CURRENT_FILEPATH");
System.out.println("*** Error deleting file "+(String)globalMap.get("tFileList_1_CURRENT_FILEPATH")+"\n"+e);
e.printStackTrace();
}
}

In the Advanced setting of tJavaRow, you must import required librairies:
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
// Import de Apache Commons Codec for Base64 conversion of documents
import org.apache.commons.codec.binary.Base64;

And a minimalistic job should look like this one:

Regards,
TRF
Five Stars

Re: [resolved] Upload Attachment into Salesforce

Ok thanks, resolved by using Apex Data Loader though. The solution above works however it is not very well handled by Talend, technically you cannot load files into salesforce without having dev skills, so it is not very useful to business or less technical skilled users. At least if there was a component doing this base64 encoding itself would be very helpful. Thanks anyway
One Star

Re: [resolved] Upload Attachment into Salesforce

Hi,
Talend is a developer tool requiring technical skill, not an end user tool.
Data Loader is helpfull but cannot do what Talend can.
Regards,
TRF
Six Stars JR
Six Stars

Re: [resolved] Upload Attachment into Salesforce

You can actually achieve this without diving deep into Java programming. Please evaluate the tFileInputRaw component. Connect this via a tMap to tSalesforceOutput and as the value for the Body field in the Salesforce schema, use a Base64 encode method like described above (e.g. "new org.apache.commons.codec.binary.Base64.encodeBase64("row1.content")"). But please be aware that TOS 6.2.1 had an issue with uploading attachments to Salesforce in general. It may only be possible in one of the upcoming versions (> 6.2.1).