Tuesday, May 28, 2013

Creating a Visualforce Email Template and using in Apex Class

To create a Visualforce email template:
  1. Do one of the following:
    • If you have permission to edit public templates, from Setup, click Communication Templates | Email Templates.
    • If you don’t have permission to edit public templates, at the top of any Salesforce page, click the down arrow next to your name. From the menu under your name, select Setup or My Settings—whichever one appears. Then from the left side of the page, click Email | My Templates.
  2. Click New Template.
  3. Choose Visualforce and click Next.
    You can’t send a mass email using a Visualforce email template.
  4. Choose a folder in which to store the template.
  5. Select the Available For Use checkbox if you would like this template offered to users when sending an email.
  6. Enter an Email Template Name.
  7. If necessary, change the Template Unique Name. This is a unique name used to refer to the component when using the Force.com API. In managed packages, this unique name prevents naming conflicts on package installations. This name can contain only underscores and alphanumeric characters, and must be unique in your organization. It must begin with a letter, not include spaces, not end with an underscore, and not contain two consecutive underscores. With the Template Unique Name field, a developer can change certain components’ names in a managed package and the changes are reflected in a subscriber’s organization.
  8. Select an Encoding setting to determine the character set for the template.
  9. Enter a Description of the template. Both template name and description are for your internal use only.
  10. Enter the subject line for your template in Email Subject.
  11. In the Recipient Type drop-down list, select the type of recipient that will receive the email template.
  12. In the Related To Type drop-down list, optionally select the object from which the template will retrieve merge field data.
  13. Click Save.
  14. On the Viewing Email Templates page, click Edit Template.
  15. Enter markup text for your Visualforce email template.
    Note
    If you are including an image, we recommend uploading it to the Documents tab so that you can reference the copy of the image that is on our server. For example:
    <apex:image id="Logo" value="https://na7.salesforce.com/servlet/servlet.ImageServer?
    id=015D0000000Dpwc&oid=00DD0000000FHaG&lastMod=127057656800" 
    height="64" width="64"/>
  16. Click Version Settings to specify the version of Visualforce and the API used with this email template. If your organization has installed managed packages from the AppExchange, you can also specify which version of each managed package to use with this email template. Generally, you should use the default value for all versions. This associates the email template with the most recent version of Visualforce, the API, as well as each managed package. You can specify an older version of Visualforce and the API to maintain specific behavior. You can specify an older version of a managed package if you want to access components or functionality that differs from the most recent package version.
  17. Click Save to save your changes and view the details of the template, or click Quick Save to save your changes and continue editing your template. Your Visualforce markup must be valid before you can save your template.
    Note
    The maximum size of a Visualforce email template is 1 MB.
    You can’t send a mass email using a Visualforce email template. The {!Receiving_User.field_name} and {!Sending_User.field_name} merge fields work only for mass email and are unavailable in Visualforce email templates.
The following example shows how you can define a Visualforce email template that displays all the cases associated with a contact. The example uses an <apex:repeat> tag to iterate through all the cases related to a contact and incorporate them into the body of the template:
<messaging:emailTemplate recipientType="Contact"
 relatedToType="Account"
 subject="Case report for Account: {!relatedTo.name}"
 language="{!recipient.language__c}"
 replyTo="support@acme.com">

 <messaging:htmlEmailBody>
  <html>
   <body>

   <p>Dear {!recipient.name},</p>
   <p>Below is a list of cases related to {!relatedTo.name}.</p>
   <table border="0" >
    <tr>
     <th>Case Number</th><th>Origin</th>
     <th>Creator Email</th><th>Status</th>
    </tr>
    <apex:repeat var="cx" value="{!relatedTo.Cases}">
    <tr>
     <td><a href = 
      "https://na1.salesforce.com/{!cx.id}">{!cx.CaseNumber}
     </a></td>
     <td>{!cx.Origin}</td>
     <td>{!cx.Contact.email}</td>
     <td>{!cx.Status}</td>
    </tr>
    </apex:repeat> 
   </table>
   <p/>
   <center>
    <apex:outputLink value="http://www.salesforce.com">
     For more detailed information login to Salesforce.com
    </apex:outputLink>
   </center>
   </body>
  </html>
 </messaging:htmlEmailBody>
</messaging:emailTemplate>
Notice the following about the markup:
  • The attributes recipientType and relatedToType act as controllers for the email template. With them you can access the same merge fields that are available to other standard controllers. The recipientType attribute represents the recipient of the email. The relatedToType attribute represents the record to associate with the email.
  • The <messaging:htmlEmailBody> component can include a mix of Visualforce markup and HTML. The <messaging:plainTextEmailBody> component can only include Visualforce markup and plain text.
  • To translate Visualforce email templates based on recipients' or related objects' languages, use the <messaging:emailTemplate> tag's language attribute (valid values: Salesforce supported language keys, for example, “en-US”). The language attribute accepts merge fields from the email template's recipientType and relatedToType attributes. You create custom language fields for use in the merge fields. The Translation Workbench is required to translate email templates. The example uses a merge field to obtain a language attribute for the contact receiving the email.

Example:

Create a Apex Class Name UserNotLoginToday

public class UserNotLoginToday {
//capture the user id
public Id thisUserId {get;set;}
//a list to hold this year's gifts
public List<User> thisNotLoginUsers = new List<User>();
//get the gifts into the list
public List<User> getUsersNotLoginToday() {
//criteria for opps
thisNotLoginUsers = [SELECT Id, Name FROM User
WHERE LastLoginDate <: Date.Today().AddDays(-1)];   
return thisNotLoginUsers;   
}
}
Create an Apex User Component name - NotLoginUserComponent

<apex:component controller="UserNotLoginToday" access="global">

   <p>Dear Sales force Administrator,</p>
   <p>Below is a list of users not logged into sales force with in a day.</p>
   <p/>
  <ul>
    <apex:repeat var="cx" value="{!UsersNotLoginToday}">
    <li>
     <a href = "https://na2.salesforce.com/{!cx.id}">{!cx.Name}
     </a></li>
    </apex:repeat> 
   </ul>
</apex:component>
 
Create a visualforce email template to send list of users to administrator
 
 <messaging:emailTemplate subject="users not login" recipientType="User" relatedToType="User" replyTo="pawanns@hotmail.com">
<messaging:htmlEmailBody >
  <html>
   <body>
    <c:NotLoginUserComponent />
   <center>
    <apex:outputLink value="http://www.salesforce.com">
     For more detailed information login to Salesforce.com
    </apex:outputLink>
   </center>
   </body>
  </html>
 </messaging:htmlEmailBody>
</messaging:emailTemplate>
     

No comments:

Post a Comment