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
Soubhagya Ranjan 2Soubhagya Ranjan 2 

getiing error in trigger REQUIRED_FIELD_MISSING, Missing target address (target, to, cc, bcc):

I want a trigger in account object when a new record is inserted with same phon enumber then it will automatically mail to account owner that account has been ctreated .

but i am facing issue REQUIRED_FIELD_MISSING, Missing target address (target, to, cc, bcc): 

below is my code 


trigger EmailAfterInserttest on Account(after Insert,after update) {
    Messaging.reserveSingleEmailCapacity(trigger.size);
    List<Messaging.SingleEmailMessage> emails = new List<Messaging.SingleEmailMessage>();
    Set<Id> accId = new Set<Id>();
    List<ID>ownerids=new List<ID>();
    List<String> sendTo = new List<String>();
  List<User>users=[select name,id,email from user where id in:ownerids];
  for(User u:users){
      sendTo.add(u.Email);
    }
  
    for(Account a : Trigger.new)
        {
            accId.add(a.Id);
            ownerids.add(a.ownerid) ;
        }
        
        
    for (Account acct : Trigger.new) {
    List<Account> accList = [Select ID, owner.Id,owner.email, Name ,fax, phone From Account Where Id in :accId And phone=:acct.phone];
        if(acclist.size()>0)
        {
         Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();
        email.setToAddresses(sendTo);
        email.setSubject('Inserted Account Alert');
        email.setPlainTextBody('This message is to alert you that the account named ' + acct.Name + ' has been inserted.');
        emails.add(email);
        Messaging.sendEmail(emails);
        }
    }
   
}
 
bob_buzzardbob_buzzard
You need to execute the code that generates the sendTo after you have figured out the owner ids, otherwise it is empty:
 
    List<ID>ownerids=new List<ID>();
  
    for(Account a : Trigger.new)
        {
            accId.add(a.Id);
            ownerids.add(a.ownerid) ;
        }
        
    List<String> sendTo = new List<String>();
    List<User>users=[select name,id,email from user where id in:ownerids];
    for(User u:users){
      sendTo.add(u.Email);
    }

Also, your trigger isn't bulkified - you have a query inside a for loop is one example. This means that if you bulk insert accounts it stands a good chance of failiing. You can read up on bulkification at:

https://developer.salesforce.com/page/Best_Practice%3A_Bulkify_Your_Code
sfdcMonkey.comsfdcMonkey.com
hi rajan 
you got this error message because  your sendTo list is empty use below code 
* first fill ownerids list and then you can query this users.
 
trigger EmailAfterInserttest on Account(after Insert,after update) {
    Messaging.reserveSingleEmailCapacity(trigger.size);
    List<Messaging.SingleEmailMessage> emails = new List<Messaging.SingleEmailMessage>();
    Set<Id> accId = new Set<Id>();
    List<ID>ownerids=new List<ID>();
    List<String> sendTo = new List<String>();
   for(Account a : Trigger.new){
            accId.add(a.Id);
            ownerids.add(a.ownerid) ;
        }
  
  List<User>users=[select name,id,email from user where id in:ownerids];
  
  
  for(User u:users){
      sendTo.add(u.Email);
    }
  
   
        
    for (Account acct : Trigger.new) {
    List<Account> accList = [Select ID, owner.Id,owner.email, Name ,fax, phone From Account Where Id in :accId And phone=:acct.phone];
        if(acclist.size()>0){
         Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();
        system.debug('sendTo'+sendTo);
		email.setToAddresses(sendTo);
        email.setSubject('Inserted Account Alert');
        email.setPlainTextBody('This message is to alert you that the account named ' + acct.Name + ' has been inserted.');
        emails.add(email);
        Messaging.sendEmail(emails);
        }
    }
   
}

i hope it helps you 
thanks let me inform if it helps you