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
Shayne0Shayne0 

Select Option to Update a Record

Hey All,

I'm trying to create a select option on a VF page that will allow the user to select an Email Template from a picklist.  Upon selection, I want to populate a text field with the email template ID.  I've built the selector, but can't figure out how to get the id into the text field.

VF Page:
<apex:page standardController="Touch__c" extensions="EmailTemplateSelector">
	<apex:form >
         <apex:selectList value="{!selectedTemplateId}" size="1" rendered="true">
             <apex:selectOptions value="{!myPersonalTemplateOptions}"/>
         </apex:selectList>
     </apex:form>
</apex:page>

Class
public class EmailTemplateSelector {

    private Id recordId;
    
    public String selectedTemplateId {get; set;}
    
    public EmailTemplateSelector(ApexPages.StandardController ctr) {

        recordId = ApexPages.currentPage().getParameters().get('id');
        
    }     
    
    public List<SelectOption> getMyPersonalTemplateOptions() {


        
        List<SelectOption> options = new List<SelectOption>();
        for (EmailTemplate t : [
            select Id,Name 
            from EmailTemplate]) {
                options.add(new SelectOption(t.Id,t.Name));
            }
        system.debug(options);
        return options;
        


        
    }
    
    public void updateTouch(String[] selectedTemplateId){

        For(Touch__c touch : [select id from Touch__c where id =: recordId]){
         
            For(EmailTemplate e : [select id from EmailTemplate where id IN: selectedTemplateId]){

            touch.Email_Template_ID__c = e.id;
            update touch;
            system.debug(selectedTemplateId);
            system.debug(e);
            }

            //return null;


        }
        
    }
    
}

 
Best Answer chosen by Shayne0
Alexander TsitsuraAlexander Tsitsura
Hi Shayne0,

Try this page 
<apex:page standardController="Touch__c" extensions="EmailTemplateSelector">
    <apex:form >
         <apex:selectList value="{!selectedTemplateId}" size="1" rendered="true">
             <apex:selectOptions value="{!myPersonalTemplateOptions}"/>
             <apex:actionSupport event="onchange" reRender="test"/>
         </apex:selectList>
         <br/>
         <apex:outputPanel id="test">
             "{!selectedTemplateId}"
         </apex:outputPanel>
     </apex:form>
</apex:page>

This page demonstate you that template id passed to selectedTemplateId property.

I recomended you avoid soql and dml for loop
public void updateTouch(){
    EmailTemplate[] templates =  [select id from EmailTemplate where id = :selectedTemplateId];
    Touch__c[] toUpdateTouchs = new Touch__c[] {};
    
    For(Touch__c touch : [select id from Touch__c where id =: recordId]){
        For(EmailTemplate e : templates]) {
            touch.Email_Template_ID__c = e.id;
            toUpdateTouchs.add(touch);
        }
    }

    update toUpdateTouchs;
}

As a common practice, if your question is answered, please choose 1 best answer. 
But you can give every answer a thumb up if that answer is helpful to you.

Thanks,
Alex

All Answers

Alexander TsitsuraAlexander Tsitsura
Hi Shayne0,

Try this page 
<apex:page standardController="Touch__c" extensions="EmailTemplateSelector">
    <apex:form >
         <apex:selectList value="{!selectedTemplateId}" size="1" rendered="true">
             <apex:selectOptions value="{!myPersonalTemplateOptions}"/>
             <apex:actionSupport event="onchange" reRender="test"/>
         </apex:selectList>
         <br/>
         <apex:outputPanel id="test">
             "{!selectedTemplateId}"
         </apex:outputPanel>
     </apex:form>
</apex:page>

This page demonstate you that template id passed to selectedTemplateId property.

I recomended you avoid soql and dml for loop
public void updateTouch(){
    EmailTemplate[] templates =  [select id from EmailTemplate where id = :selectedTemplateId];
    Touch__c[] toUpdateTouchs = new Touch__c[] {};
    
    For(Touch__c touch : [select id from Touch__c where id =: recordId]){
        For(EmailTemplate e : templates]) {
            touch.Email_Template_ID__c = e.id;
            toUpdateTouchs.add(touch);
        }
    }

    update toUpdateTouchs;
}

As a common practice, if your question is answered, please choose 1 best answer. 
But you can give every answer a thumb up if that answer is helpful to you.

Thanks,
Alex
This was selected as the best answer
Shayne0Shayne0
Thanks Alex!