+ Start a Discussion
Bernd NawrathBernd Nawrath 

Email trigger when new account is added

Hello dear community, I need some help programming an email trigger for my production, I already have a PDF as a component and I want this PDF to be sent automatically when a new account is added. Any advices on how to make a code to work for this purpose?

Thanks in advance :) 
11c-at-sfdc11c-at-sfdc
Are you looking for something like this?
====
trigger emailOnNewAccounts on Account(after insert) 
{
    Set<Id> accountIds = new Set<Id>();
    for(Account ac : Trigger.new)
    {
              accounIds.add(ac.Id);
    }

for(Contact mContact : [Select Id, Email from Contact where Id in : accountIds])
   {
   if(mContact.Email != null)
     {
        Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
        String[] toAddresses = new String[]{mContact.Email};
        mail.setToAddresses(toAddresses);
        //other mail settings like mail.setReplyTo, mail.setSubject, etc.
        //get file attachments
        List<Messaging.Emailfileattachment> fileEmailAttachments = new List<Messaging.Emailfileattachment>();
        for (Attachment attach : [select Name, Body, BodyLength from Attachment where ParentId = :mContact.Id])
        {
          Messaging.Emailfileattachment aff = new Messaging.Emailfileattachment();
          aff.setFileName(attach.Name);
          aff.setBody(attach.Body);
          fileEmailAttachments.add(aff);
        }
        mail.setFileAttachments(fileEmailAttachments);
        //email
        Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
    }       
  }
}
====