0

we have deployed a trigger on Tasks that sends an email when the users change the value of the status task field. Now we are using this email as a notification but we need to attach into the email some files stored in the Notes&Attachments task list. Our code is like:

trigger SendMail on Task (before update) {

for(Task t : trigger.new){
    if ((t.Status == 'Submitted')&&(t.SentMail__c == False)) {
       Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();
       Opportunity opp = [SELECT id, Name, Nombre_PR__c, Area_PR__c, Opportunity_Country__c, Email_PR__c, Local_reference__c FROM Opportunity WHERE Id = :t.WhatId];                    

        // create email content
        String nombre = opp.Name; 
        String propietario = opp.Nombre_PR__c;
        String area = opp.Area_PR__c;
        String pais = opp.Opportunity_Country__c;
        String correo = opp.Email_PR__c;
        String referencia = opp.Local_reference__c;
        String subject = 'Solicitud informe preventa tipo ';
        email.setSubject(subject);


        String line1 = 'El comercial/Desarrollo de negocio '+propietario+' ha solicitado un informe de preventa de tipo '+tipo+' perteneciente a la oportunidad "'+nombre+'".'+'\r\n'+'\r\n';
        String line2 = 'El email de '+propietario+' es '+correo+'. Se solicita fecha de entrega del informe el '+t.ActivityDate+'.'+'\r\n'+'\r\n';
        String line3 = 'La referencia local de la oportunidad es: '+referencia+'.'+'\r\n'+'\r\n'+'\r\n';
        String line4 = 'La descripción de la solicitud es: '+t.Demo_request_description__c+'.'+'\r\n';
        String body = line1 + line2 + line3 + line4; 
        email.setPlainTextBody(body);


        email.setToAddresses(new String[]{'[email protected]'});
        if(email != null){
            Messaging.sendEmail(new Messaging.singleEmailMessage[] {email});
            t.SentMail__c = True;               
          }

 }  
}

Thanks.

1 Answer 1

1

Merge following code in trigger:

   // fetch attachments for Opportunity
   List<Attachment> attList = [SELECT id, Name, body, ContentType FROM Attachment WHERE ParentId = : opp.id];

   // List of attachments handler
   Messaging.EmailFileAttachment[] efaList = new Messaging.EmailFileAttachment();
   for(Attachment att : attList)
   {
     // Create the email attachment
     Messaging.EmailFileAttachment efa = new Messaging.EmailFileAttachment();
     efa.setFileName(att.Name);
     efa.setBody(att.body);
     efa.setContentType(att.ContentType);
     efa.setInline(false);
     efaList.add(efa);
   }

    // Attach files to email instance
    email.setFileAttachments(efaList);

You can read in more detail here

3
  • Hello, thanks regal. It could be possible to select one specific attachment? based on the name or the date of the file?
    – Guillermo
    Commented Feb 24, 2014 at 14:03
  • Hello regal, when I test your code it throw the following error: Method does not exist or incorrect signature:[Messaging.EmailFileAttachment].add(Messaging.EmailFileAttachment) at line: efaList.add(efa); It could be due to efaList is not a list? How can I create a list of Messaging.EmailFileAttachment?? Thanks.
    – Guillermo
    Commented Feb 26, 2014 at 14:09
  • @Guillermo Yes. Type mistake!
    – Ashwani
    Commented Feb 26, 2014 at 16:17

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .