A little background. We created a new object for our product called 'Systems'. This 'Systems" object has a multi picklist field for 'Add-Ons' depending on what the customer has loaded on their system. Each 'System' is tied to an account and an account can have multiple systems.
My end goal of this project is to have a salesperson use a template to auto-generate an email to a contact. This email template will look through all the systems for a particular account, figure out which add-ons they don't have, and populate the email with verbiage for those add-ons. I don't know if this is possible, but it would be great to use this email template in Gmail (using Cirrus Insight) and have an entry field pop up where the user can enter the account name. Cirrus allows users to enter specific info into merge fields for things like {!Contact} or {!Opportunity} but I don't know how this would work for a "variable" being passed into a customer controller. It is not a merge field so I don't think Cirrus can handle this. Is there a way to create a pop-up window?
I quickly found out that creating this email template was going to be more complicated than just using Salesforce Merge fields. I need the ability to add or remove entire paragraphs depending on an account's data. I did some research and found that this could possibly be done using Visualforce Templates, Components, and Apex Controllers.
I was able to use a controller, component, and template to pull add-on data from an account using the API call with an account name hardcoded in. I need the ability for a user to enter in which account they are emailing about, however.
I threw together some code that doesn't look quite right but I feel I am stuck. I am starting small with basics and only trying to display all the add-ons a particular account has in the email for now. Can anyone help me out here? Is there a way easier way to do this that I am missing?
VisualForce Template:
<messaging:emailTemplate subject="Test to Pass Value" recipientType="Contact" relatedToType="Account">
<messaging:HTMLEmailBody >
account: {!relatedTo.Name}
<c:EmailComp ToID="{!relatedTo.Id}"/>
</messaging:HTMLEmailBody>
VisualForce Component:
<apex:component controller="EmailController" access="global">
<apex:attribute name="ToID" type="ID" description="the account ID" assignTo="{!accountID}"/>
<h1>{!account.Name}</h1>
<apex:dataTable value="{!AddOns}" var="addOns">
<apex:column headerValue="Add On">
{!addOns}
</apex:column>
</apex:dataTable>
Apex Controller:
public class EmailController {
public ID accountID{set; get;}
public Account account {
get {
if (accountId != null && account == null) {
account = [
SELECT Name
FROM Account
WHERE Id = :accountID
];
}
// Always return the account
return account;
}
set;
}
public List<System__c> addOns {
get {
if (this.account != null
//&& this.account.Add_Ons__c != null
&& this.addOns == null) {
this.addOns = new List<System__c>();
String accountString = '' + this.account;
List<System__c> allAddOns = Database.query('Select Add_ons__c From system__c Where account__r.Name = ' + accountString);
addOns = allAddOns;
}
return addOns;
}
set;
}
}