Here is an odd bug a reader ran into this weekend involving ColdFusion 8's new autosuggest feature. It was difficult to debug but ended up being quite trivial.

The reader was not using an Ajax call to load the data for the suggestion list. Instead he performed a query, used valueList twice, and supplied those lists to two autosuggest cfinput fields.

One worked fine, but the other threw a JavaScript error. The problem ended up being his data. His list included a record that had double quotes in it. Here is a simple example:

<cfset list = "apple,orange,peaches ""and"" cream,klingon birds of prey">

In this list, the 3rd item, peaches "and" cream, includes quotes. Notice that I've escaped the quotes for ColdFusion to parse it correctly. When you run a page that makes use of this...

<cfform name="foo"> <cfinput name="test" autosuggest="#list#"> </cfform>

You end up with this generated in the browser:

_cf_autosuggestarray="apple,orange,peaches "and" cream,klingon birds of prey".split(",");

What's sad about this is that ColdFusion could easily handle this for you. In fact, it provides multiple options. One, toScript, handles the quotes just fine:

<cfset list = "apple,orange,peaches ""and"" cream,klingon birds of prey"> <cfoutput> <script> var #toScript(list,"mylist")#; </script> </cfoutput>

This returns:

<script> var mylist = "apple,orange,peaches \"and\" cream,klingon birds of prey"; ; </script>

See how it is escaped? This bug doesn't exist if yo use Ajax to load the data. If I change my input field to this:

<cfinput name="test" autosuggest="url:test3.cfm?v={cfautosuggestvalue}">

And then simply do:

<cfset list = "apple,orange,peaches ""and"" cream,klingon birds of prey"> <cfoutput>#serializeJSON(list)#</cfoutput>

It works fine. Something to watch out for folks! (And I'm going to file a quick bug report now.)