+ Start a Discussion
Ramesh KallooriRamesh Kalloori 

How to send the new Contact details in email as pdf attachment to the user?

Best Answer chosen by Ramesh Kalloori
SFDC_DevloperSFDC_Devloper
Hi Ramesh,

Trigger:
trigger sendEmailToContact on Contact (after insert, after update) 
{    
    List<Messaging.SingleEmailMessage> mails = new List<Messaging.SingleEmailMessage>();
    for(Contact c : trigger.new)
    {   
        Messaging.EmailFileAttachment attach = new Messaging.EmailFileAttachment();
        attach.setContentType('application/pdf');
        attach.setFileName('Contact.pdf');
        
        String body;
        
        body = '<html><h1 style=\"text-align:center;\">Contact Information</h1><br/><br/><table align=\"center\"><tr><td>Contact Name</td><td>' + c.Name + '</td></tr><tr><td>MobilePhone</td><td>' + c.MobilePhone + '</td></tr><tr><td>Email</td><td>' + c.Email + '</td></tr></table></html>';
        System.debug('HTML is ' + body);
        
        attach.Body = Blob.toPDF(body);
                
        Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
        mail.setToAddresses(new String[] { c.Email });
        mail.setSubject('PDF Generation');
        mail.setHtmlBody('PFA');
        mail.setFileAttachments(new Messaging.EmailFileAttachment[] { attach });     
        
        mails.add(mail);  
    }
    if(!mails.isEmpty())
    {
        Messaging.SendEmail(mails);
    }
}
 Create contact with Name,Phone and Email fields.Once you Created contact you will receive one email with PDF attachement .

Output:
User-added image

[If it helps, mark it as "Best Answer"]


Thanks,
Rockzz