function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
adamproadampro 

Can't get attachment from Email Service

Hi,

 

I'm creating an email service, but I can't seem to get it to include the attachments. I've found plenty of example code on this yet somehow it's not working. It will create a task like I want it to, but it never associates the attachments.

 

Task[] newTask = new Task[0];

newTask.add( new Task (
                Subject = subject,
                OwnerId = matchUpp.OwnerId,
                Type = 'Email',
                ActivityDate = system.now().date(),
                Status = 'Completed',
                Description = BodyText,
                Priority = 'Normal',
                WhatId = matchOpp.Id
                )
);
                
insert newTask;

if ( email.binaryAttachments != null && email.binaryAttachments.size() > 0 ) {
    for (Messaging.Inboundemail.Binaryattachment bAttachment : email.binaryAttachments ) {
        /*
        Attachment attachment = new Attachment();
        attachment.Name = bAttachment.fileName;
        attachment.Body = bAttachment.body;
        attachment.ParentId = newTask[0].Id;
            	
        insert attachment;
        */
        Attachment a = new Attachment(ParentId = newTask[0].Id, 
                                          Name = bAttachment.filename, 
                                          Body = bAttachment.body);
            		insert a;
            	}
        }
            
if (email.textAttachments != null && email.textAttachments.size() > 0) {
            	for (Messaging.Inboundemail.Textattachment tAttachment : email.textAttachments ) {

    /*
    Attachment attachment = new Attachment();
    attachment.Name = tAttachment.fileName;
    attachment.Body = blob.valueOf(tAttachment.Body);
    attachment.ParentId = newTask[0].Id;
            	
    insert attachment;
    */
            		
    Attachment a = new Attachment(ParentId = newTask[0].Id, 
                                          Name = tAttachment.filename, 
                                          Body = blob.valueOf(tAttachment.body));
    insert a;
    }
}

 

For whatever reason, this won't seem to work. I've tried attaching .txt, .jpg, and .png files and none have shown up. Does anyone see something I'm doing wrong?

 

Thanks

Best Answer chosen by Admin (Salesforce Developers) 
Ritesh AswaneyRitesh Aswaney

Setup > Develop > Email Services

When you actually create your new Email Service, there is a drop-down setting for selecting the Attachment Type. This has caught me out in the past - I would select Both in this and re-try.

Text attachments only  Binary attachments only  All 

Accept AttachmentsNone 

All Answers

Ritesh AswaneyRitesh Aswaney

Setup > Develop > Email Services

When you actually create your new Email Service, there is a drop-down setting for selecting the Attachment Type. This has caught me out in the past - I would select Both in this and re-try.

Text attachments only  Binary attachments only  All 

Accept AttachmentsNone 
This was selected as the best answer
adamproadampro

That's exactly what it was. Thank you so much!