I have a pageblock table. Upon click the add button, it will trigger the add() method on my controller and return the boolean 'BO' value, I would like to check if boolean is true, there will be a javascript display which will prompt user to continue. However, I'm unable to display the msg. Why is that so? But the msg will be displayed when I remove the if('{!BO}' == true).. Is there something wrong with my if statement? Anyone?


            function confirmBO(){
                if('{!BO}' == true){
                  return confirm('Are you sure you want to continue?');

                 <apex:commandButton action="{!add}" value="Add" oncomplete="if(!confirmBO()) return false;"/>

  • Try doing an alert({!BO}); before your if statement, so that you can see the value you are comparing to true. – TC Sutton Feb 19 '15 at 17:51
  • You are leaving yourself vulnerable to XSS by merging in the value that way. Always use JSENCODE or a similar encoding function to protect yourself! – Adrian Larson Feb 19 '15 at 17:59
  • @TCSutton ive tried your method. it display the value.. but it does'nt go inside the if block – maherz_91 Feb 19 '15 at 18:02
  • @AdrianLarson I'm sorry i dont und what you mean, btw im new to salesforce. – maherz_91 Feb 19 '15 at 18:03

Its just a matter of data types... I guess it will work in either way: if you remove the quotes around your boolean variable or if you write true in quotes

  • That should only be the case with === no? Which should be used in this case anyway. – Adrian Larson Feb 19 '15 at 19:07
  • @AdrianLarson === operator do not perform type conversion. It performs a rather more restrictive check. alert('true' == true); // false alert('true' === true); // false alert(true == 1); // true alert(true === 1); // false – CloudConnect Feb 20 '15 at 18:28
  • Further clarification could be found at: stackoverflow.com/questions/359494/… – CloudConnect Feb 20 '15 at 18:30

I think the problem as to do with the incorrect use of the "oncomplete" property, which is intended for use with ajax calls and seems out of place. I don't believe you ever get to the point where your oncomplete handler is invoked -- that is why you never see the confirm dialog.

If you want the prompt to appear before you take an action, you should probably call your confirm() javascript method in an onclick() handler instead.

For example:

<apex:commandButton action="{!add}" value="Add" onclick="return confirmBO();" />

I would also update the confirmBO method to always return either true or false depending on if you want the operation to proceed by default or not. I presume the BO controller method returns a boolean variable.

function confirmBO(){
        return confirm('Are you sure you want to continue?');
    } else {
        return false;  // do not invoke action by default

The onclick handler will invoke first and the apex action will only be fired if the onclick handler/logic returns true. I believe this is your desired behavior.

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.