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
premaprema 

how do i send emails whenever a pdf is uploaded on notes and attachments of particular object

 when an application submitted it genrerates pdf and saves in notes and attachments now i want to sena an email with pdf attachment to the key contact email.
i tried with below code 
rigger PDFAttachTrigger on Attachment (after insert) {
List<Messaging.SingleEmailMessage> mails = new List<Messaging.SingleEmailMessage>();
       
    for(Attachment att : Trigger.new)
    {
        if(att.Name.endsWith('_DTAC_Application__c.pdf') && att.parentId.getSObjectType().getDescribe().getName().equals('DTAC_Application__c'))
        {
            Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
            String[] toAddresses = new String[] {'prema.salesforce@gmail.com'};
            mail.setToAddresses(toAddresses);
            mail.setSubject('DTAC Application Submission Confirmation');
            mail.setSaveAsActivity(false);
            mail.setPlainTextBody('Dear');
           
            Messaging.Emailfileattachment efa = new Messaging.Emailfileattachment();
            efa.setFileName(att.Name);
            efa.setBody(att.Body);
           
            mail.setFileAttachments(new Messaging.EmailFileAttachment[] {efa});
            mails.add(mail);
           
           
}
    }
   
Messaging.sendEmail(mails);
}
it is sending email with a pdf attachment to the to address which addressed in  String[] toAddresses = new String[] {'prema.salesforce@gmail.com'};
but i want to use email id from email template  and other fields too

How can i achieve this? 
 
PriyaPriya (Salesforce Developers) 
Hey,

You have to query all the email field from the object. For example if you want to send the email to some user, then use the beloq querry 

User u = [Select id, email, name from user where alias = Somename Limit 1];
.
.
.
String[] toAddresses = new String[] {'prema.salesforce@gmail.com',u.email};

Thanks!