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
Richie DRichie D 

VisualForce Email Templates

Hi,

 

We are trying to send out a VisualForce email template via the standard

 

Messaging.sendEmail(new Messaging.Email[] { email });

 

 

methods in apex. All the email attributes are set correctly but the Id of the object to 'merge' in the template isn't received by the custom controller. The custom controller expects the value to be passed from the template into a component where it is then accessed. It works correctly in the standard salesforce 'send email' function.

 

Has anybody got this to work? Is it even possible?

 

I have seen an idea posted about this but am unsure on the current status.

 

Any help appreciated.

 

Rich.

wesnoltewesnolte

Hey

 

So you're doing all of this:

 

        Contact c = new Contact(lastName='friend',email=this.email,jobRequisition__r=jr);

        

        insert c;

        

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

        

        // We populate the EmailContent object that will be used in the Email template to display Applications values

SendAppEmail sendAppEmail = new SendAppEmail(jr);

EmailContent__c emailContent = new EmailContent__c();

EmailTemplate et;

        et = [SELECT id, DeveloperName FROM emailTemplate WHERE DeveloperName = 'Test template'];

 

        if(et!=null){

       mail.setTemplateId(et.id);

       mail.setTargetObjectId(c.id);

       mail.setWhatId(emailContent.Id);

       mail.setSaveAsActivity(false);

       

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

       

       for ( Messaging.sendEmailResult result : results ) {

           if ( !result.isSuccess () ) {

               System.debug ( result );

           }

           else{

               ApexPages.Message msg = new ApexPages.Message(ApexPages.Severity.INFO, 'Email successfully sent to : '+ c.Email );

               ApexPages.addMessage(msg);  

           }

       }

       

       delete c;

        } 

 

 Cheers,

Wes 

 

Message Edited by wesnolte on 07-02-2009 04:54 AM