2

I am using the Email Services in SFDC, and I am not able to get the attachments I attach to the emails to be uploaded to the new record that is being created. I built my APEX class off of the sample one in Force.com Cookbook. I have modified the code to work with my custom object, and that is working fine, the custom object record is being created just fine. The only thing I updated in the section of code that checks and attaches the attachments is the name for my custom object. Everything else is the same. Again, the custom object record is being created properly and consistently. It's just the attachments that are not showing up. I double checked to make sure the emails and attachments were under the file limit size, and they all are.

Here is my code:

// Creates new announcement object  

Announcements__c[] myAnnouncement = new Announcements__c[0];

global Messaging.InboundEmailResult handleInboundEmail(
    Messaging.InboundEmail email,
    Messaging.InboundEnvelope envelope) {
    Messaging.InboundEmailResult result =
        new Messaging.InboundEmailresult();

    // Records the senders email address

    String SendersEmail = envelope.fromAddress;

    // Retrieves and parses content from the email.

    // Splits each line by the terminating newline character and
    // looks for the position of announcement name, point of contact, and body copy 

    String[] emailBody = email.plainTextBody.split('\n', 0);
    String AnnouncementName = emailBody[0].substring(19);
    String PointOfContact = emailBody[2].substring(18);
    String AnnouncementBody = emailBody[4].substring(6);

    // Creates a new announcement from the information
    // retrieved from the inbound email  

    try {
        myAnnouncement.add(new Announcements__c(
            Senders_Email__c = SendersEmail,
            Name = AnnouncementName,
            Point_of_Contact__c = PointOfContact,
            Body__c = AnnouncementBody));

        insert myAnnouncement;
    } catch (System.DmlException e) {
        System.debug('ERROR: Not able to create announcement' + e);
    }

    // Searches the email for binary attachments and
    // associates them with the new announcement record

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

    return result;
}

Any help would be appreciated.

Thank you.

  • Can you let us know if any of the answers helped you to figure out a solution? Then please accept the answer to close the question or tell us if it still doesn't work. – Uwe Heim Oct 30 '14 at 7:44
  • @Uew Heim - It was the attchments setting. I overlooked it. Thanks for the response. – KDB Oct 30 '14 at 14:33
2

This may sound stupid but what are your settings for attachments in the email service itself?

There doesn't seem to be an issue with the code.

enter image description here

  • Ugh. Rookie mistake. This was it. Thanks cricketlang! – KDB Oct 30 '14 at 14:31
1

Actually there seems to be a kind of odd behavior for some content-types in salesforce which may result in symptoms you are describing.

Here you find some backgrounds and a workaround pattern: Email attachment not received in EmailService handler

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.