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
wt35wt35 

apex:commandButton : Confirm pop-up box before proceeding

 

Hi community,

 

I have a button on which I want to include a warning message, that is, when the user clicks on the button, a message appears asking if he/she wants to continue...

 

This is where I have gotten so far, when I click on the button I do have the message popping up and asking if I want to continue, however the change() action is called even if I close this window...I want the processing to take place only if the user clicks OK...

 

Thanks you!

 

 

<apex:page controller="HelloWorld">

<apex:form >
<apex:commandButton value="Warning" onclick="javascript&colon;window.alert('Do you want to proceed?');" action="{!change}"/>
</apex:form>

</apex:page>

 

Best Answer chosen by Admin (Salesforce Developers) 
Puja_mfsiPuja_mfsi

Hi,

You need to add return infront of confirm.So when u click on ok it returns true and call action method otherwise do nothing.

 

uor code replace with the following:

 

 <apex:commandButton value="Warning" onclick="return confirm('Do you want to proceed?');" action="{!sort}"/>

 

Please let me know if u have any query on same and if this post helps u please throw KUDOS by click on star at left.

All Answers

digamber.prasaddigamber.prasad

Hi,

 

Instead of using window.alert('Do you want to proceed?'), use window.confirm(''Do you want to proceed?') and your problem should be solved.

 

Let me know if you have any question about above.

 

Happy to help you!

wt35wt35

The issue remains the same.

wt35wt35

 

 

After some digging, it seems I need to use the actionFunction tag to perform validation first in JS and then launch my controller method.

 

However it seems my controller method is not called...

 

VF:

 

<apex:page controller="HelloWorld">

<apex:form >

<apex:actionFunction name="myChange" action="{!change}" rerender=""/>
<apex:commandButton value="Warning" onclick="javascript&colon;confirmation();"/>

</apex:form>

<script>
function confirmation() {
     myChange();
}
</script>


</apex:page>

 

Apex method:

 

 

   public PageReference change(){
      System.debug('#######WORKS');
      PageReference pageRef = new PageReference('/apex/HelloWorld');
      pageRef.setRedirect(true);
      return pageRef;
      
   }

 

Puja_mfsiPuja_mfsi

Hi,

You need to add return infront of confirm.So when u click on ok it returns true and call action method otherwise do nothing.

 

uor code replace with the following:

 

 <apex:commandButton value="Warning" onclick="return confirm('Do you want to proceed?');" action="{!sort}"/>

 

Please let me know if u have any query on same and if this post helps u please throw KUDOS by click on star at left.

This was selected as the best answer
Puja_mfsiPuja_mfsi

Hi,

No need to use action function i'll provide u the solution above uor latest post please try the same.

wt35wt35

Simple and efficient.

Thanks!

wt35wt35

Puja, not sure if you still can help me but now I implemented this logic into my real VF Page (which much more advanced that the frist example) and it is not working... I am wondering if it is because I have added an apex:param inside the button:

 

<apex:commandButton value="Mark As Complete" onclick="return confirm('test');" action="{!markPillarAsComplete}" reRender="Merchant_Introduction_Engagement_Section">
                        <apex:param name="pillarName_MarkAsComplete" value="Merchant Intro Engagement" assignTo="{!pillarName_MarkAsComplete}" />
</apex:commandButton>

 

When I click on my button I do get a confirmation pop-up, however the Apex action is not executed....

Puja_mfsiPuja_mfsi

Hi,

Could u please check in debug log whether the method is call or not.because u have used reRender attribute so the page is not reloaded .So you need to add a debug in method and check in debug log 

 

 

wt35wt35

I made an additional test in my test page with an apex:param included and it still works so the issue seems to lie somewhere else:

 

<apex:form >
<apex:commandButton onclick="return confirm('test');" value="Warning" action="{!change}" >
<apex:param name="myParam1" value="test" assignTo="{!s}" />
</apex:commandButton>
</apex:form>

 

wt35wt35

Puja,

 

I haven't checked my debug log yet, but I can confirm this is because of rerender.

When I remove "rerender"  the warning and action work well.

But now I have a dilemma:

 

Basically I have a section that show some records  and my "Complete" button.

When clicking on that button, the action method marks all these actions as Complete and returns a PageRefefernce so that a full page refresh is done. The problem is that the updated actions do no display if I don't rerender the appropriate section (which I never understood since it is a full page refresh, hence a rerender should not be needed).

 

Now the business asks for a warning pop-up before proceeding, which I can implement thanks to your solution (using onclick="return confirm..."

 

But now I need to find a solutiuon where I can use onclick and rerender at the same time...

 

Do you have any ideas?

Thanks!

 

 

Steve Harris 34Steve Harris 34

Don't just return the results of confirm(), because if you return any value from the onclick handler it will bypass the on page action where you are rerendering. Instead test for results of confirm() and only return false if it fails, don't return anything otherwise and the handlers will continue execution.

This works whether you have "rerender" or not:

<apex:commandButton value="Warning" onclick="if (!confirm('Do you want to proceed?')) return false;" action="{!sort}" rerender="updateArea" />