+ Start a Discussion
Anto HotelbedsAnto Hotelbeds 

query attachments on outbound email

Hi all,

In my org, when a customer service agent is working on a case and hits the send email button to use the standard send email functionality, I hav a requirement that is:
- Capture the body of the email and create with it a Case Comment related to the parent case of the email. Also, get all the attachments related to the email and relate them to the case. Finally, delete the email message.

I am able to do everything except for the attachments part. I have created a trigger on emailMessage that creates the case comment without trouble, but another part of my trigger is created to get the attachments, but it always returns 0 elements in the list. This is the trigger on email message related to the attachment part(just getting the attachment of the first email to check if it works):
 
trigger EmailMessageAfterOperationTrigger on EmailMessage (after delete, after insert, after undelete, after update) {

if (trigger.isInsert){
    	Attachment[] attList = [select id, name, body from Attachment where ParentId = :Trigger.new[0].Id];
    	Attachment[] insertAttList = new Attachment[]{};

        system.debug('Has the email attachments?:' + Trigger.new[0].HasAttachment);
    	system.debug('list of attachments realted to the email message: ' + attList);

    	for(Attachment a: attList){
               Attachment att = new Attachment(name = a.name, body = a.body, parentid = Trigger.new[0].id);
               insertAttList.add(att);
        }
       if(insertAttList.size() > 0){
            insert insertAttList;
       }
    	
    }
This is the debug log the system shows:
 
09:45:00.579 (1579755455)|USER_DEBUG|[146]|DEBUG|Has the email attachments?:true
09:45:00.579 (1579760206)|SYSTEM_METHOD_EXIT|[146]|System.debug(ANY)
09:45:00.579 (1579778024)|SYSTEM_METHOD_ENTRY|[147]|String.valueOf(Object)
09:45:00.579 (1579802168)|SYSTEM_METHOD_EXIT|[147]|String.valueOf(Object)
09:45:00.579 (1579814171)|SYSTEM_METHOD_ENTRY|[147]|System.debug(ANY)
09:45:00.579 (1579819259)|USER_DEBUG|[147]|DEBUG|list of attachments realted to the email message: ()

I have also created a trigger on attachment before insert but it doesnt get fired when sending an email with attachment:
 
trigger AttachmentBeforeOperationTrigger on Attachment (before delete, before insert, before update) {

	if (Trigger.isInsert || Trigger.isUpdate){
		for (Attachment a: Trigger.new){
			system.debug('el attachment: ' + a);
		}
	}

}

I need to get this attachments related to the email message, can please anyone help me?

Thanks,

Antonio





 
NagaNaga (Salesforce Developers) 
Hi Antonio,

Please see the code below

List<Messaging.EmailFileAttachment> attach1 = new List<Messaging.EmailFileAttachment>();

List<Document> doc = [Select id, DeveloperName, ContentType, type, Body from Document where Name = 'Document Name]; 

 for(Document dc: doc)         

  {               

Messaging.EmailFileAttachment attach = new Messaging.EmailFileAttachment(); 

attach.setContentType(dc.contentType);               

attach.setFileName(dc.developerName+'.'+dc.type);               

attach.setInline(false);               

attach.Body = dc.Body;               

attach1.add(attach);           

}  
In the SignleEmailMessage object set this:
mail.setFileAttachments(attach1);

If you want some more information on adding attachments to outbound emails 

Please follow the below link

http://www.salesforce.com/docs/developer/pages/Content/pages_email_sending_attachments.htm

Best Regards
Naga Kiran
Anto HotelbedsAnto Hotelbeds
Hi,

Thanks for your help but I am not trying to send the email via Apex. I want to use the standard Salesforce functionality  (EmailAuthor) and get the attachments.

 
Richa Sharma 45Richa Sharma 45
Hi Anto,
Did you find any solution for it?
James John 18James John 18
Hello, 

Much obliged for your assistance yet I am making an effort not to send the email through Apex. I need to utilize the standard Salesforce usefulness (EmailAuthor) and get the connections. https://signaturesbyml.com/