Is it possible to access the email of a Lead's owner directly?












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.










share|improve this question






















  • Lead owner can be a USer or a Queue, so you have to work accordingly.
    – Pranay Jaiswal
    Dec 3 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 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 at 18:29


















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.










share|improve this question






















  • Lead owner can be a USer or a Queue, so you have to work accordingly.
    – Pranay Jaiswal
    Dec 3 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 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 at 18:29
















5












5








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.










share|improve this question













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.







trigger email leads






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Dec 3 at 18:15









Josh

1357




1357












  • Lead owner can be a USer or a Queue, so you have to work accordingly.
    – Pranay Jaiswal
    Dec 3 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 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 at 18:29




















  • Lead owner can be a USer or a Queue, so you have to work accordingly.
    – Pranay Jaiswal
    Dec 3 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 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 at 18:29


















Lead owner can be a USer or a Queue, so you have to work accordingly.
– Pranay Jaiswal
Dec 3 at 18:17




Lead owner can be a USer or a Queue, so you have to work accordingly.
– Pranay Jaiswal
Dec 3 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 at 18:23






@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 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 at 18:29






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 at 18:29












1 Answer
1






active

oldest

votes


















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






share|improve this answer























  • 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 at 18:35








  • 1




    @Josh yeah I meant that, :P I forgot to change , My bad
    – Pranay Jaiswal
    Dec 3 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 at 20:48










  • You dont have to use setWhatID if you use setTargetObjectId check the link I mentioned for setTargetId
    – Pranay Jaiswal
    Dec 3 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 at 21:06













Your Answer








StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "459"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsalesforce.stackexchange.com%2fquestions%2f241262%2fis-it-possible-to-access-the-email-of-a-leads-owner-directly%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









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






share|improve this answer























  • 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 at 18:35








  • 1




    @Josh yeah I meant that, :P I forgot to change , My bad
    – Pranay Jaiswal
    Dec 3 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 at 20:48










  • You dont have to use setWhatID if you use setTargetObjectId check the link I mentioned for setTargetId
    – Pranay Jaiswal
    Dec 3 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 at 21:06


















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






share|improve this answer























  • 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 at 18:35








  • 1




    @Josh yeah I meant that, :P I forgot to change , My bad
    – Pranay Jaiswal
    Dec 3 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 at 20:48










  • You dont have to use setWhatID if you use setTargetObjectId check the link I mentioned for setTargetId
    – Pranay Jaiswal
    Dec 3 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 at 21:06
















4












4








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






share|improve this answer














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







share|improve this answer














share|improve this answer



share|improve this answer








edited Dec 3 at 18:35

























answered Dec 3 at 18:24









Pranay Jaiswal

13.1k32351




13.1k32351












  • 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 at 18:35








  • 1




    @Josh yeah I meant that, :P I forgot to change , My bad
    – Pranay Jaiswal
    Dec 3 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 at 20:48










  • You dont have to use setWhatID if you use setTargetObjectId check the link I mentioned for setTargetId
    – Pranay Jaiswal
    Dec 3 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 at 21:06




















  • 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 at 18:35








  • 1




    @Josh yeah I meant that, :P I forgot to change , My bad
    – Pranay Jaiswal
    Dec 3 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 at 20:48










  • You dont have to use setWhatID if you use setTargetObjectId check the link I mentioned for setTargetId
    – Pranay Jaiswal
    Dec 3 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 at 21:06


















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 at 18:35






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 at 18:35






1




1




@Josh yeah I meant that, :P I forgot to change , My bad
– Pranay Jaiswal
Dec 3 at 18:36




@Josh yeah I meant that, :P I forgot to change , My bad
– Pranay Jaiswal
Dec 3 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 at 20:48




@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 at 20:48












You dont have to use setWhatID if you use setTargetObjectId check the link I mentioned for setTargetId
– Pranay Jaiswal
Dec 3 at 20:57




You dont have to use setWhatID if you use setTargetObjectId check the link I mentioned for setTargetId
– Pranay Jaiswal
Dec 3 at 20:57




1




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 at 21:06






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 at 21:06




















draft saved

draft discarded




















































Thanks for contributing an answer to Salesforce Stack Exchange!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsalesforce.stackexchange.com%2fquestions%2f241262%2fis-it-possible-to-access-the-email-of-a-leads-owner-directly%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Plaza Victoria

In PowerPoint, is there a keyboard shortcut for bulleted / numbered list?

How to put 3 figures in Latex with 2 figures side by side and 1 below these side by side images but in...