+ Start a Discussion
Zach Gavin DelacroixZach Gavin Delacroix 

Using Custom Controller in Visualforce Email Template

Hello Experts,

I'd like an advise on how to use custom controller in Visualforce Email Template.

Basically, I would create a Controller like this.
 
public class HourlyJobReport{
    
    public List<job__c> JobList{get;set;}
    
    public HourlyJobReport() {
        
        JobList = new List<Job__c>();
        JobList = [select name, status__c, duration__c, start__c from job__c where status__c = 'Dispatch' and Duration__c > 60];
    }
    
}

Then would reference the code to the Component like this.
 
<apex:component controller="HourlyJobReport" access="global">

    <apex:dataTable value="{!JobList}" var="job">
             <column value="{!job.name}"/>
    </apex:dataTable>


</apex:component>

Then use the component in the Template like this.
 
<c:nameOfTheComponent>

This approach works great. But I'd like to have the Repeat tag in my visualforce Template Intead of using it in the Component. One reason is that when I do styling from the Component, the Style does not work when the Visualforce Email Template is Sent through email.

in the Preview of the VisualForce Page, it looks like this one below. (Note, I'm using style here from the Component that I have created for the custom controller)

User-added image

But when it's sent to my email, it will look like just a plain text. Something like below.

User-added image

Does anyway here encountered issues like this or have solution for this one?

Thanks a lot :)

-Zach
Best Answer chosen by Zach Gavin Delacroix
Zach Gavin DelacroixZach Gavin Delacroix
Thanks for the responses guys!

I got this one resolved now by adding the style Inline instead of putting the Style inside the <style> tag.

So the code looks like this.
 
<apex:component controller="HourlyJobReport" access="global">
  
  <table style="border:solid 1px">
    <tr>
        <td style="border:solid 1px"> Job Name</td>
        <td style="border:solid 1px"> Status</td>
        <td style="border:solid 1px"> Start</td>
        <td style="border:solid 1px"> Duration</td>
    </tr>
    <apex:repeat value="{!JobList}" var="job">
        <tr> 
            <td style="border:solid 1px"> {!job.name} </td>
            <td style="border:solid 1px"> {!job.Status__c} </td>
            <td style="border:solid 1px"> {!job.Start__c} </td>
            <td style="border:solid 1px"> {!job.Duration__c} </td>
        </tr>
    </apex:repeat>
  </table>
</apex:component>

Not sure but it seems that when using Custom Components, the styling drop off when the visualforce is sent to Email if the style is inside the <style> tag or External StyleSheet.

Thanks for helping out :)

-Zach

All Answers

JeffreyStevensJeffreyStevens
I've got styling to work in my components on email templates. Try putting the styling in the lowest level attribute - like on the <td> or create a <div> around the element. 
Zach Gavin DelacroixZach Gavin Delacroix
Thanks for the responses guys!

I got this one resolved now by adding the style Inline instead of putting the Style inside the <style> tag.

So the code looks like this.
 
<apex:component controller="HourlyJobReport" access="global">
  
  <table style="border:solid 1px">
    <tr>
        <td style="border:solid 1px"> Job Name</td>
        <td style="border:solid 1px"> Status</td>
        <td style="border:solid 1px"> Start</td>
        <td style="border:solid 1px"> Duration</td>
    </tr>
    <apex:repeat value="{!JobList}" var="job">
        <tr> 
            <td style="border:solid 1px"> {!job.name} </td>
            <td style="border:solid 1px"> {!job.Status__c} </td>
            <td style="border:solid 1px"> {!job.Start__c} </td>
            <td style="border:solid 1px"> {!job.Duration__c} </td>
        </tr>
    </apex:repeat>
  </table>
</apex:component>

Not sure but it seems that when using Custom Components, the styling drop off when the visualforce is sent to Email if the style is inside the <style> tag or External StyleSheet.

Thanks for helping out :)

-Zach
This was selected as the best answer