1

So I have an action function which calls an apex controller method. I call this action function within some javascript at the top of my page and after that call I want to send some information to Google Analytics based on the results of the method call.

However, when I try to get the variables from the controller, I am getting old data that has not been updated from the action function. I know that the action function is calling the method because the page rerenders and shows the updated information.

Here is the JavaScript from the page which is contained in a function called enterPress

searchFunction(); if({!errorInSearch} == false){ trackButton('myCategory', 'myAction', {!numberOfSearchResults} + ' results returned'); }

Here is the action function for searchFunction( )

<apex:commandLink status="loadStatus" onClick="enterPress(true)" rerender="EntireRelationshipWidget" value="Search"/>
<apex:actionFunction action="{!search}" name="searchFunction"/>

I cannot include the method for search from the controller because it is so long and no one will want to read through it all. Essentially it searches a list of names for ones that you want to see. At the end of the method it will update numberOfSearchResults with the number of names that match the search. If there was an error that occured in the search method (not a long enough search string, etc) then it sets errorInSearch to true.

So to re-iterate my problem...when I search for a name it does send the google analytics code however it sends it to soon because it has the information that was in the numberOfSearchResults and the errorInSearch before the search method executes.

Let me know if there is any more information that can help you solve the problem!

1

So what I ended up doing was utilizing the reRender attribute of my commandLink. So when the page re-renders there is some javascript that will check if a search was successfully executed before hand. If so, then it will do the code for sending Google Analytics the information. This will work because the reRender would occur after the apex method finishes executing so I will always have the current information available.

So in the head of my VF page I have

if({!searchExecuted} && ~{!errorInSearch}){
    trackButton('myCategory', 'myAction', {!numberOfSearchResults} + ' results returned');
}

I have added the variable searchExecuted which is a boolean that is set to true when the search method is called.

Anyone have another solution?

  • 2
    Using oncomplete on the apex:commandLink might be a simpler way to go if you are confident that both the action function and re-render will have completed. – Keith C Oct 22 '14 at 16:56
  • Oh that would have been much simpler. I will probably change my code back to that because then I won't need the extra variable. Thanks for the advice! – Louis Pujol Oct 22 '14 at 17:50
  • The answer posted above will work only if the page reloads. What if it needs to be done when only particular sections rerender. Any ideas ? Tried oncomplete and that did not help. – chinmay bhusari Mar 7 '16 at 11:41

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.