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
ScriptMonkeyScriptMonkey 

can't suppress primary email send in Custom Controller to Preview / Test Email Messages

I've written a Visualforce page to edit "Custom" style salesforce email templates, but it lets you use a WYSIWYG editor rather than needing to know html or coping and pasting from another software. That all works great, and everything is good to go except one functionality.  I'm recreating the "send test email" from the salesforce interface.

 

I've got it asking for whatId and setTargetObjectId (so I can use the template and have it merge everything) as well as adding a new email address to the mail.setToAddress() method, and all seems good.  

 

The only problem is that the email goes to the setTargetObjectId as well as the email addresses I added.  I want to use the setTargetObjectId to merge all the fields into the email, but suppress the email to that contact.  I know there is a checkbox on the salesforce test email window referring to "send Preview Email" and I need to reproduce that functionality.

 

Here's my code to send the email, if it helps..

'

            Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
            mail.setTargetObjectId(dummytask.whoid);
            transient string[] toAddresses = new list<string>();
            toAddresses.add(testEmailTo);
            mail.setToAddresses(toAddresses);
            mail.setWhatId(dummytask.whatid);
            mail.setTemplateId(idToTest);
            mail.setReplyTo('noreply@mycompany.com');
            mail.setSenderDisplayName('Email Test');
            mail.setSaveAsActivity(false);
            Messaging.sendEmail(new Messaging.SingleEmailMessage[] {mail});
            dummyTask = new task();
            idTotest = null;