You need to sign in to do that
Don't have an account?
rishabh rathor 18
how to get email template id in select option
here its my code:-
// Apex controller
public class Filtered_Contacts {
public List<opportunity> oppList{get;set;}
//public string stagename{get;set;}
public Boolean show{set;get;}
public Boolean showbtn{set;get;}
public opportunity opp{get;set;}
public List<WrapperClass> wrapList {get; set;}
public String selectedTemplateName { public get; public set; }
// counstructor
public Filtered_Contacts(){
oppList = new List<opportunity>();
opp = new opportunity();
show =false;
showbtn=false;
wrapList = new List<WrapperClass>();
}
public pageReference fetchopp(){
show =true;
showbtn=true;
system.debug('====='+show);
oppList = New List<opportunity>();
system.debug('====='+show);
for(Opportunity op : [select id, Name , StageName from opportunity where AccountId=:opp.AccountId and StageName=:opp.StageName]) {
wrapList.add(new WrapperClass(op));
system.debug('wrapList'+wrapList);
}
return null;
}
public PageReference sendEmail() {
List<Opportunity> selectedopportunity = new List<Opportunity>();
for(WrapperClass oppt : wrapList) {
if(oppt.selected == true) {
selectedopportunity.add(oppt.opps);
}
}
System.debug('These are the selected opportunities…'+selectedopportunity);
String body = '';
body = '<table border="1" style="border-collapse: collapse"><tr><th>id</th><th>Name</th></tr>';
for(Opportunity oppp : selectedopportunity) {
String url='<a href="'+system.Url.Getsalesforcebaseurl().toexternalform()+'/'+oppp.Id+'"> '+oppp.Name+'</a>';
String name = oppp.Name;
body += '<tr><td>' + url + '</td><td>' + name + '</td></tr>' ;
}
body += '</table>';
system.debug('body '+body);
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
String[] toAddresses = new String[] {'rishabhrathor05@gmail.com'};
system.debug(toAddresses);
mail.setToAddresses(toAddresses);
mail.setReplyTo('rishabhrathor05@gmail.com');
mail.setSenderDisplayName('Salesforce');
mail.setSubject( 'See Link');
mail.setBccSender(false);
mail.setUseSignature(false);
mail.setPlainTextBody('Hello');
// mail.setTemplateId(t.id);
mail.HtmlBody = body;
system.debug('======='+body);
Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
List<Messaging.SendEmailResult> results = Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
if(results[0].success){
ApexPages.addMessage(new ApexPages.Message(ApexPages.severity.CONFIRM, 'Success!'));
}
return null;
}
public List<SelectOption> getMyPersonalTemplateOptions() {
showbtn=true;
List<SelectOption> options = new List<SelectOption>();
options.add(new SelectOption('','-None-'));
for (EmailTemplate t : [select Id,Name,Body from EmailTemplate ])
{
options.add(new SelectOption(t.Id,t.Name));
}
system.debug('========'+options);
return options;
}
public class WrapperClass {
public Opportunity opps {get; set;}
public Boolean selected {get; set;}
public WrapperClass(Opportunity op) {
opps = op;
selected = false;
}
}
}
<!--- vf page--->
<apex:page Controller="Filtered_Contacts" lightningStylesheets="true" sidebar="false">
<script>
function checkAll(cb,cbid)
{
var inputElem = document.getElementsByTagName("input");
for(var i=0; i<inputElem.length; i++)
{
if(inputElem[i].id.indexOf(cbid)!=-1){
inputElem[i].checked = cb.checked;
}
}
}
</script>
<article class="slds-card">
<div class="slds-card__header slds-grid">
<header class="slds-media slds-media_center slds-has-flexi-truncate">
<div class="slds-media__figure">
<span class="slds-icon_container slds-icon-standard-account" title="Opportunity">
<svg class="slds-icon slds-icon_small" aria-hidden="true">
<!-- <use xlink:href="/assets/icons/standard-sprite/svg/symbols.svg#account"></use>-->
</svg>
<span class="slds-assistive-text">Opportunity</span>
</span>
</div>
<div class="slds-media__body">
<h2 class="slds-card__header-title">
<a href="javascript:void(0);" class="slds-card__header-link slds-truncate" title="Opportunity">
<span>Opportunities</span>
</a>
</h2>
</div>
</header>
</div>
</article>
<apex:slds />
<apex:form id="theform">
<div class="demo-only demo-only--sizing slds-grid slds-wrap slds-m-top_large">
<label ><strong> Account Name </strong></label>
<div class="slds-size_1-of-4">
<!-- <apex:outputLabel value="{!$ObjectType.Account.fields['Name'].label}" /> -->
<apex:inputField value="{!opp.AccountId}" />
</div>
<label ><strong> Stage Name </strong></label>
<div class="slds-size_1-of-4">
<apex:inputfield value="{!opp.StageName}" />
</div>
<label><strong> Template Name </strong></label>
<div class="slds-size_1-of-4">
<apex:selectList value="{!selectedTemplateName}" multiselect="false" size="1">
<apex:selectOptions value="{!myPersonalTemplateOptions}"/>
</apex:selectList>
</div>
</div><br/>
<div class="slds-grid">
<div class="slds-size_1-of-12">
<apex:commandButton action="{!fetchopp}" value="Search" styleClass="slds-button slds-button_brand"/>
</div>
<div class="slds-size_1-of-12">
<apex:outputPanel rendered="{!showbtn}" >
<apex:commandButton action="{!sendEmail}" value="Send Email" styleClass="slds-button slds-button_brand"/>
</apex:outputPanel>
</div>
</div>
<div class="slds-p-top_small">
<apex:outputPanel rendered="{!show}">
<table class="slds-table slds-table_cell-buffer slds-table_bordered slds-p-top_small" >
<thead>
<tr>
<th class="" scope="col">
<div class="slds-truncate" >
<input type="checkbox" value="" onclick="checkAll(this,'checkedone')"/>
</div>
</th>
<th class="" scope="col">
<div class="slds-truncate" >Id</div>
</th>
<th class="" scope="col">
<div class="slds-truncate" title="Last Name">Opportunity Name</div>
</th>
<th class="" scope="col">
<div class="slds-truncate" title="Email">Stage </div>
</th>
</tr>
</thead>
<tbody>
<apex:repeat value="{!wrapList}" var="con">
<tr class="slds-hint-parent">
<td><apex:inputCheckbox value="{!con.selected}" id="checkedone"/></td>
<td data-label="Id" >
<div class="slds-truncate" title="Id"><apex:outputField value="{!con.opps.Id}"/> </div>
</td>
<td data-label="Opportunity Name" >
<div class="slds-truncate" title=" Opportunity Name"> <Apex:outputfield value="{!con.opps.Name}"/></div>
</td>
<td data-label="Stage Name">
<div class="slds-truncate" title="Stage Name"> <apex:outputfield value="{!con.opps.StageName}"/></div>
</td>
</tr>
</apex:repeat>
</tbody>
</table>
</apex:outputPanel>
</div>
</apex:form>
</apex:page>
// Apex controller
public class Filtered_Contacts {
public List<opportunity> oppList{get;set;}
//public string stagename{get;set;}
public Boolean show{set;get;}
public Boolean showbtn{set;get;}
public opportunity opp{get;set;}
public List<WrapperClass> wrapList {get; set;}
public String selectedTemplateName { public get; public set; }
// counstructor
public Filtered_Contacts(){
oppList = new List<opportunity>();
opp = new opportunity();
show =false;
showbtn=false;
wrapList = new List<WrapperClass>();
}
public pageReference fetchopp(){
show =true;
showbtn=true;
system.debug('====='+show);
oppList = New List<opportunity>();
system.debug('====='+show);
for(Opportunity op : [select id, Name , StageName from opportunity where AccountId=:opp.AccountId and StageName=:opp.StageName]) {
wrapList.add(new WrapperClass(op));
system.debug('wrapList'+wrapList);
}
return null;
}
public PageReference sendEmail() {
List<Opportunity> selectedopportunity = new List<Opportunity>();
for(WrapperClass oppt : wrapList) {
if(oppt.selected == true) {
selectedopportunity.add(oppt.opps);
}
}
System.debug('These are the selected opportunities…'+selectedopportunity);
String body = '';
body = '<table border="1" style="border-collapse: collapse"><tr><th>id</th><th>Name</th></tr>';
for(Opportunity oppp : selectedopportunity) {
String url='<a href="'+system.Url.Getsalesforcebaseurl().toexternalform()+'/'+oppp.Id+'"> '+oppp.Name+'</a>';
String name = oppp.Name;
body += '<tr><td>' + url + '</td><td>' + name + '</td></tr>' ;
}
body += '</table>';
system.debug('body '+body);
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
String[] toAddresses = new String[] {'rishabhrathor05@gmail.com'};
system.debug(toAddresses);
mail.setToAddresses(toAddresses);
mail.setReplyTo('rishabhrathor05@gmail.com');
mail.setSenderDisplayName('Salesforce');
mail.setSubject( 'See Link');
mail.setBccSender(false);
mail.setUseSignature(false);
mail.setPlainTextBody('Hello');
// mail.setTemplateId(t.id);
mail.HtmlBody = body;
system.debug('======='+body);
Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
List<Messaging.SendEmailResult> results = Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
if(results[0].success){
ApexPages.addMessage(new ApexPages.Message(ApexPages.severity.CONFIRM, 'Success!'));
}
return null;
}
public List<SelectOption> getMyPersonalTemplateOptions() {
showbtn=true;
List<SelectOption> options = new List<SelectOption>();
options.add(new SelectOption('','-None-'));
for (EmailTemplate t : [select Id,Name,Body from EmailTemplate ])
{
options.add(new SelectOption(t.Id,t.Name));
}
system.debug('========'+options);
return options;
}
public class WrapperClass {
public Opportunity opps {get; set;}
public Boolean selected {get; set;}
public WrapperClass(Opportunity op) {
opps = op;
selected = false;
}
}
}
<!--- vf page--->
<apex:page Controller="Filtered_Contacts" lightningStylesheets="true" sidebar="false">
<script>
function checkAll(cb,cbid)
{
var inputElem = document.getElementsByTagName("input");
for(var i=0; i<inputElem.length; i++)
{
if(inputElem[i].id.indexOf(cbid)!=-1){
inputElem[i].checked = cb.checked;
}
}
}
</script>
<article class="slds-card">
<div class="slds-card__header slds-grid">
<header class="slds-media slds-media_center slds-has-flexi-truncate">
<div class="slds-media__figure">
<span class="slds-icon_container slds-icon-standard-account" title="Opportunity">
<svg class="slds-icon slds-icon_small" aria-hidden="true">
<!-- <use xlink:href="/assets/icons/standard-sprite/svg/symbols.svg#account"></use>-->
</svg>
<span class="slds-assistive-text">Opportunity</span>
</span>
</div>
<div class="slds-media__body">
<h2 class="slds-card__header-title">
<a href="javascript:void(0);" class="slds-card__header-link slds-truncate" title="Opportunity">
<span>Opportunities</span>
</a>
</h2>
</div>
</header>
</div>
</article>
<apex:slds />
<apex:form id="theform">
<div class="demo-only demo-only--sizing slds-grid slds-wrap slds-m-top_large">
<label ><strong> Account Name </strong></label>
<div class="slds-size_1-of-4">
<!-- <apex:outputLabel value="{!$ObjectType.Account.fields['Name'].label}" /> -->
<apex:inputField value="{!opp.AccountId}" />
</div>
<label ><strong> Stage Name </strong></label>
<div class="slds-size_1-of-4">
<apex:inputfield value="{!opp.StageName}" />
</div>
<label><strong> Template Name </strong></label>
<div class="slds-size_1-of-4">
<apex:selectList value="{!selectedTemplateName}" multiselect="false" size="1">
<apex:selectOptions value="{!myPersonalTemplateOptions}"/>
</apex:selectList>
</div>
</div><br/>
<div class="slds-grid">
<div class="slds-size_1-of-12">
<apex:commandButton action="{!fetchopp}" value="Search" styleClass="slds-button slds-button_brand"/>
</div>
<div class="slds-size_1-of-12">
<apex:outputPanel rendered="{!showbtn}" >
<apex:commandButton action="{!sendEmail}" value="Send Email" styleClass="slds-button slds-button_brand"/>
</apex:outputPanel>
</div>
</div>
<div class="slds-p-top_small">
<apex:outputPanel rendered="{!show}">
<table class="slds-table slds-table_cell-buffer slds-table_bordered slds-p-top_small" >
<thead>
<tr>
<th class="" scope="col">
<div class="slds-truncate" >
<input type="checkbox" value="" onclick="checkAll(this,'checkedone')"/>
</div>
</th>
<th class="" scope="col">
<div class="slds-truncate" >Id</div>
</th>
<th class="" scope="col">
<div class="slds-truncate" title="Last Name">Opportunity Name</div>
</th>
<th class="" scope="col">
<div class="slds-truncate" title="Email">Stage </div>
</th>
</tr>
</thead>
<tbody>
<apex:repeat value="{!wrapList}" var="con">
<tr class="slds-hint-parent">
<td><apex:inputCheckbox value="{!con.selected}" id="checkedone"/></td>
<td data-label="Id" >
<div class="slds-truncate" title="Id"><apex:outputField value="{!con.opps.Id}"/> </div>
</td>
<td data-label="Opportunity Name" >
<div class="slds-truncate" title=" Opportunity Name"> <Apex:outputfield value="{!con.opps.Name}"/></div>
</td>
<td data-label="Stage Name">
<div class="slds-truncate" title="Stage Name"> <apex:outputfield value="{!con.opps.StageName}"/></div>
</td>
</tr>
</apex:repeat>
</tbody>
</table>
</apex:outputPanel>
</div>
</apex:form>
</apex:page>
So,Try replacing
//mail.setTemplateId(t.id);
to
mail.setTemplateId(selectedTemplateName );
Hope this helps!