I have the following commandButton which runs the javascript function 'sendEmails'. This runs an ApexFunction which runs apex code to create and send an email from the controller. I need to add in a 'Confirm you want to send this email' modal dialog though to prevent accidental sending of emails. So the function should only be executed if the confirmation box OK is clicked. How do I do this please?

<apex:commandButton value="Send" rendered="{!showEmail}" 
     onClick="sendEmails();return false;"
     styleClass="slds-button slds-button_neutral" />

If I replace the 'onClick' event with:

onClick="if(!confirm('Are you sure you want to send the email?'));return false;"

then how do I get it to run the 'sendEmail()' function?

My javascript with the actionFunction it calls is:

function sendEmails(){                
   var temp = document.getElementsByClassName('emailbody')[0].id;
   var ckeditorBody = CKEDITOR.instances[temp].getData();

<apex:actionFunction name="CallSendEmail" action="{!sendEmail}" rerender="errorMessageId">
    <apex:param name="Richbody" value="" assignTo="{!body}"/>

You can do it in two way

  1. Add your method to action and add onclick method

    onClick="if(confirm('Are you sure you want to send the email?')){return true;}else{return false;}"

  2. You can call action function

    onClick="if(confirm('Are you sure you want to send the email?')){CallSendEmail();}"

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.