Recipes by Category

App Distribution (2) Bundle logic, interface and services for distribution. App Logic (37) The Apex programming language, workflow and formulas for logic. Collaboration (6) The Salesforce Chatter collaboration platform. Database (29) Data persistence, reporting and analytics. Integration (33) Web Service APIs and toolkits for integration. Security (9) Platform, application and data security. Tools (4) Force.com tooling User Interface (36) Visualforce MVC and metadata-drive user interfaces. Web Sites (12) Public web sites and apps with optional user registration and login.
Beta Feedback
Cookbook Home » Retrieving Email Attachments and Associating Them with Records

Retrieving Email Attachments and Associating Them with Records

Post by Developer Force  (2010-07-16)

Status: Certified
Level: novice

Problem

You want to retrieve an attached resume from the job applicant's incoming email and associate the attachment with the job application.

Solution

Verify that the incoming email message has an attached file. Create an attachment from the file and associate the attachment with the job application.

This example looks for a binary file attachment (for example, a PDF) that we can associate with a Job Application as a resume:

  
//Searches the email for binary attachments and
// associates them with the job application  
    
 
if (email.binaryAttachments != 
            null && email.binaryAttachments.size() > 0)
{
for (integer i = 0 ; i < email.binaryAttachments.size() ; i++)
{
Attachment a = new Attachment(ParentId = newJobApplication[0].Id,
Name = email.binaryAttachments[i].filename,
Body = email.binaryAttachments[i].body);
insert a;
  }
 }

Discussion

  • This example searches for binary attachments only. You would also need to consider other attachment types, such as text file attachments. You can define different actions depending on the type of attached file.
  • To view incoming email status and debug any processing errors, view the debug logs created when email services Apex code executes. See “Monitoring Debug Logs” in the Salesforce.com online help.
  • For a complete example containing code from all the email recipes, see Email Recipes—Complete Code Example.
Attachment validation is an essential part of processing emails. Malicious code can masquerade as a different file type.

Share

Recipe Activity - Please Log in to write a comment

 You can also add a content type to the attachment record.  Here are docs on the attachment object and here are docs on the InboundEmail.BinaryAttachment so you can see the fields.


by Reid_S_Carlberg  (2011-05-26)

X

Vote to Verify a Recipe

Verifying a recipe is a way to give feedback to others and broaden your own understanding of the capabilities on Force.com. When you verify a recipe, please make sure the code runs, and the functionality solves the articulated problem as expected.

Please make sure:
  • All the necessary pieces are mentioned
  • You have tested the recipe in practice
  • Have sent any suggestions for improvements to the author

Please Log in to verify a recipe

You have voted to verify this recipe.