function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
jadenjaden 

Email Template Help

I am looking for some help; I have a trigger I am working on using an email template.

 

Here is the basic description:

 

If due date is created or changed on a task (along with some other criteria); send  an email using a template where the merge fields are from a custom object  called project.  The project Id’s is the ‘whatid’ on the task record which is the object for my trigger. 

 

The email needs to go to the owner of task (assigned to); the created by and last modified if they are different.  These are from the user table not contacts.

 

I can build an array of email addresses to pass to the Toaddress  from the user table.

 

It looks like in the documentation that you can only use the whatID when you use a Contact for the SetTargetObject. 

 

If I set the targetobject to a user, I get an error saying that the whatid is not available for sending emails to userid’s which is what I need to do.   

 

So what I need help with is:

 

Using a trigger on the task; how can I send an email to a user id pointing at a custom object for the merge fields?

 

    String[] toAddresses = new String[]  {a valid email address would be assigned here};

    Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();

    mail.setSaveAsActivity(false);

    mail.setToAddresses(toAddresses);

    mail.settargetObjectid('005E0000000hErv');  //User id

   

    mail.setWhatId(TaskProjIds.get(currenttask.id));              //points to project

    mail.setTemplateId('00XE0000000cegY');   // Template

    Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });

 

This gives the error that whatid is not allowed when settargetobject is a user.

 

If I set the SetTargetObject to a random contact; the email does send and does merge fields.  However the contact also gets the email which is not what I would want.

 

I have searched the forums and it does seem others have run into this but I do not see a clear resolution .

Any help appreciated,

colemabcolemab

please see my post here:

 

http://boards.developerforce.com/t5/Apex-Code-Development/Use-lead-to-populate-email-template-but-send-to-different-email/m-p/359791#M63800

 

Basically, I logged a ticket with salesforce about this and I am waiting a reply but in the mean time I am using a dummy contact address of no-reply@salesforce.com and then clearing the address on the contact.

 

Please log a ticket with support as well - maybe they will fix it.