Inbound Email Handler to Save attachments Salesforce | Techila
1141
single,single-post,postid-1141,single-format-standard,ajax_updown_fade,page_not_loaded,,qode_grid_1300,footer_responsive_adv,hide_top_bar_on_mobile_header,qode-content-sidebar-responsive,qode-theme-ver-9.4.2,bridge,wpb-js-composer js-comp-ver-4.12,vc_responsive
 

Inbound Email Handler to Save attachments Salesforce

blog-31

Inbound Email Handler to Save attachments Salesforce

This post will be concentrating on handling the attachments in the incoming email.  An InboundEmail object stores binary attachments in an InboundEmail. BinaryAttachment object.


The following are properties for InboundEmail.BinaryAttachment:

·        body
The body of the attachment.

·        fileName
The name of the attached file.

·        headers
Any header values associated with the attachment. Examples of header names include Content-TypeContent-Transfer-Encoding, and Content-ID.

·        mimeTypeSubType
The primary and sub MIME-type.

In the Post Inbound Email Handler in Salesforce we have created a contact using the headers and from name and from email attributes of incoming email. Now I am extending the same example and would save the attachment of the incoming email in the attachments object below the contact.

Apex Code:

global class myHandler implements Messaging.InboundEmailHandler {

global Messaging.InboundEmailResult handleInboundEmail(Messaging.InboundEmail email,
Messaging.InboundEnvelope envelope) {

Messaging.InboundEmailResult result = new Messaging.InboundEmailresult();

Contact con= new contact();
con.FirstName = email.fromname.substring(0,email.fromname.indexOf(‘ ‘));
con.LastName = email.fromname.substring(email.fromname.indexOf(‘ ‘));
con.Email = envelope.fromAddress;
insert con;

if (email.binaryAttachments != null && email.binaryAttachments.size() > 0) {
for (integer i = 0 ; i < email.binaryAttachments.size() ; i++) {
Attachment attachment = new Attachment();
attachment.ParentId = con.Id;
attachment.Name = email.binaryAttachments[i].filename;
attachment.Body = email.binaryAttachments[i].body;
insert attachment;
}
}

return result;

}

}

Courtesy– http://www.worldofsalesforce.com/2016/07/inbound-email-handler-to-save-attachment-salesforce.html

Author: techila

No Comments

Post A Comment