1

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;.

  • how are you refreshing the page? Is that through some rendering? – Santanu Boral Aug 20 '18 at 18:34
  • Just the browser refresh button. – Gabriel Rivera Aug 20 '18 at 18:50
  • I usually use a history object (either custom, or the standard activity created from the mail) and check that before sending mail to prevent duplicate sends for emails campaigns or client based mail. – battery.cord Aug 21 '18 at 13:43
1

One of the quick trick is to make the customerEmailMessage = null after sending the email as you have mentioned it required="true" in VFP.

So, it will not send an email during refresh for sure.

public void sendEmail() {

      //all your existing code and after sending email make customerEmailMessage as null
      Messaging.sendEmail(new Messaging.MassEmailMessage[] { mail });
      customerEmailMessage = null; //add this line.
      selectedAccountsList = new List<Account>();
}

Secondly, after sending email, you can put a extra URL parameter to the page and verify that just before sending an email. If parameter is available then mail already sent and you can ignore rest of the code. In this case, return type will be PageReference of sendEmail() method.

  • I should've mentioned that I had tried that too, but the email oddly still gets sent again. – Gabriel Rivera Aug 20 '18 at 18:50
  • are you sending email on page action? It is still not clear how the required field is filled up and send email is automatically get called – Santanu Boral Aug 20 '18 at 18:54
  • check my updated answer including the second point. – Santanu Boral Aug 20 '18 at 19:10
  • I'm sorry; I don't quite follow. Would you mind editing the code in your answer to show me what you mean? I even set customerEmailMessage = null in the constructor and the email still gets sent again. – Gabriel Rivera Aug 20 '18 at 20:05
0

I added an id to the pageBlock with the button and now have the button rerender it. The email is no longer resent when I refresh the page:

<apex:pageBlock id="pb">
    <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};" rerender="pb"/>
     </apex:pageBlockButtons>
</apex:pageBlock>

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.