I have created a VF page, which sends mail to a particular set of contacts. My VF and Controller are working fine, and i am able to send mail. However, its the VF UI which is troubling me.

Whenever the dropdown showing the email template (EmailTemplate Object) changes, I need to render a button. But I am not able to access the label of the selected dropdown so that I can work on it. Right now I am using object id in rendered portion of VF which I want to get rid of Here is my VF and Apex:

VF Page:

<apex:page StandardController="Contact" extensions="SendEmailtoContactController"  showHeader="false" sidebar="false" title="Send Mass Email">    

       display: none;


    <apex:form >
             <apex:pageMessages id="showmsg"> </apex:pageMessages>
        <apex:pageBlock tabStyle="Contact" title="Send Mass Email">

            <apex:pageBlockSection >
                <apex:actionRegion >
                    <apex:selectList value="{!emailTemplate}" size="1" label="Choose Email Template">
                        <apex:selectOptions value="{!emailTemplateOptions}" />
                        <apex:actionSupport event="onchange" action="{!passValueToController}" reRender="btn" />
                <td>  <apex:inputFile value="{!attBody}" filename="{!attName}"/></td>

                <div align="left" draggable="false" >

                <div align="center" draggable="false">
                    <apex:outputPanel id="btn">
                    <apex:commandButton value="Send Mass Emails" action="{!sendEmailToContacts}" onclick="onUpload();" rendered="{!(emailTemplate=='00Xq0000001RJoAAAW')}"/> <!-- PROBLEM AREA-->

                    <apex:commandButton value="Send Preview Mail" action="{!sendEmailTologgedin}"/>
                    <apex:commandButton value="Close" onClick="window.close();"/>
                    <apex:actionStatus id="actStatusId" >
                        <apex:facet name="start" >
                            <img src="/img/loading.gif" />                    

    <div class="waitingSearchDiv" id="el_loading" style="background-color: #fbfbfb; height:100%;opacity:0.75;width:100%;">
    <div class="waitingHolder" style="top: 100px; width: 91px;">
        <img class="waitingImage" src="/img/loading.gif" title="Please Wait..."  style="width: 40;height:40;"/>
    <span class="waitingDescription">Attachment is Uploading . . . .</span>

    function onUpload()
        var ul = document.getElementById('el_loading');
        ul.style.display = 'inline';

Relevant pieces of Apex:

public with sharing class SendEmailtoContactController{
    public List<SelectOption> emailTemplateOptions {get;set;}
    public String emailTemplate {get;set;}
    public list<EmailTemplate> eTemp = new list<EmailTemplate>();
    public transient blob attBody{get;set;}
    public string attName{get;set;}
    public string commentBody{get;set;}
    public List<Contact> contactList{get;set;}
    public list<Attachment> attMail;

public SendMassEmailContactController(ApexPages.StandardController controller) { 

        emailTemplateOptions = new List<SelectOption>();

        emailTemp = new list <EmailTemplate>([Select Id, Name from EmailTemplate where name in ('456test','123test')]);
        for(EmailTemplate et : emailTemp) {
            emailTemplateOptions.add(new SelectOption(et.id,et.name)); 

        emailTemplate = emailTemp[0].id;

        contactList = [Select Id, Name,Email,Email_Comments__c,Account.Name from Contact where Newsletter__c = true];
        attMail = new list<Attachment>( [select id, ContentType, Body, parentId From Attachment where parentId in :emailTemp]);

    public void sendEmailToContacts(){
        //-------------------------- Throw error when no attachment present in the email
        if (attname == null || attname ==''){            
            ApexPages.addmessage(new ApexPages.message(ApexPages.severity.FATAL,'Select attachment file'));
            BatchtoSendContactMassEmail bc = new BatchtoSendContactMassEmail();
            // Batch simply sends the mail to the contacts

You could create a Map<Id, String> and populate it with the template Ids and names from your selectOptions, then create a getter variable that pulls from the map using the selected template ID:

public static Map<Id, String> templateMap { get; set; }
public static String selectedTemplate {
  get {
    return templateMap.get(emailTemplate);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.