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
ruchika Nayyarruchika Nayyar 

how to attached pdf file in salesforce ??

i have created
Upload any pdf file into Document first. Send an email as attachment to lead email Id. 
Email Subject : Welcome
Please find the attached PDF.
but i am getting mail but not attached file??

trigger SendEmail on Lead (after insert, after update) {
    
    for(Lead ld:trigger.new){
        {
             if(ld.Email!= null)
             {
    Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();
        
    
       email.setToAddresses(new String[]{'ruchikanayyar12@gmail.com'});        
        email.setReplyTo('ruchinayyar12@gmail.com');
        email.setSenderDisplayName('CRM Support');
        email.setSubject('Welcome');
        email.setBccSender(false);
        email.setUseSignature(false);
        email.setPlainTextBody('Please find the attached PDF');
                 email.saveAsActivity = false;
        email.setTargetObjectId(ld.OwnerId);
      
    
     List<Messaging.Emailfileattachment> fileAttachments = new List<Messaging.Emailfileattachment>();
        for (Attachment a : [select Name, Body, BodyLength from Attachment where ParentId = :ld.Id])
        {
        
        Messaging.Emailfileattachment efa = new Messaging.Emailfileattachment();
         efa.setContentType('application/pdf');
         efa.setFileName('pdffile.pdf');
           String body;
        
        body = '<html><h1 style=\"text-align:center;\">LeadInformation</h1><br/><br/><table align=\"center\"><tr><td>Name</td><td>' + ld.Name + '</td></tr><tr><td>Age</td><td>' + ld.Email + '</td></tr><tr><td>State</td><td>' + ld.Company + '</td></tr><tr><td>City</td><td>' + ld.Phone + '</td></tr></table></html>';
        System.debug('HTML is ' + body);
        
        efa.Body = Blob.toPDF(body);  

        fileAttachments.add(efa);
        }
        email.setFileAttachments(fileAttachments);
      //Send email
        Messaging.sendEmail(new Messaging.SingleEmailMessage[] { email });
       
}       
}
}

    

}
Abhishek_DEOAbhishek_DEO

I do not see where you are adding atatchment object "a" to Email object.If you are able to query relevant PDF from attachment object then yo just need to add below

Messaging.Emailfileattachment efa = new Messaging.Emailfileattachment();
efa.setFileName(a.Name);
efa.setBody(a.Body);
fileAttachments.add(efa);

If you are creating your PDF at run time then there is no need to query attachment object.

Thanks,

Abhishek

ruchika Nayyarruchika Nayyar
I have attached pdf file in static resource .but i will add attached document to salesforce so that i will use in my program.
thanks
ruchika
ruchika Nayyarruchika Nayyar
how to upload pdf file in document ???
where it is located?