Visualforce Email Templates

,

Earlier in this chapter, you created a simple email message by setting the value of the email body in your Apex code.

The Force Platform gives you the ability to create and use email templates that can be used to send email messages with a particular format, substituting relevant data where appropriate.

You can create email templates with text, HTML, or with Visualforce. In this section, you will learn how to use Visualforce to create an email template, and then use that template to send an email to a job applicant. In fact, the Visualforce template you use significantly reduces the amount of code needed to send out the email in the existing code, while also improving the flexibility of the emails sent.

Creating an Email Template

The first of your two steps is to create an email template for the message. You have to have Administrator rights in order to create a template, which you should have with your core developer account.

1.
Go to Setup Communications Templates Email Templates and click New Template to bring up the page shown in the figure below.



Figure 207. Defining an email template


2.
Select Visualforce as the template type and click Next.

The following page, shown completed in the figure below, prompts you, in the upper panel, for the basic information required to create this template:

  • Folder - specifies the folder in which to store the template

  • Available for use - makes the template available to users

  • Email Template Name, Developer Name and Description - labels the template, providing a unique identifier for use with the API, and provides a description for internal use

The lower panel asks for information used to create the outline of the Visualforce page template:

  • Email Subject - the subject of the subsequent email

  • Recipient Type - the source of the email recipient; indicate that a recipient comes from the Salesforce standard objects of Contact or Lead, or the more general User, which contains all Force Platform users.

  • Related To Type - the base table used in the Visualforce controller

You can use fields from either the Recipient Type object or the Related To Type object in your Visualforce template, as well as any fields you can access through related syntax, just as you can in a standard Visualforce Page.

For this particular example, you will not need to use the Recipient Type object for Users, as you will see in the remainder of this section.

3.
Click the Available for Use checkbox, enter Interview Schedule as the Email Template Name and give the template a description.

4.
In the lower panel, enter Interview Schedule as the email subject, User for the Recipient Type, and Job_Application__c as the Related To Type, and click Save.

The following figure displays the lower portion of the newly-created template. The default template text is displayed in the Plain Text Preview, along with two related lists of Visualforce attachments and standard attachments. You can test your email template right now, but why not edit the Visualforce template a bit before getting to that step?

Figure 208. Part of a Visualforce email template


5.
Click Edit Template, which will bring you into a standard Visualforce edit window with the generated template code.

You can see that this template is using components prefaced by messaging: instead of apex:, since these components are from the messaging workspace.

The template, as currently implemented, uses plainTextEmailBody tags to indicate standard text content. Although best practices call for implementing both a plain text and HTML version of every email, for the purposes of this exercise you can replace this body with an HTML body.

6.
Replace the plainTextEmailBody with the new Visualforce code, as shown below.

<messaging:emailTemplate
       subject="Interview Schedule"
recipientType="User" relatedToType="Job_Application__c">
<messaging:htmlEmailBody >
Dear {!recipient.Name}, <p/>
You have been selected to interview for position
{!relatedTo.Position__r.Job_Description__c}.   <p/>
The interview schedule is attached.
</messaging:htmlEmailBody>
</messaging:emailTemplate>

This template seems to have all the basic information your email needs. You are ready to save the new version and try it out.

7.
Save your Visualforce page to return to the detail page for the template.

8.
Click Preview in the Email Template section of the page to bring up a dialog box, as shown below.

The Preview dialog prompts you for the two values you need to create an email from the template: a User record (or ID) and a Job Application record (or ID).

If you simply give those values, the generated email previews in the Preview area on the detail page for the template. You can also choose to have the email message sent to an email address for further verification that the email looks good.

Figure 209. Previewing your Visualforce email template


9.
Select a User and a Job Application record, click the Send email preview box, and OK.

The email appears in the preview box and you should receive an email message that looks very similar to the preview shown above.

The email looks fine, so you can return to the class code for the sendCandidateSchedule class to use this template, instead of simply setting the body of the email message. In order to change the code, you need to know the unique ID of the template you just created.

10.
Look at the URL of the detail page for your template. Your instance name should be followed by a 15-character unique ID. Copy that ID, in preparation for changing your code.

11.
Go to Setup Develop Apex Classes sendEmail, and go to the sendCandidateSchedule method.

12.
Delete all the code in the ‘Create the email’ section after the first two lines, so that this section looks like the following code:

// Create the email to the candidate and attach schedule
  Messaging.SingleEmailMessage mail =
    new Messaging.SingleEmailMessage();
  mail.setToAddresses( new String[]
    { jobApplication.Candidate__r.Email__c });

13.
Add in the following lines of code:

// Create the email to the candidate and attach schedule
Messaging.SingleEmailMessage mail =
    new Messaging.SingleEmailMessage();
mail.setToAddresses( new String[]
    { jobApplication.Candidate__r.Email__c });
mail.setTemplateId(your_template_id);
								mail.setwhatId(JobApplicationId);

Replace your_template_id with the ID for your template. Save your modified code

These two lines of code supply everything your template needs to assemble the email message body. You have successfully integrated your new template into your code, improving the appearance of the outgoing email, as well as simplifying the code used to perform this task.

When you deleted the previous code for the email, you also got rid of the code that attached a Visualforce document to the message. How will you replace that?

As you may have noticed, the email template mentions that the interview schedule is attached to the message. Since the email template itself is a Visualforce page, you can create a Visualforce attachment as part of the template—and it’s quite easy.

1.
Return to the email template you just created and click Edit Template.

2.
Place your cursor after the closing tag for the htmlEmailBody and before the end tag for the emailTemplate.

3.
Enter <mess as the start of a new tag. Select the <messaging:attachment> tag for inclusion into the code. Save the code for the template.

With these tags, you have identified a portion of the Visualforce page that is created as an attachment. And you already have the Visualforce code for that attachment written.

4.
Get the code for the Visualforce attachment from the Code Share project for this book. The code is very similar to the code for the candidateinterviewscheduleVisualforce page, with the main differences being that references in field expressions to Job Application object have been replaced with the keyword relatedTo, since te Job_Applicatoin__c object was identified as that entity in the template.

5.
Return to your template and paste the code for the attachment between the messaging:attachment tags, removing the beginning and ending page tags.

6.
Save the new version of the template.

Now that your Visualforce PDF has been properly added as an attachment, you can see it listed in the related list for your template. You should be able to run this new version of your email from the same button you used before.

1.
Return to the detail page for a Job Application and click Email Schedule.

2.
Enter an email address on the next page and click Send. The email sent should look like the figure below, with the attachment included in the email.

Figure 210. An email from a Visualforce email template


With this code, you have matched the functionality in the previous version of this Apex class. But you may have noticed that you did not use the recipient attribute of the Visualforce template since you used the setToAddresses to indicate the address for the email. You had to address the email this way since the recipient attribute was set to point to a record in the User object, and your Candidates, being outside the Force Platform organization, did not have a User record.

Using a Visualforce template for your outgoing emails allows you to include a Visualforce page, rendered as a PDF, with the template, and simplifies the code you need to send a message based on that template.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.138.101.91