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
Pranav ChitransPranav Chitrans 

Send Email Attachment whenever new lead is created

I want to write the trigger in such a way that when ever new lead is getting created it automatically sens email and also attch one pdf attchment to it... I written some logic.. which is working fine when ever new lead is created it send the email but wont be able to write the logic for attchment... I am confused.. from where i have to get the attchment.. because while inserting recors.. there is no such filed of attchment... it ony comes when i save the data"Notes and attachment"/...
trigger SendAttchmnt on Lead (before Insert,before update,after Insert) 
{
 if(trigger.isInsert || trigger.isUpdate)
 {
  for(Lead l : trigger.New)
  {  
   if(l.email == null)
   {
   l.addError('Email Req');
   }
  }
 }
 
 if(trigger.isInsert && trigger.isAfter)
 {
  List<Messaging.SingleEmailMessage> mailList = new List<Messaging.SingleEmailMessage>();
  for(Lead ld : trigger.New)
  {
   if(ld.Email != null)
   {   
    Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();
    List<String> toAddresses = new List<String>();
    toAddresses.add(ld.Email);
    email.setToAddresses(toAddresses);
    email.setSubject('Lead generated!');
    String message = '<html><body>Hi ' +ld.name + ', <br/>Lead Created '+'You are now customer of our group.<br/><br/> <b>Regards,</b><br/> Admin </body>  </html>';
    email.setHtmlBody(message);
    mailList.add(email);          
   //Messaging.sendEmail(new Messaging.SingleEmailMessage[]{email});
   }   
  }
  Messaging.sendEmail(mailList);   
  
  List<Messaging.Emailfileattachment> fileAttachments = new List<Messaging.Emailfileattachment>(); 
 }
}

​i had wriiten half the code of attachment but I got confused..so i left there,,..a dn one mor thing which i face the problem in line 26 in the body tag Hi, after that it shows Null (Hi Null,) why the name is not getting in email....
Amit K AAmit K A
What kind of attachment you want toattach?

one way is that you can create a VF page ,add all the content on it and mark it as renderas=pdf.
take this page into blob and attact it to mail.

Thanks
 
Pranav ChitransPranav Chitrans
pdf.... I just want when ever lead is trigger the email should be sent along with attachment by using trigger.... how to attch file using trigger
Amit K AAmit K A
Can you please clear with your requirements ? what exactly do you want to do?
 do you need to attach a pdf file in email which is attached on lead or  ???
Pranav ChitransPranav Chitrans
Amit K A
yes.....