+ Start a Discussion
wesnoltewesnolte 

VisualForce email templates and custom controllers

Hello

 

I've looked around the forums and can't find something that answers my question exactly. Basically I'm trying to send an email to a 'contact' concerning a job application. I've created a master-detail field on the contact object linking to the necessary job application, but when the email is sent, the details for the job application (relatedTo fields) are blank.

 

The code follows:

 

(Here a create a temp contact record to get around only being able to send to contacts)

 

public class SendEmail { Job_Req__c jr; public String email {get;set;} public SendEmail(ApexPages.StandardController controller) { jr = (Job_Req__c)controller.getSubject(); } public void sendEmail(){ Contact c = new Contact(lastName='friend',email=this.email,jobRequisition__r=jr); insert c; Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage(); mail.setTemplateId('00X80000001Bm2m'); mail.setTargetObjectId(c.id); Messaging.sendEmailResult[] results = Messaging.sendEmail(new Messaging.SingleEmailMessage[]{mail}); for ( Messaging.sendEmailResult result : results ) { if ( !result.isSuccess () ) { System.debug ( result ); } }

  delete c;

} }

 

 

<messaging:emailTemplate subject="I found this great job for you!" recipientType="Contact" relatedToType="Job_Req__c"> <messaging:plainTextEmailBody > Hello Your friend thought you may be interested in the following job: Reference: {!relatedTo.id} Title: {!relatedTo.Job_Title__c} Description: {!relatedTo.Job_Description__c} Kind Regards, The Team </messaging:plainTextEmailBody> </messaging:emailTemplate>

 

Wes