+ Start a Discussion

Dynamic Search with apex:inputText - Sleep Method Workaround Needed

So I have an inputText component on my Visualforce page that dynamically edits my SOQL query for every onkeyup and also rerenders a table whose values are based upon the SOQL query. My issue is best explained with an example:


If a user wanted to search for "Salesforce" he/she would obviously type out S, then a, then l, etc. Since the SOQL query is edited with each keyup the table below continually rerenders as the user types. In certain scenarios such as this one, this can be a major problem.


Assume that a search term for Salesforce would return no records, leaving the table empty. However, a search of Sales would return many records, say 350.


Once the user finishes typing the second 's' the "Sales" query begins accumulating several records, but the user is simultaneously typing new letters-f, o, r, etc. Each of these remaining letters lead to SOQL queries that return 0 results (Salesf gives 0 results, Salesfo gives 0 results, etc.) and thus are completed quickly. The final result is that even though the ultimate search term "Salesforce" should return 0 records, the table is actually populating with the results of the search term "Sales" because, given the high number of pulls from the database, this SOQL query actually completed after the other SOQL queries finished.


I have found solutions for a similar issue with AJAX such as the one here. The problem is that these all require utilizing Javascript and the setTimeout function to cause the server to pause before searching the database (the assumption here is that this allows the user to finish typing his/her ultimate search term). Unfortunately, Salesforce seems to ignore the setTimeout javascript function as any sort of sleep/wait method could potentially throw off their multi-tenant architecture.


Does anyone have any good ideas I could utilize to overcome this issue that don't involve some sort of a sleeper method? Note: I have already noted that if I add a limit to my SOQL query this issue can be resolved, but unfortunately my client demands there be no limit size to the search results...


If anyone comes across this same issue I recommend you recheck your code-upon a second glance I realized I was not properly calling my javascript function; after fixing this the .setTimeout javascript method worked perfectly.