Variable assignment in loop

I had to debug something quite subtle yesterday and learned something in the process.

Spent quite some time on this problem. Perhaps if my ColdFusion fundamentals were stronger, the problem should be identified easily.

The problem was this, in a cfloop query, the code was meant to overwrite the value from the query for a given condition.
Supposed you have this query below:


#FieldID# #FieldLabel# #variables.FieldLabel#

So as you can see we are looping over a query, but for one particular condition, in this case: if the fieldLabel column called job type, we want to output something different.

The result was this table:

169 Job Type this is overwritten
170 Location this is overwritten
171 Industry Sector this is overwritten
172 Job Duration this is overwritten
173 Job Start Date this is overwritten
174 Salary this is overwritten
896 Account to use this is overwritten

The bug was, our application was reffering to FieldLabel unprefixed by the scope, as you can see from the result, it will always getting the value from the qAllActiveFields (as from scoping from a view the query scope has precedence over variables scope).

Very subtle bug, but can really be so easily avoided by doing any of the folllowing:

  • Never set variable name inside a loop as one of the column name of that query
  • Prefix correctly