Ask a Jedi: Writing for both ColdFusion 6 and 7

A reader asks:

I have a CF app that needs the codebase to be able to run on both CF6.1 and CF7. The function queryNew() can take 2 params in CF7, but not CF6.1. How can I code round this? I have tried this, but it doesn't work: qMyQuery = false;
iCFMajorRelease = listFirst(server.ColdFusion.ProductVersion,",");
if(iCFMajorRelease LT 7){
qMyQuery = queryNew("oneColumn,twoColumn");
} else {
qMyQuery = queryNew("oneColumn,twoColumn","CF_SQL_VARCHAR, CF_SQL_VARCHAR");
}

So as he probably discovered - this code will throw an error when run on ColdFusion 6, even though, in theory, the code doesn’t run. The problem is that the CFML is getting compiled (or interpreted, I always forget the right term) and CF6 simply won’t like the syntax used in the second cfif branch. I haven’t seen this before with functions, but have seen it in the past when trying to use a tag that didn’t exist in a previous version. You can see this for yourself in CFMX7 by doing the following:

<cfif 0> <cfset foo = queryNew("o","cf_sql_varchar", "9")> </cfif>

So how do you get around this? By cfincluding. If you move the code that will only run in CFMX7 to a cfinclude, than CFMX6 will not have a problem. (As long as you don’t accidently screw up your cfif conditional.) For an example of this - see how I do charting in Galleon. In order to support BlueDragon, I moved the charting to it’s own include file.

Raymond Camden's Picture

About Raymond Camden

Raymond is a developer advocate. He focuses on JavaScript, serverless and enterprise cat demos. If you like this article, please consider visiting my Amazon Wishlist or donating via PayPal to show your support. You can even buy me a coffee!

Lafayette, LA https://www.raymondcamden.com

Comments