Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7493063
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T16:44:08+00:00 2026-05-29T16:44:08+00:00

I need to send an email: to an arbitrary email address using an email

  • 0

I need to send an email:

  • to an arbitrary email address
  • using an email template
  • merging in values from a Lead

The code would look something like this:

Messaging.SingleEmailMessage msg = new Messaging.SingleEmailMessage();
msg.setToAddresses(new String[] { 'me@somedomain.com' });
msg.setSaveAsActivity(false);

// this causes a runtime error. Lead ID not allowed 
msg.setWhatId(lead.Id);

msg.setTemplateId(templateID);
messages.add(msg);

Problem is, you cannot use a Lead ID for the WhatID. I’m not sending an email to the Lead, so I can’t use the Lead for the TargetObjectId.

Is this possible?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T16:44:09+00:00Added an answer on May 29, 2026 at 4:44 pm

    Frustrating that SFDC still doesn’t support a more flexible templating context, isn’t it? Given your constraints, I think you do have options:

    • do your own templating replacement by loading the template dynamically and replacing home-grown tags with lead fields (e.g. $LEAD.NAME or whatever)
    • temporarily save the Lead as a supported object (Account perhaps?), persist it, use that as the WhatId, send your email, and then rollback/delete (EDIT: as pointed out above, rollback apparently “unsends” emails, so don’t use that.)
    • use a third-party or custom off-platform mail solution that offers template-based merging that is more flexible than Apex, probably via a WS API

    I’ve played with all of these approaches but the only one I’ve seriously implemented is #3. Option 1 seems like an attractive option assuming you can make it work.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Since I need to send email by code, I'm currently using plan text but
We need to send email from our desktop application using C# 3.5 . The
I need to send email notifications in my Java web application. I'm using Apache
I am using this code to send email var message = new MailMessage(abc@somedomain.com, administrator@anotherdomain.com);
I'm using the excellent MvcMailer package to send email from within my application. I'm
I need to send an email from my iPhone app but the name of
I need to send a email from application with text in the from field.
I need to send html email. So I am using PEAR library with Mail
I'm using asp.net with vb.net.. I need to send an email to my website
I know how to send email using PHPmail. But what if I need to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.