function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Suman MSuman M 

Passing selectoption values from VF Page to controller

Hi,

I Have a VF Page which displays the EmailTemplate Name for user selection, Once an user selects a Template name and click submit I need to Pass that selected value to the Controller. I'm getting stuck here, pls help on this. Thank You

VF Page:

<apex:page controller="Opt_In_Out_Renewal_Class">
    <apex:sectionHeader title="SAP OptIn Email Template Selection"/>
        <apex:form >
            <apex:messages styleClass="error" style="color:red" />
            <apex:pageBlock title="Select an Email Template">
                <apex:selectList value="{!OptInTemplates}" size="1">
                    <apex:selectOptions value="{!OptInRenewalNoticeTemplate}"/>
                </apex:selectList>
            </apex:pageBlock>
            <apex:commandButton value="Submit" action="{!Submit}"/>
            <apex:commandButton value="Cancel" action="{!Cancel}"/>
        </apex:form>
</apex:page>


Controller:

public class Opt_In_Out_Renewal_Class
{
    public ID RenewalID;
    public PageReference P;
    map<Id,String> mapOptIntemplates = new map<Id,String>();
    map<Id,String> mapOptOutTemplates = new map<Id,String>();
    List<EmailTemplate> result;
    integer i=0;
    public Opt_In_Out_Renewal_Class()
    {
        RenewalID=ApexPages.currentPage().getParameters().get('RenewalID');
    }
   
    public String OptInTemplates { public get; public set; }
    public List<SelectOption> getOptInRenewalNoticeTemplate()
    {
        List<SelectOption> OptIns= new List<SelectOption>();
        for (EmailTemplate t : [select Id,Name from EmailTemplate where FolderId = '00l70000001QZrV' AND Name Like '%Renewal Notice template- Opt In%'])
        {
            OptIns.add(new SelectOption(t.Id,t.Name));
            mapOptIntemplates.put(t.Id,t.Name);
        }
        return OptIns;
    }
    public void setOptInTemplates(String value)
    {
       
    }
    public String OptOutTemplates{public get; public set;}
    public List<SelectOption> getOptOutRenewalNoticeTemplate()
    {
        List<SelectOption> OptOuts= New List<SelectOption>();
        for (EmailTemplate t1:[select Id, Name From EmailTemplate Where FolderId = '00l70000001QZrV' AND Name Like'%Renewal Notice template- Opt Out%'])
        {
            OptOuts.add(new SelectOption(t1.Id,t1.Name));
            mapOptOutTemplates.put(t1.Id,t1.Name);
        }
        return OptOuts;
    }
   
    public PageReference Submit()
    {
        result=(List<EmailTemplate>)[select Name from EmailTemplate where Name = :mapOptIntemplates.get(OptInTemplates)];
        system.debug('***Name***'+result);
        for(i=0;i<result.length;i++)
        {
        if(result[i]=='Renewal Notice template- Opt In_Detailed')
        {
            P = New PageReference('/_ui/core/email/author/EmailAuthor?p3_lkid='+RenewalID+'&retURL=%2F'+RenewalID+'&template_id='+'00X70000001X0aT');
        }
        else if(result[i]=='Renewal Notice template- Opt In_One Price')
        {
            P = New PageReference('/_ui/core/email/author/EmailAuthor?p3_lkid='+RenewalID+'&retURL=%2F'+RenewalID+'&template_id='+'00X70000001X57E');
        }
        }
        return P;
    }
    public PageReference Cancel()
    {
        P = New PageReference('/'+RenewalID);
        return P;
    }
}
DevADSDevADS
Hey Suman,

You can use my blog for this. The code is preety straight forward & generic. You can change it as per your requirement.
http://salesforce-atom.blogspot.in/2013/12/email-template-example.html

Hope It will help.Let me know If you can't make it. Happy Coding!!
Suman MSuman M
Hi Amit,

mine is a kind of different scenario, Here I have a Button XYZ on a record detail page, once I click it, It directs me to a VF Page, where I can select an Email Template and click submit. On submitting it redirects to Standard salesforce SendAnEmail functionality Page, in that page the EmailTemplate that I selected on VF page will be shown.

In the above given VF and controller I tried some changes, Now I'm able to get the selected template on VF to controller and able to query with that.

i.e; on Submiting the page after selecting an emailtemplate, below function will be called

public PageReference Submit()
    {
        system.debug('***OptInTemplates***'+OptInTemplates);
        system.debug('***OptOutTemplates***'+OptOutTemplates);
        result=[select Id, Name from EmailTemplate where Id=:OptInTemplates];
        result1=[select Id, Name From EmailTemplate where Id=:OptOutTemplates];
        system.debug('***Name***'+result);
        system.debug('***Name2***'+result1);
       
        if(!result1.isEmpty())
        {
        for(i=0;i<3;i++)
        {
        if(result[i].Name=='Renewal Notice template- Opt In_Detailed')
        {
            P = New PageReference('/_ui/core/email/author/EmailAuthor?p3_lkid='+RenewalID+'&retURL=%2F'+RenewalID+'&template_id='+'00X70000001X0aT');
        }
        else if(result[i].Name=='Renewal Notice template- Opt In_One Price')
        {
            P = New PageReference('/_ui/core/email/author/EmailAuthor?p3_lkid='+RenewalID+'&retURL=%2F'+RenewalID+'&template_id='+'00X70000001X57E');
        }
         return P;
        }
        }
        else
        {
        for(i=0;i<3;i++)
        {
        if(result1[i].Name=='Renewal Notice template- Opt Out_Detailed')
        {
            P = New PageReference('/_ui/core/email/author/EmailAuthor?p3_lkid='+RenewalID+'&retURL=%2F'+RenewalID+'&template_id='+'00X70000001X0aU');
        }
        else if(result1[i].Name=='Renewal Notice template- Opt Out_One Price')
        {
            P = New PageReference('/_ui/core/email/author/EmailAuthor?p3_lkid='+RenewalID+'&retURL=%2F'+RenewalID+'&template_id='+'00X70000001X57F');
        }
        }
        return P;
        }
    }

In the above code, result and result1 stores the EmailTemplate output that is Name and ID, but further I'm not able to using them in Comparing.

result and result I have decleared as List<EmailTemplate>.

Can u help on this. Thank You
Suman MSuman M
I Have sloved It. It is working. Thank You.


DevADSDevADS
Good...Nice!!, My Pleasure! 
 
Happy Coding!
DevADSDevADS
Hey Suman,

Share your code here & mark it as an answer for this question.

Thanks,
Amit