0

Getting error on line -- "sendTo.add(acclist.owner.email);"

trigger emailOwner on Contact (after insert) {
        list<id> accId = new list<id>();
        
         List<Messaging.SingleEmailMessage> mails =  new List<Messaging.SingleEmailMessage>();
        
        for(Contact con :trigger.new){
            if(con.AccountId!=null){   list<account> acclist = [Select Owner.Email from Account where Id =:con.AccountId];
                
       Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
                
                List<String> sendTo = new List<String>();
         sendTo.add(acclist.owner.email);
          mail.setToAddresses(sendTo);
                
                mail.setReplyTo('b@gmail.com');
          mail.setSenderDisplayName('Conatct Created');
                
                mail.setSubject('new Contact Created to Account');
          String body = 'Dear';
          body += 'One new contact created.';
          mail.setHtmlBody(body);
        
         
          mails.add(mail);
            }
        }
         Messaging.sendEmail(mails);
    
    }
0

Throwing an alternative into the mix.

Apex is good, but can be used to over engineer a simple business need. In this case, from what I can see is that you are only looking for a "notification" email. Process Builder and an Email Alert using a Text Email Template should do the trick.

enter image description here enter image description here enter image description here

PS: Make sure your Email Template is marked as Available For Use.

0

You're trying to get a field from a List. This isn't allowed. You can only get the value from a single record. Also, please note that your code may fail in large data loads, as you're querying the account per record. In addition, you should use the user's Id, not an email address, to avoid hitting the daily bulk mail limits.

trigger emailOwner on Contact (after insert) {
    Map<Id, Id> accountOwnerIds = new Map<Id, Id>();
    for(Contact record: Trigger.new) {
        accountOwnerIds.put(record.AccountId, null);
    }
    for(Account record: [SELECT OwnerId FROM Account WHERE Id = :accountOwnerIds.keySet()]) {
        accountOwnerIds.put(record.Id, record.OwnerId);
    }
    List<Messaging.SingleEmailMessage> mails =  new List<Messaging.SingleEmailMessage>();
    
    for(Contact record: Trigger.new) {
        Id emailToId = accountOwnerIds.get(record.AccountId);
        if(emailToId != null) {
            Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
            mail.setTargetObjectId(emailToId);
            mail.setReplyTo('b@gmail.com');
            mail.setSenderDisplayName('Conatct Created');
            mail.setSubject('new Contact Created to Account');
            String body = 'Dear';
            body += 'One new contact created.';
            mail.setHtmlBody(body);
            mails.add(mail);
        }
    }
    if(!mails.isEmpty()) {
        Messaging.sendEmail(mails);
    }
}
5
  • With the above code. I'm still getting the error
    – Gxdev
    Mar 10 at 11:17
  • Illegal assignment from Id to User
    – Gxdev
    Mar 10 at 11:18
  • Variable does not exist: sendTo
    – Gxdev
    Mar 10 at 11:18
  • Method does not exist or incorrect signature: void setTreatTargetAsRecipient(Boolean) from the type Messaging.SingleEmailMessage
    – Gxdev
    Mar 10 at 11:19
  • @Gxdev Mistakes were made. They were actually pretty trivial. I've fixed them. setTreatTargetAsRecipient isn't needed, either (it should have been setTreatTargetObjectAsRecipient, but the default is true, so not needed here).
    – sfdcfox
    Mar 10 at 14:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.