+ Start a Discussion
Aidel BruckAidel Bruck 

not getting email sent by trigger

I posted this question yesterday, but didn't really get any solutions, so here goes again:
 have the following trigger that is obviously working (I checked the log), But no email is being sent out.
I check that the deliveribility is set to 'all emails' 
I checked the single email limits and they are fine, 
I piut the messaging.send() into an asycronous function
And still no luck

Please advice

This is the code

trigger NewFileAlert on ContentDocument (after insert) 
{

  List<Messaging.SingleEmailMessage> mails = 
  new List<Messaging.SingleEmailMessage>();
  
  for (ContentDocument newDoc : Trigger.new) 
  {
      
      Messaging.SingleEmailMessage mail = 
      new Messaging.SingleEmailMessage();
    
      
      List<String> sendTo = new List<String>();
      sendTo.add(allthepeople@everybody.com);
       ...
      mail.setToAddresses(sendTo);
    
      mail.setReplyTo('me@gmail.com');
      mail.setSenderDisplayName('Aidel Bruck');
   
      mail.setSubject('New Document Added');
      String body = 'Please note: A new document has been added/n  ';
      body += 'File name: '+ newdoc.title+'/n' ;
      body += 'Created By: '+ newdoc.createdbyid+ '/n';
      body += 'Created Date: '+ newdoc.CreatedDate+ '/n';
      body += 'link to file: '+ System.URL.getSalesforceBaseUrl().getHost()+newdoc.id;
      mail.setHtmlBody(body);

      mails.add(mail);
    }
  Messaging.sendEmail(mails);
v varaprasadv varaprasad
Hi Aidel,

Try this:
 
trigger NewFileAlert on ContentDocument (after insert) 
{

  List<Messaging.SingleEmailMessage> mails = 
  new List<Messaging.SingleEmailMessage>();
  
  for (ContentDocument newDoc : Trigger.new) 
  {
      
      Messaging.SingleEmailMessage mail = 
      new Messaging.SingleEmailMessage();
    
      
      List<String> sendTo = new List<String>();
      sendTo.add('allthepeople@everybody.com'); //add email in string format
       ...
      mail.setToAddresses(sendTo);
    
      mail.setReplyTo('me@gmail.com');
      mail.setSenderDisplayName('Aidel Bruck');
   
      mail.setSubject('New Document Added');
      String body = 'Please note: A new document has been added/n  ';
      body += 'File name: '+ newdoc.title+'/n' ;
      body += 'Created By: '+ newdoc.createdbyid+ '/n';
      body += 'Created Date: '+ newdoc.CreatedDate+ '/n';
      body += 'link to file: '+ System.URL.getSalesforceBaseUrl().getHost()+newdoc.id;
      mail.setHtmlBody(body);

      mails.add(mail);
    }
 
   Messaging.SendEmailResult[] results = Messaging.sendEmail(mails);
if (results[0].success) {
    System.debug('The email was sent successfully.');
} else {
    System.debug('The email failed to send: '
          + results[0].errors[0].message);
}
  
  
}

Hope this helps you!
If my answer helps resolve your query, please mark it as the 'Best Answer' & upvote it to benefit others.


Thanks
Varaprasad
Salesforce Freelance Consultant/Developer/Administrator
@For Salesforce Project Support: varaprasad4sfdc@gmail.com


Salesforce latest interview questions  :
https://www.youtube.com/channel/UCOcam_Hb4KjeBdYJlJWV_ZA?sub_confirmation=1


 
Raj VakatiRaj Vakati
try this
 
trigger NewFileAlert on ContentDocument (after insert) 
{
    // Step 0: Create a master list to hold the emails we'll send
    List<Messaging.SingleEmailMessage> mails = 
        new List<Messaging.SingleEmailMessage>();
    
    for (ContentDocument newDoc : Trigger.new) 
    {
        // Step 1: Create a new Email
        Messaging.SingleEmailMessage mail = 
            new Messaging.SingleEmailMessage();
        
        // Step 2: Set list of people who should get the email
        List<String> sendTo = new List<String>();
        sendTo.add('test@gmail.com');
        mail.setToAddresses(sendTo);
        
        // Step 3: Set who the email is sent from
        mail.setReplyTo('myaddress');
        mail.setSenderDisplayName('Aidel Bruck');
       
        // Step 4. Set email contents - you can use variables!
        mail.setSubject('New Document Added');
        String body = 'Please note: A new document has been added/n  ';
        body += 'File name: '+ newdoc.title+'/n' ;
        body += 'Created By: '+ newdoc.createdbyid+ '/n';
        body += 'Created Date: '+ newdoc.CreatedDate+ '/n';
        body += 'link to file: '+ System.URL.getSalesforceBaseUrl().getHost()+newdoc.id;
        mail.setHtmlBody(body);
       
        // Step 5. Add your email to the master list
       // mails.add(mail);
    }
    // Step 6: Send all emails in the master list
   // Messaging.sendEmail(mails);
}

 
Aidel BruckAidel Bruck
@v varparasad
Now I'm really stumped. 
The log says that the email was sent.