[SalesForce] Apex Command Button using action and onclick not working

What I'm trying to do is have an Apex command button that on the click opens a new VF page and also, have an action that does some backend query to get data ready for the popup VF page.

Not sure why this isn't working. Anyways, here's my code :

These next two blocks of code are located on the same VF page FYI :

<apex:outputPanel>
   <apex:inputText required="true" label="Source"/>
      <!--<input type="button" onclick="openPopup()" value="open popup"/>-->
      <apex:commandButton value="open popup" onclick="openPopup()" action="{!executeQuery}"/>
</apex:outputPanel>




 <script type="text/javascript">
    var newWindow = null;
    function openPopup() {
      newWindow = window.open('OMTCustomPage2', 'Popup', 'height=500, width=500');
    }

    function closePopup(object,name) {
      if(newWindow!=null)
         newWindow.close();
    }
 </script>

And this is in the 2nd VF page which is the popup :

 <script type="text/javascript">
    var newWindow = null;

    function openPopup() {
      newWindow = window.open('OMTCustomPage2', 'Popup', 'height=500, width=500');
    }

    function closePopup(object,name) {
      if(newWindow!=null)
          newWindow.close();
    }
</script>

Best Answer

You command button is actually running the action method in it's own onclick handler (you would normally not define an onclick handler on a command button).

Try this instead:

<apex:commandButton value="open popup" oncomplete="openPopup()" action="{!executeQuery}"/>

What this does is run your back-end query, then open the popup. You can't easily do this asynchronously.

Make sure that your return value for this controller function returns either void or a null PageReference.