My requirement is that I need to create new records / update existing records based on the data from an email attachment consisting of .csv file format that is parsed to the Email Service that I've created. But I'm unable to figure it how to do this process which might be helpful to upload data via an email. If there is any piece of code which meets my requirement pls pass it to me.
Thanks in advance
This is my email service code can you help me on this because when I parse email containing the csv file attachment to the particular sObject the debug logs show null stating that there is no attachment in my Email
global class SupplierCapacityDataImportEmail implements Messaging.InboundEmailHandler
{
global Messaging.InboundEmailResult handleInboundEmail(Messaging.InboundEmail email, Messaging.Inboundenvelope envelope)
{
Messaging.InboundEmailResult result = new Messaging.InboundEmailResult();
Messaging.InboundEmail.textAttachment[] tAttachments = email.TextAttachments;
System.debug('Text Attachement from the email.' + tAttachments);
Messaging.InboundEmail.BinaryAttachment[] bAttachments = email.BinaryAttachments;
System.debug('Binary Attachement from the email.' + bAttachments);
String csvbody = '';
If(bAttachments != Null)
{
For(Messaging.InboundEmail.binaryAttachment btt : bAttachments)
{
System.debug('This is a Binary Attachement from the email parsed.'+ btt);
If(btt.Filename.endsWith('.csv'))
{
System.debug('Binay Attachment' + btt);
csvbody = btt.body.toString();
System.debug('Battachment' + csvbody);
}
}
}
Else If(tAttachments != Null)
{
For(Messaging.InboundEmail.textAttachment ttt : tAttachments)
{
System.debug('This is a Text Attachment from the email parsed.' + ttt);
If(ttt.Filename.endsWith('.csv'))
{
System.debug('Text Attachment' + ttt);
csvbody = ttt.body;
System.debug('TXTAttachment' + csvbody);
}
}
}
return result;
}
}
Best Answer
My New code to read and insert data from a csv file using the salesforce email service