5

For instance, I am writing some code to send an email using a after insert trigger. I am wanting to get the email address of the lead owner I am currently iterating over, but am running into an error.

public static void sendAdrEmailNotification(List<Lead> leadList) {

     ...

     for (Lead lead : leadList) {
          ...
          singleMail.setToAddresses(lead.Owner.Email); //Email cannot be resolved
          ...
     }
}

How can I get the email address of the lead owner in the Apex code? I know I can use a SOQL query but I obviously do not want to do this in a for loop so I'm not sure where to go from here.

  • Lead owner can be a USer or a Queue, so you have to work accordingly. – Pranay Jaiswal Dec 3 '18 at 18:17
  • @PranayJaiswal What's strange is SELECT Id, Owner.Email FROM Lead works in a SOQL query so I'm not sure why it doesn't work in Apex. I know formulas use Lead.Owner:User.Email syntax, I just don't know what the syntax is in Apex – Josh Dec 3 '18 at 18:23
  • You can create a formula field on lead, and then use that in APex code. Formula field values can be accessed in after trigger – Pranay Jaiswal Dec 3 '18 at 18:29
4

You don't need soql to do that, you can directly use setTargetObjectId

setTargetObjectId(targetObjectId)

optional . The ID of the contact, lead, or user to which the email will be sent. The ID you specify sets the context and ensures that merge fields in the template contain the correct data.Required if using a template,

so your code will be

for (Lead lead : leadList) {
          ...
           if(lead.OwnerId.getSobjectType() ==  User.SObjectType)
          singleMail.setTargetObjectId(lead.OwnerId); 
          ...
     }

The advantage of using setTargetObjectId is, you can send unlimited emails to the user. If manually specify email id then you are restricted to only 5000 emails a day limit.

src: https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_classes_email_outbound_single.htm

  • Thanks @Pranay this resolved it. Also I think you meant to say my code should be singleMail.setTargetObjectId(lead.OwnerId); in your code block – Josh Dec 3 '18 at 18:35
  • 1
    @Josh yeah I meant that, :P I forgot to change , My bad – Pranay Jaiswal Dec 3 '18 at 18:36
  • @Prany, I may have to open a new question, but I'm attempting to use an email template here, using setWhatId which should fill out the template, but you cannot use both setWhatId and setTargetObjectId together or you get an error. Where can I go from here? – Josh Dec 3 '18 at 20:48
  • You dont have to use setWhatID if you use setTargetObjectId check the link I mentioned for setTargetId – Pranay Jaiswal Dec 3 '18 at 20:57
  • 1
    Why not use renderEmailTemplate method to get merged email data for lead? developer.salesforce.com/docs/atlas.en-us.apexcode.meta/… – Pranay Jaiswal Dec 3 '18 at 21:06

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.