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
Cloud-LingCloud-Ling 

sending outbound email

Greetings,

 

Any idea why i can't send Email? my code below

 

Class Code:

 

public class EmailMessage
{  
   public static void SendEmail()
   {
       System.debug('Sending Email...');
       Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
       String[] toAdd = new String[]{'m*******','l*******com','t******.com'};
       mail.setToAddresses(toAdd);
       mail.setReplyTo('ke****.com');
       mail.setSenderDisplayName('Salesforce Notification');
       mail.setSubject('Duplicating Account Name');
       mail.setBccSender(false);
       mail.setUseSignature(false);
       mail.setPlainTextBody('Some user attempts to duplicate you Account Name.');
       List<Messaging.SendEmailResult> results = Messaging.sendEmail(new Messaging.Email[] { mail });
       System.debug('Email Sent: '+results.get(0).isSuccess() );
    }
}

 Trigger Code:

 

trigger ErrorDuplicateOutput on Account (before insert, before update)
{
  List<Account> accList =new List<Account>();
  Set<String> b = new Set<String>();
  accList = [Select Name FROM Account];
  
  for (Account acc : accList)
  {
   b.add(acc.Name);
  }
  
  for(integer x = 0; x < Trigger.new.size(); x++)
  {
      if(b.contains(Trigger.new[x].name))
      {
          Trigger.new[x].addError('Error:Account name already exist!');
          EmailMessage.SendEmail();
      }
  }
}

 

Any ideas are welcome, thank you!

S-L

 

 

Best Answer chosen by Admin (Salesforce Developers) 
mikefmikef

I tested your EmailMessage class and method an that worked from the system log console.

 

I think the email is not firing because you have the email logic after the error logic. The addError() method kills all processes.

 

I would change you code to something more like this. Because your original code will break when you have more then 1000 accounts, and it's not very efficient for bulk inserts.

 

 

trigger AccountTrigger on Account(before insert){

   Set<String> accNames = new Set<String>();
   Map<String, Account> accMap = new Map<String, Account>();

   for(Account acc : trigger.new){
      accNames.add(acc.Name);
   } 

   for(Account acc : [select Id, Name from Account where Name in : accNames]){
      accMap.put(acc.Name, acc);
   }

   for(Account acc : trigger.new){
      if(accMap.containsKey(acc.Name)){
         EmailMessage.SendEmail();
         acc.addError('Error:Account name already exist!');
      }
   }
}

 Let me know if the order of the email message send solves your issue.

 

All Answers

mikefmikef

I tested your EmailMessage class and method an that worked from the system log console.

 

I think the email is not firing because you have the email logic after the error logic. The addError() method kills all processes.

 

I would change you code to something more like this. Because your original code will break when you have more then 1000 accounts, and it's not very efficient for bulk inserts.

 

 

trigger AccountTrigger on Account(before insert){

   Set<String> accNames = new Set<String>();
   Map<String, Account> accMap = new Map<String, Account>();

   for(Account acc : trigger.new){
      accNames.add(acc.Name);
   } 

   for(Account acc : [select Id, Name from Account where Name in : accNames]){
      accMap.put(acc.Name, acc);
   }

   for(Account acc : trigger.new){
      if(accMap.containsKey(acc.Name)){
         EmailMessage.SendEmail();
         acc.addError('Error:Account name already exist!');
      }
   }
}

 Let me know if the order of the email message send solves your issue.

 

This was selected as the best answer
sfdeveloper9sfdeveloper9

I have a very much similar scenario. I tried to send the email before the addError() method. but it doesn't work for me.

my code looks like this

 

dcu.sendEmail(body, toAddresses, 'subject', 'test@gmail.com');                    
asset.addError('Error:');

 

Cloud-ling/Mike , can you let me know if that worked for you.

 

Thanks,

PK