I have a requirement to save attachment received from email in Salesforce object using Email To Salesforce. I tried using the below code but attachment are not getting saved, when I checked using the system.debug() it shows null value for both binaryattachment and testattachment.
Is there any setting that I need to check? I tested this with both text file and image file as attachment.
public class EmailProcess implements Messaging.InboundEmailHandler
{
public Messaging.InboundEmailResult handleInboundEmail(Messaging.InboundEmail email, Messaging.Inboundenvelope envelope)
{
system.debug('email.textAttachments:' + email.textAttachments); //null
system.debug('email.binaryAttachments:' + email.binaryAttachments); //null
List<Attachment> attList = new List<Attachment>();
//Assigned parentId
system.debug('parentId' + parentId); //Shows record Id
for (Messaging.Inboundemail.BinaryAttachment file : email.binaryAttachments)
{
Attachment attachment = new Attachment();
attachment.Name = file .fileName;
attachment.Body = file .body;
// some hardcoded or id extracted from email reference
attachment.ParentId = parentId;
attList .add(attachment);
}
if(attList .size()>0)
{
insert attList;
}
}
}
Best Answer
When you add (or edit) the email handler class via the email service definition you get a choice of what sort of attachments should be passed to it as shown below. Check that setting.