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.
trigger email leads
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.
add a comment |
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.
trigger email leads
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 isSELECT Id, Owner.Email FROM Leadworks in a SOQL query so I'm not sure why it doesn't work in Apex. I know formulas useLead.Owner:User.Emailsyntax, 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
add a comment |
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.
trigger email leads
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
trigger email leads
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.
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 isSELECT Id, Owner.Email FROM Leadworks in a SOQL query so I'm not sure why it doesn't work in Apex. I know formulas useLead.Owner:User.Emailsyntax, 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
add a comment |
Lead owner can be a USer or a Queue, so you have to work accordingly.
– Pranay Jaiswal
6 hours ago
@PranayJaiswal What's strange isSELECT Id, Owner.Email FROM Leadworks in a SOQL query so I'm not sure why it doesn't work in Apex. I know formulas useLead.Owner:User.Emailsyntax, 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
add a comment |
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
Thanks @Pranay this resolved it. Also I think you meant to say my code should besingleMail.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, usingsetWhatIdwhich should fill out the template, but you cannot use bothsetWhatIdandsetTargetObjectIdtogether 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
|
show 1 more comment
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
Thanks @Pranay this resolved it. Also I think you meant to say my code should besingleMail.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, usingsetWhatIdwhich should fill out the template, but you cannot use bothsetWhatIdandsetTargetObjectIdtogether 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
|
show 1 more comment
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
Thanks @Pranay this resolved it. Also I think you meant to say my code should besingleMail.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, usingsetWhatIdwhich should fill out the template, but you cannot use bothsetWhatIdandsetTargetObjectIdtogether 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
|
show 1 more comment
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
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
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 besingleMail.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, usingsetWhatIdwhich should fill out the template, but you cannot use bothsetWhatIdandsetTargetObjectIdtogether 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
|
show 1 more comment
Thanks @Pranay this resolved it. Also I think you meant to say my code should besingleMail.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, usingsetWhatIdwhich should fill out the template, but you cannot use bothsetWhatIdandsetTargetObjectIdtogether 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
|
show 1 more comment
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.
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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 Leadworks in a SOQL query so I'm not sure why it doesn't work in Apex. I know formulas useLead.Owner:User.Emailsyntax, 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