function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
SFDC NBSFDC NB 

Confirm before saving

Hello,

I'm new to Apex, I'd like to know how can we have a user confirm Yes/No and save the record based on user input.

e.g.

User creates new contact and clicks save, if there is another contact with the same email address we'd want a confirmation box to pop up and say that this is a duplicate if they'd still like to continue.
If the user clicks yes, the contact is created/saved. If the user clicks no the record is not save.




Gaurav NirwalGaurav Nirwal
<apex:commandButton value="Remove All" action="{!removeAll}" rerender="form"
     status="LoadingStatus" onclick="return confirm('Are you sure?');"/>
Gaurav NirwalGaurav Nirwal
<apex:actionFunction action="{!removeAll}" name="removeAll" rerender="form" status="LoadingStatus"/>
<script>
function confirmRemoveAll() {
    if(confirm('Are you sure?')) {
        removeAll();
        return false;
    }
    return false;
}
</script>
<apex:commandButton value="Remove All" onclick="return confirmRemoveAll();"/>
Gaurav NirwalGaurav Nirwal
Apex code executes at server, not at client so showing dialog boxes cannot happen from Apex. For that reason you will either have to move the validation to the client (using javascript onclick handler) or you will have to split the saving process in two Ajax calls to server, one to validate the other to save and then use the intermediary validation result to optionally show dialog box. The choice depends on the complexity of the validation (validateSearchItems)
Gaurav NirwalGaurav Nirwal
<script type="text/javascript">
    jQuery.noConflict();

    jQuery(document).ready(function() {

        jQuery('.saveAction').click(function(e) {
            if ( confirm('Are you sure you want to save? Click OK to continue saving.') ) {
                return true;
            }
            e.preventDefault();
        });
    });
</script>

<!-- somewhere on your page is your button -->
<apex:commandButton action="{!save}" value="Save" styleClass="saveAction"/>
Gaurav NirwalGaurav Nirwal
document.getElementsByName('inlineEditSave')[0].onclick = function(){
if(accstatus.value=='Locked'){
   var userchoice =confirm('Are you sure you want to lock the Account?');
   if(userchoice){sfdcPage.save(); return userchoice;}
   else{sfdcPage.revert(); return userchoice;}
}
else{sfdcPage.save(); return userchoice;}
};
Gaurav NirwalGaurav Nirwal
If my answer can solves your problem Please select as best answer
Gooch ForeverGooch Forever
@SFDC NB

You need to create custom VF page and Controller to save Conact records. then override your VF page on standard page.
For more visit https://www.salesforce.com/us/developer/docs/pages/Content/pages_quick_start_tabs.htm