I am calling vf page on button which will send email.Email method is executing from <Apex:page> action.

My requirement is display confirm box on custom button.

I am trying to add <apex:messages/> in my code but not working. Can someone help me on this?

<apex:page action="{!sendEmail}" extensions="OrderConfirmationEmail" standardController="Opportunity"> 

closed as unclear what you're asking by Sebastian Kessel, Reshma, battery.cord, codeyinthecloud, glls Jan 11 at 15:13

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.


There's two different interpretations of your question

Confirm box to ask if the user wants to actually send the message

Based off of the code you provided, there is no way to do a confirmation box.

Instead, you should use an action method to call your apex code and some JS to display your confirm box:

<!-- Remove the action from the page element -->
<apex:page extensions="OrderConfirmationEmail" standardController="Opportunity"> 
    <-- action functions must be within a form -->
        <!-- Create the function that will call your action -->
        <apex:actionfunction name="sendEmail" action="{!sendEmail} />
        // Call your action function on page load
        (function() {
            if (confirm('Do you want to send the email?')) {
                sendEmail(); // This method must match the "name" attribute
                             // of your action function above

Note: I would also recommend trying to use jquery and document.ready to make sure the method is called at the correct time.

Send a "success" message after the email is sent:

To show a success message you need to add code into your send email method in apex:

public class OrderConfirmationEmail {
    public ApexPages.standardController stdCon {get; set;}
    public Opportunity opp {get; set;}

    public OrderConfirmationEmail(ApexPages.standardController stdCon) {
        this.stdCon = stdCon;
        this.opp = getOpp(stdCon.getId());

    public void sendEmail() {
        List<Messaging.SingleEmailMessage> messages = new List<Messaging.SingleEmailMessage>();

            Build Email

         Messaging.sendEmaiLResult result = messaging.sendEmail(messages);

         if (result.isSuccess()) {
             ApexPages.addMessage(new ApexPages.message(
                 'Email is Successful'
         } else {
             ApexPages.addMessage(new ApexPages.message(
                 'Email failed'

    private Opportunity getOpp(Id oppId) {
        return [
            SELECT Id, CustomField__c
            FROM Opportunity
            WHERE Id = :oppId

Call this function in Button onclick

function myFunction() {
  var txt;
  if (confirm("Press a button!")) {
    //Your Process
  • 2
    Again, how does this solve the problem in the question? Go into more detail. – Derek F Jan 11 at 12:49

Not the answer you're looking for? Browse other questions tagged or ask your own question.