+ Start a Discussion
Ines CostaInes Costa 

Calling an Apex method from inside a Visualforce tab <apex:commandButton>

In case someone has the same problem for the future I do believe this is a bug and this is the workaround that I found. 

I had a button that performed a search in a standalone visualforce page and it was working without any issues. From the button I could see it working and refreshing the pageblock and calling the function in the debug logs. I then decided to embed it in a visualforce tab panel and it stopped working - page block would refresh but no reference to my function/system debugs in the debug logs. 
But I found a workaround that works:

My initial button inside the tab panel/tab:
<apex:commandButton styleClass="search-block-button" value="Search" action="{!SearchAccountProspect}" rerender="TablePanelProspect" status="TableUpdateStatus" />

Workaround - instead of using the action and calling the function there you can use the onclick and call a action function inside Visualforce:

<apex:commandButton styleClass="search-block-button" value="Search Prospects" onclick="SearchProspect(); return false;" rerender="TablePanelProspect" status="TableUpdateStatus" />

<apex:actionFunction name="SearchProspect" action="{!SearchAccountProspect}" rerender="TablePanel" status="TableUpdateStatus"/>
(placed just after the apex:form tab - outside the apex:TabPanel tag.
bob_buzzardbob_buzzard
Your actionfunction is rerendering TablePanel but your commandbutton was rerendering TablePanelProspect - what happens if you remove the onclick handler from the commandbutton and rerender TablePanel?
Ines CostaInes Costa
I got the same result from my tests with and without the onclick. And even with the actionFunction it appears that only the SearchAccountProspect() function gets called and it doesn't instantiate the class which means that all the controls I had on the page (pagination for number of results to avoid the view State limitation on the number of records.

I tried 4 solutions:-
1- separate VF page that has a search functionality with a commandButton = everything works fine - search results ok / sorting works / pagination works
2- same exact code but the VF page is now part of of tabpanel with a commandButton to search = search and other functionalities stopped working - no reference in the debug logs to any call to the Apex class/functions
3- same exact code but with the actionFunction calling a javascript to call the search functionality = search and other functionalities stopped working - debug logs show that the search function is called but since it doesnt instatiate the constructor etc the results page doesn't work - no sorting, pagination etc 
4- same page as attempt #1 but now in the tabPanel inside an IFRAME - everything works fine - search results ok / sorting works / pagination works - AND goes around the view state limitations since its running separate pages individually
bob_buzzardbob_buzzard
FYI - actionfunction still posts the viewstate back to the server - if you want a stateless solution you'd need to use JavaScript remoting.
Ines CostaInes Costa
I know :) Unfortunately I'm not very good with javascript so my solution was to separate the pages and use iframes to have everything on the client size. Not ideal but unfortunately I don't have time to learn JavaScript in the near future.