0

I'm following this tutorial by Jeff Douglas on creating a real time search VF page.

When a user types into the search box, it fires an apex:actionFuncion VF component. The function it calls from the controller is getting a null value from the input box. Why is that?

VF page

<apex:page controller="debugPageController">
   <apex:form>

    <script type="text/javascript">
        function doSearch() {
            console.log('doSearch is firing');

            console.log("Input value: " + document.getElementById("search-box").value);

            searchServer(
                document.getElementById("search-box").value
            );
        }
    </script>

        <apex:actionFunction name="searchServer" action="{!runSearch}">
            <apex:param name="search-box" value="" />
        </apex:actionFunction>

        <input type="text" id="search-box" value="" onkeyup="doSearch();"/>

    </apex:form>
</apex:page>

Controller

public class debugPageController {

    public PageReference runSearch() {
        String searchBox = Apexpages.currentPage().getParameters().get('search-box');
        System.debug('searchBox >>>>> ' + searchBox);
        return null;
    }

}
1

Try adding the rerender attribute to your apex:actionFunction. For some reason the actionFunction won't take parameters without this attribute.

Also, you can't have a dash (-) in the name of your apex:param. So, instead of search-box, rename it to searchBox for example.

<apex:actionFunction name="searchServer" action="{!runSearch}" rerender="">
     <apex:param name="searchBox" value="" />
</apex:actionFunction>

Also, update your controller method to retrieve the parameter with a new name.

public PageReference runSearch() {
    String searchBox = Apexpages.currentPage().getParameters().get('searchBox');
    System.debug('searchBox >>>>> ' + searchBox);
    return null;
}
  • get an "ReferenceError: Can't find variable: searchServer" when doing that. – Tyler Zika Oct 20 '16 at 19:38
  • @TylerZika Please see my updated answer. – smukov Oct 20 '16 at 19:45
  • "Unable to post message to na39.salesforce.com. Recipient has origin null." I did update my post with your suggestions, Thank you. – Tyler Zika Oct 20 '16 at 19:49
  • 1
    You probably clicked the 'Preview' button in your Developer Console to open your VF page, correct? This adds the ?core.apexpages.request.devconsole=1 to the end of your URL. Remove this from the URL and the error should go away. Your users won't have this parameter in their URL in any case, since this is only being added by Dev Console. – smukov Oct 20 '16 at 19:53

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.