Take the 2-minute tour ×
Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. It's 100% free, no registration required.

I have a Visualforce page with a custom link that calls a function in the page's controller extension. Before that function gets called, I'd like to use JavaScript to pop up an confirmation dialog box along the lines of "You have chosen to mark this item as resolved. Your name will be added to the comments. OK/Cancel". If OK is clicked, we proceed to the Apex function call. If Cancel is clicked, nothing happens.

I've found this example:

Custom JS confirm button for IF

But it looks to me like it will always call the Apex function. My understanding of what's happening is likely incomplete.

I'm not using any JavaScript packages at present. I'd like to keep things that way if possible.

Here's the code I started with. It's a trimmed down version of what I linked to and gets accessed by adding the following to the link onclick="return showConfirmation();"

<apex:outputPanel id="sendScript">
    <script type="text/javascript">
        function showConfirmation() {
            return confirm('All selected items will be marked resolved. Your name will be added to the comments.');
        }
    </script>
</apex:outputPanel>
share|improve this question
    
May help if you include your relevant VF code and what you've already tried. –  Samuel De Rycke Mar 20 at 16:29
add comment

1 Answer 1

up vote 7 down vote accepted

The answer that you found is OK. You could try this:

<apex:commandLink onclick="return confirm('You have chosen to mark this item as resolved. Your name will be added to the comments. OK/Cancel');" action="{!yourMethod}" value="Call your method"/>

If you return a false onclick the method will not be called.
The confirm function returns true or false (OK or Cancel).

share|improve this answer
    
Thank you! That works and it is simpler than the example I linked to. The only thing I'm having trouble wrapping my head around is why "YourMethod" doesn't get called when "cancel" is chosen. –  Jagular Mar 20 at 17:09
1  
In that case, the confirm method returns a false, when the event onclick returns a false the event "propagation" stops. That even calls yourMethod from the browser to the server (were your controller is). –  Martin Borthiry Mar 20 at 17:37
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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