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





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty{ margin-bottom:0;
}






up vote
5
down vote

favorite












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







New contributor




Josh is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.




















  • Lead owner can be a USer or a Queue, so you have to work accordingly.
    – Pranay Jaiswal
    6 hours ago










  • @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
    6 hours ago












  • 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
    6 hours ago



















up vote
5
down vote

favorite












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







New contributor




Josh is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.




















  • Lead owner can be a USer or a Queue, so you have to work accordingly.
    – Pranay Jaiswal
    6 hours ago










  • @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
    6 hours ago












  • 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
    6 hours ago















up vote
5
down vote

favorite









up vote
5
down vote

favorite











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







New contributor




Josh is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











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







New contributor




Josh is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











share|improve this question







New contributor




Josh is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









share|improve this question




share|improve this question






New contributor




Josh is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









asked 6 hours ago









Josh

1306




1306




New contributor




Josh is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





New contributor





Josh is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






Josh is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.












  • Lead owner can be a USer or a Queue, so you have to work accordingly.
    – Pranay Jaiswal
    6 hours ago










  • @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
    6 hours ago












  • 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
    6 hours ago




















  • Lead owner can be a USer or a Queue, so you have to work accordingly.
    – Pranay Jaiswal
    6 hours ago










  • @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
    6 hours ago












  • 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
    6 hours ago


















Lead owner can be a USer or a Queue, so you have to work accordingly.
– Pranay Jaiswal
6 hours ago




Lead owner can be a USer or a Queue, so you have to work accordingly.
– Pranay Jaiswal
6 hours ago












@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
6 hours ago






@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
6 hours ago














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
6 hours ago






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
6 hours ago












1 Answer
1






active

oldest

votes

















up vote
4
down vote



accepted










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
    6 hours ago








  • 1




    @Josh yeah I meant that, :P I forgot to change , My bad
    – Pranay Jaiswal
    6 hours ago










  • @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
    4 hours ago










  • You dont have to use setWhatID if you use setTargetObjectId check the link I mentioned for setTargetId
    – Pranay Jaiswal
    4 hours ago










  • Ah, but then it'll try to use the lead owner to fill in the fields instead of the lead object, right? The fields in my template are all based off of the lead object
    – Josh
    4 hours ago











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',
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
});


}
});






Josh is a new contributor. Be nice, and check out our Code of Conduct.










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








up vote
4
down vote



accepted










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
    6 hours ago








  • 1




    @Josh yeah I meant that, :P I forgot to change , My bad
    – Pranay Jaiswal
    6 hours ago










  • @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
    4 hours ago










  • You dont have to use setWhatID if you use setTargetObjectId check the link I mentioned for setTargetId
    – Pranay Jaiswal
    4 hours ago










  • Ah, but then it'll try to use the lead owner to fill in the fields instead of the lead object, right? The fields in my template are all based off of the lead object
    – Josh
    4 hours ago















up vote
4
down vote



accepted










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
    6 hours ago








  • 1




    @Josh yeah I meant that, :P I forgot to change , My bad
    – Pranay Jaiswal
    6 hours ago










  • @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
    4 hours ago










  • You dont have to use setWhatID if you use setTargetObjectId check the link I mentioned for setTargetId
    – Pranay Jaiswal
    4 hours ago










  • Ah, but then it'll try to use the lead owner to fill in the fields instead of the lead object, right? The fields in my template are all based off of the lead object
    – Josh
    4 hours ago













up vote
4
down vote



accepted







up vote
4
down vote



accepted






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 6 hours ago

























answered 6 hours ago









Pranay Jaiswal

12k32051




12k32051












  • 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
    6 hours ago








  • 1




    @Josh yeah I meant that, :P I forgot to change , My bad
    – Pranay Jaiswal
    6 hours ago










  • @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
    4 hours ago










  • You dont have to use setWhatID if you use setTargetObjectId check the link I mentioned for setTargetId
    – Pranay Jaiswal
    4 hours ago










  • Ah, but then it'll try to use the lead owner to fill in the fields instead of the lead object, right? The fields in my template are all based off of the lead object
    – Josh
    4 hours ago


















  • 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
    6 hours ago








  • 1




    @Josh yeah I meant that, :P I forgot to change , My bad
    – Pranay Jaiswal
    6 hours ago










  • @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
    4 hours ago










  • You dont have to use setWhatID if you use setTargetObjectId check the link I mentioned for setTargetId
    – Pranay Jaiswal
    4 hours ago










  • Ah, but then it'll try to use the lead owner to fill in the fields instead of the lead object, right? The fields in my template are all based off of the lead object
    – Josh
    4 hours ago
















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
6 hours ago






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
6 hours ago






1




1




@Josh yeah I meant that, :P I forgot to change , My bad
– Pranay Jaiswal
6 hours ago




@Josh yeah I meant that, :P I forgot to change , My bad
– Pranay Jaiswal
6 hours ago












@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
4 hours ago




@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
4 hours ago












You dont have to use setWhatID if you use setTargetObjectId check the link I mentioned for setTargetId
– Pranay Jaiswal
4 hours ago




You dont have to use setWhatID if you use setTargetObjectId check the link I mentioned for setTargetId
– Pranay Jaiswal
4 hours ago












Ah, but then it'll try to use the lead owner to fill in the fields instead of the lead object, right? The fields in my template are all based off of the lead object
– Josh
4 hours ago




Ah, but then it'll try to use the lead owner to fill in the fields instead of the lead object, right? The fields in my template are all based off of the lead object
– Josh
4 hours ago










Josh is a new contributor. Be nice, and check out our Code of Conduct.










draft saved

draft discarded


















Josh is a new contributor. Be nice, and check out our Code of Conduct.













Josh is a new contributor. Be nice, and check out our Code of Conduct.












Josh is a new contributor. Be nice, and check out our Code of Conduct.
















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

Lallio

Unable to find Lightning Node

Futebolista