Is it possible to access the email of a Lead's owner directly?
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
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
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 isSELECT 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 useLead.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
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
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
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 isSELECT 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 useLead.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
add a comment |
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 isSELECT 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 useLead.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
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
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, usingsetWhatId
which should fill out the template, but you cannot use bothsetWhatId
andsetTargetObjectId
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 userenderEmailTemplate
method to get merged email data for lead? developer.salesforce.com/docs/atlas.en-us.apexcode.meta/…
– Pranay Jaiswal
Dec 3 at 21:06
|
show 2 more comments
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
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, usingsetWhatId
which should fill out the template, but you cannot use bothsetWhatId
andsetTargetObjectId
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 userenderEmailTemplate
method to get merged email data for lead? developer.salesforce.com/docs/atlas.en-us.apexcode.meta/…
– Pranay Jaiswal
Dec 3 at 21:06
|
show 2 more comments
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
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, usingsetWhatId
which should fill out the template, but you cannot use bothsetWhatId
andsetTargetObjectId
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 userenderEmailTemplate
method to get merged email data for lead? developer.salesforce.com/docs/atlas.en-us.apexcode.meta/…
– Pranay Jaiswal
Dec 3 at 21:06
|
show 2 more comments
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 Dec 3 at 18:35
answered Dec 3 at 18:24
Pranay Jaiswal
12.5k32251
12.5k32251
Thanks @Pranay this resolved it. Also I think you meant to say my code should besingleMail.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, usingsetWhatId
which should fill out the template, but you cannot use bothsetWhatId
andsetTargetObjectId
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 userenderEmailTemplate
method to get merged email data for lead? developer.salesforce.com/docs/atlas.en-us.apexcode.meta/…
– Pranay Jaiswal
Dec 3 at 21:06
|
show 2 more comments
Thanks @Pranay this resolved it. Also I think you meant to say my code should besingleMail.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, usingsetWhatId
which should fill out the template, but you cannot use bothsetWhatId
andsetTargetObjectId
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 userenderEmailTemplate
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
|
show 2 more comments
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
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 useLead.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