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.