I'm going to have to write a custom controller for a Visualforce email to pull back details for the contract where my "send email" button will be hosted. My relatedtotype is the Account object (has to be this way because of the amount of related information to Account that I need to pull) if that at all helps.
I was hoping it would be a simple SOQL of:
SELECT <fields 1 - 20> FROM Contracts WHERE id = get.id
or something similar.
I'm afraid my syntax knowledge is lacking and that is why I'm coming to you!
Fingers crossed my life will be:
- Apex to host this SOQL
- VisualforceComponent to bring in the result
- Tie into Visualforce email
Feel free to let me know if my logic is lacking!
UPDATE 1: (now deleted to reduce length, superseded by next update)
UPDATE 2:
I am working on the custom controller. So far I have the following Apex Controller:
public class findContract {
public Id conID {get; set;}
Public List<Contract> currentcontract = new List<Contract>();
public List<Contract> getcurrentcontract() {
currentcontract = [SELECT Id, StartDate, Business_Type__c, Affinity__c, ContractTerm, CompanySigned.name, CompanySignedDate, CustomerSigned.name, CustomerSignedDate, CustomerSignedTitle,
EndDate, Services_Taken_AI_Only__c, Services_Taken_AI_Only_HS__c, Services_Taken_Advice_Only__c, Services_Taken_Advice_Only_HS__c, Services_Taken_Franchise_Comp_EL__c,
Services_Taken_Franchise_Comp_HS__c, Services_Taken_Consultancy__c, Services_Taken_Franchise_Entry_EL__c, Services_Taken_Env__c,
Services_Taken_eRAMS__c, Services_Taken_FRA__c, Services_Taken_HS__c, Services_Taken_SBP__c, Services_Taken_Training__c, Services_Taken_JIT__c, H_S_Notes__c,
Finance_Notes__c, PEL_Notes__c, SpecialTerms FROM Contract WHERE Id = '800D00000044RXCIA2']; //:conID
return currentcontract ;
}
}
I've hard coded the ID because I just want to get something to display to start with, I'm getting nothing right now.
In the Component, I have this:
<apex:component controller="findContract" access="global">
<apex:attribute name="ContractId" description="This is the Contract Id." type="Id" assignTo="{!conID}"/>
<table border="1">
<tr>
<td><apex:outputText value="Contract Term"/></td>
<td><apex:outputText value="Start Date"/></td>
<td><apex:outputText value="Business Type"/></td>
<td><apex:outputText value="Affinity"/></td>
</tr>
<apex:repeat value="{!currentcontract}" var="con" id="theRepeat">
<tr>
<td><apex:outputField value="{!con.ContractTerm}"/></td>
<td><apex:outputField value="{!con.StartDate}"/></td>
<td><apex:outputField value="{!con.Business_Type__c}"/></td>
<td><apex:outputField value="{!con.Affinity__c}"/></td>
</tr>
</apex:repeat>
</table>
</apex:component>
It is as if nothing is coming through from the Apex Controller.
And the VF email:
<c:DisplayContractDetails ContractID="800D00000044RXCIA2"/>
Again, something basic with hardcoded values that aren't even being used yet. I am just not seeing anything returned on the email.
I know the SOQL is fine because I have run it through Force Explorer – I just cannot see where to go next right now. I've tried system.debug but nothing shows up.
Best Answer
You can't actually use a controler (directly) from a visualforce email template. Instead, you'll need to write a visualforce component, and include the component in your template. (You can write a controller for the component, but not for the email template.)
crop1645's question about having multiple contracts associated with an account is important to consider too.
Here's how I would approach this:
Something like this:
Email template:
Visualforce component: (EmailBodyContent.component)
Component controller