Skip to main content
2 of 3
added 499 characters in body
Rory
  • 469
  • 4
  • 18

Passing user entered values from Visualforce template to Apex Controller

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 />
    </messaging:HTMLEmailBody>
</messaging:emailTemplate>

VisualForce Component:

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

    <apex:attribute name="ToID" type="ID" description="the account ID" assignTo="{!accountID}"/>

    <apex:dataTable value="{!EmailContent}" var="Add_Ons">
        <apex:column >
            <apex:facet name="header">Account Name</apex:facet>
            {!Add_Ons.Add_ons__c}
        </apex:column>
    </apex:dataTable>
</apex:component> 

Apex Controller:

public class EmailController {

    public List<System__c> AddOns;
    
    public ID accountID;
    public ID getaccountID(){ return accountID; }
    public void setaccountID(ID s){
        accountID = s;
        getEmailContent();
    }
    
    public void generateEmailContent() {
        AddOns = [select Id, Add_Ons__c From System__c where account__c = :accountID ];
        
    }
    public List<System__c> getEmailContent() {
        return AddOns;
    
    }
}
Rory
  • 469
  • 4
  • 18