If I send an email from my VF page - via inputTextArea, commandButton and MassEmailMessage - and then refresh the page, the email gets sent again. How do I prevent this? Please review my code below.
Markup:
<apex:form >
<apex:pageBlock >
<apex:pageBlockSection columns="1">
<apex:outputLabel value="Message to Selected Customers" style="font-weight:bold" for="ita"></apex:outputLabel>
<apex:inputTextarea id="ita" value="{!customerEmailMessage}" style="width: 550px; height: 111px" required="true"/>
</apex:pageBlockSection>
<apex:pageBlockButtons location="both">
<apex:commandButton value="Send Email" action="{!SendEmail}" onclick="if(!confirm('Are you sure?')){return false};"/>
</apex:pageBlockButtons>
</apex:pageBlock>
</apex:form>
Controller:
public with sharing class ApplicationServerMonitorController {
private ApexPages.StandardController controller {get; set;}
public string customerEmailMessage {get;set;}
public List<Account> selectedAccountsList = new List<Account>();
public void sendEmail() {
Set<Id> acctsIds = new Set<Id>();
if(selectedAccountsList != null && selectedAccountsList.size()>0){
for(Account a : selectedAccountsList){
acctsIds.add(a.Id);
}
}
List<contact> lstcon=[Select Id from Contact WHERE AccountId IN: acctsIds];
List<Id> lstids= new List<Id>();
for(Contact c:lstcon) {
lstids.add(c.id);
}
EmailTemplate et = [Select id from EmailTemplate where name = 'AppServer Monitor Communincation' limit 1];
et.Subject = 'Changed by Apex';
et.body = customerEmailMessage;
update et;
Messaging.MassEmailMessage mail = new Messaging.MassEmailMessage();
mail.setTargetObjectIds(lstIds);
mail.setSenderDisplayName('System Admin');
mail.setTemplateId(et.id);
Messaging.sendEmail(new Messaging.MassEmailMessage[] { mail });
}
}
I've also tried public PageReference sendEmail() {
with both return ApexPages.currentPage();
and return null;
.