Requirements    
Prerequisite knowledge
Required products
User level
Familiarity with ColdFusion.
Adobe ColdFusion Enterprise Edition (2016 release) (Download trial)
All

 

For close to a decade and a half, ColdFusion has constantly raised the application development productivity bar with each new release. This does not happen by accident. In fact, in planning each new version, the ColdFusion product team meets with numerous customers and partners to ascertain exactly how the product is used, so as to be able to focus on improvements that have real value for users. This is how innovative features like reporting and event gateways and server monitoring become a reality.
And ColdFusion 9 is no exception. This new ColdFusion release boasts an impressive array of new and enhanced features, all intended to improve productivity for developers, IT administrators, and business decision makers alike.
In this article I'd like to introduce you to some of what's new and exciting in ColdFusion 9.

 
ColdFusion functionality exposed as services

Have you ever stopped to think about just how much functionality is baked into ColdFusion? We use <CFQUERY> to work with databases, allowing for highly flexible and dynamic SQL as well as query caching and more, and this is powered by a sophisticated internal engine. We use tags like <CFCHART> and expect charts to be generated and displayed, without ever really paying attention to the fact that an entire Java-based charting engine is built in and actually doing the heavy lifting. The same is true for <CFPDF>, <CFIMAGE>, <CFSEARCH>, support for SOAP and Web Services, XMPP and JMS integration, and so much more. While most of us focus on CFML the language, the truth is that the bulk of ColdFusion, the majority of what gets installed, is not the language but the extensive array of integrated services, services that are exposed to ColdFusion via CFML tags and functions.
But what if these services could be accessed outside of ColdFusion? If a PHP developer in the next cube over needed to merge PDF files, why couldn't he invoke the PDF manipulation services in ColdFusion? If a .NET developer needed to access Microsoft Exchange, why couldn't she use the brilliant Exchange tags in ColdFusion (rather than having to write lots and lots of .NET code, and I do mean lots and lots)? What about the Java developer who needs to easily manipulate spreadsheet files without tinkering with low level libraries?
And while we're at it, what about the Flex developer who needs to generate an e-mail message? Flex (well, Flash) has no built-in SMTP libraries, and so Flex developers who need to programmatically generate e-mail messages do so by writing code on the server. For ColdFusion developers this means creating a ColdFusion Component which accepts data from a Flex application (likely via an AMF call) and then passes that same data to a <CFMAIL> tag. In other words, code is being written on the server just to be able to pass data from Flex on the client to the <CFMAIL> tag. So why couldn't a Flex developer just invoke <CFMAIL> directly, passing it name=value pairs so it can generate an e-mail?
Well, with the upcoming ColdFusion 9, the answer to all of these questions is yes. These are all doable! In ColdFusion 9 we're exposing lots of those integrated ColdFusion services via AMF (Flash Remoting) and SOAP (Web Services). The PHP, .NET, and Java developers can invoke ColdFusion built-in Web Services, pass in data, and get back results. And the Flex developer can include a ColdFusion SWC file exposing ActionScript classes and MXML tags via simple abstracted AMF calls. Simply include the SWC in your Flash Builder project, and define the ColdFusion name space like this:
 
<mx:Application xmlns:cf="coldfusion.service.mxml.*">
and you'll have access to CFML tags within your Flex project. For example, to send an e-mail you could use the following:
 
<cf:Mail id="cfMail" to="{to.text}" from="{from.text}" subject="{subject.text}" subject="{subject.text}" content="{body.text}" type="html" />
The above code creates an instance of the Mail object and names it "cfMail", and sets to, from, subject, etc., with the values of other Flex objects. To actually send the e-mail all you'd need is to invoke the following (possibly when a Send button is clicked):
 
cfMail.execute();
There is much more to this "ColdFusion as a Service" functionality, including lots more services exposed, and a sophisticated security model. But the bottom line is that ColdFusion is now poised to become even more valuable to Flex and AIR developers, and now even of value to developers using other platforms and languages.

 
Executing code on server startup

ColdFusion can execute code when an application starts and ends, when a session starts and ends, when a request starts and ends, and more. But what if you need to execute code when a server starts up? Granted, this is not a common use case, but when needed, we've had to use onApplicationStart for this, probably doing something like:
 
<cfif not IsDefined("SERVER.myVar")> ... </cfif>
ColdFusion 9 adds the ability to define code to be executed onServerStart. By default this is a method named onServerStart in server.cfc. But, actually, it can be in any ColdFusion Component, including an Application.cfc. In ColdFusion Administrator you can point to the CFC and method to be invoked, and ColdFusion will execute the code on server start-up before processing any requests.

 
Working with spreadsheets in ColdFusion

Spreadsheets are key to just about all businesses and organizations, and ColdFusion developers have long sought a way to access and manipulate spreadsheet data programmatically. The truth is, ColdFusion has supported spreadsheet access for a while in a variety of ways—it's been possible to access Excel files from an ODBC driver, and it's been possible to generate these files using CFReport as well as by generating HTML or CSV content and then setting the appropriate MIME type to force the browser to display the data using Excel.
But ColdFusion developers have been asking for more—greater and more control, and the ability to read and write specific parts of spreadsheet files. And as I demoed in my usergroup presentations last week, this is indeed planned for ColdFusion 9.
Just like we did with the ColdFusion image manipulation functionality, spreadsheets in ColdFusion are manipulated using a tag or functions, or some combination thereof. The <CFSPREADSHEET> tag is used to read a sheet from a spreadsheet file (as a spreadsheet object, a query, a CSV string, or HTML), write sheets to XLS files, and add sheets to XLS files. The over 30 supporting functions like SpreadsheetSetCellValue(), SpreadsheetAddRow(), and SpreadsheetSetCellFormula() allow for more granular spreadsheet manipulation, and can be used in conjunction with supporting functions like SpreadsheetNew() to create a new spreadsheet object, and SpreadsheetInfo() which returns title, subject, sheet names, last saved date and time, and more.
Here are some of the examples I used at my presentations. This first example reads an entire spreadsheet as a query and dumps the contents:
 
<!--- Read spreadsheet ---> <cfspreadsheet action="read" src="Sales.xls" query="myQuery"> <cfdump var="#myQuery#">
This next example reads a specific cell on a specific sheet and returns it in a variable:
 
<!--- Read a spreadsheet cell ---> <cfspreadsheet action="read" src="Sales.xls" name="myVar" sheet="1" rows="C" columns="3"> <cfdump var="#myVar#">
<CFSPREADSHEET> is also used to write (or overwrite) a spreadsheet, as seen here:
 
<!--- Write spreadsheet ---> <cfspreadsheet action="write" overwrite="true" filename="Sales.xls" name="sObj" />
To update a specific cell, you need to read, update, and save, like this:
 
<!--- Read spreadsheet ---> <cfspreadsheet action="read" src="Sales.xls" name="sObj" /> <!--- Set cell value ---> <cfset spreadsheetSetCellValue(sObj, FORM.sales, FORM.row, FORM.col)> <!--- Write spreadsheet ---> <cfspreadsheet action="write" overwrite="true" filename="Sales.xls" name="sObj" />
This example uses form fields to specify the row, column, and value. Now for a more complete example, creating and formatting a spreadsheet from scratch. We'll start with a simple Application.cfc to define the datasource needed for this app:
 
<cfcomponent> <cfset this.datasource="cfartgallery"> </cfcomponent>
Now for the spreadsheet manipulation itself:
 
<!--- Get data ---> <cfquery name="ArtOrders"> SELECT orderid, customerfirstname, customerlastname, total FROM orders ORDER BY orderid </cfquery> <!--- Create new spreadsheet ---> <cfset sObj=SpreadsheetNew()> <!--- Create header row ---> <cfset SpreadsheetAddRow(sObj, "Order,First Name,Last Name,Amount")> <cfset SpreadsheetFormatRow(sObj, {bold=TRUE, alignment="center"}, 1)> <!--- Add orders from query ---> <cfset SpreadsheetAddRows(sObj, ArtOrders)> <!--- Figure out row for formula, 2 after data ---> <cfset rowDataStart=2> <cfset rowDataEnd=ArtOrders.recordCount+1> <cfset rowTotal=rowDataEnd+2> <cfset totalFormula="SUM(D#rowDataStart#:D#rowDataEnd#)"> <!--- Add total formula ---> <cfset SpreadsheetSetCellValue(sObj, "TOTAL:", rowTotal, 3)> <cfset spreadsheetSetCellFormula(sObj, totalFormula, rowTotal, 4)> <!--- Format amount column as currency ---> <cfset SpreadsheetFormatColumn(sObj, {dataformat="$00000.00"}, 4)> <!--- Save it ---> <cfspreadsheet action="write" name="sObj" filename="Orders.xls" overwrite="true">
Once again, we start with a database query. Then, SpreadsheetNew() is used to create a new spreadsheet object.
The code then creates the header row. SpreadsheetAddRow() is used to add a specific row, the column headers (as no row number was specified, SpreadsheetAddRow() adds the row to the next free row, the first). SpreadsheetFormatObject() is then used to format row 1. This function accepts a structure (which here is implicitly created inline).
Next comes the data. SpreadsheetAddRows() adds the entire query to the spreadsheet (again, as no row was specified the data gets added to the next free row).
Now for the formula which will total all orders. So as to not hard code the row, a few <cfset> statements are used to calculate the first and last data row, and the row for the total (2nd row after the end of the data, so leaving one empty row in between data and total). The formula too can't be hard coded, so instead of SUM(D2:D24), the row variables are used to build the formula string dynamically. SpreadsheetSetCellValue() is used to add a title, and SpreadsheetSetCellFormula() inserts the formula.
Next, the entire fourth column, containing the order amounts and the calculated total, is formatted to display as currency using SpreadsheetFormatColumn(). All that is left to do then is to save the file using <cfspreadsheet>. <cfspreadsheet> and its 30+ supporting functions can do lots more, but this should give you a taste of just what's possible using this innovative new feature.

 
Defining a default datasource

Look at this code snippet. Notice anything odd about it?
 
<cfquery name="Art"> SELECT * FROM art ORDER BY ArtName </cfquery>
Yep, the <CFQUERY> tag in the above snippet is missing the DATASOURCE attribute. And yet, this code works. Why? Take a look at this Application.cfc file:
 
<cfcomponent> <cfset this.datasource="cfartgallery"> </cfcomponent>
In ColdFusion 9 you can now specify an application wide default datasource by setting this.datasource in Application.cfc. The specified datasource becomes the default for all <CFQUERY>, <CFSTOREDPROC>, and other tags that expect a datasource to be specified. Of course, datasource can still be specified manually if needed, and a specified datasource will override the application default one.
This is one of those duh! enhancements that just make sense. And, as I'll explain later in this article, it also has another important purpose.

 
ORM – rethinking database integration

Database integration is a hallmark of ColdFusion applications. Indeed, the <CFQUERY> tag was one of the first added to the language, and to this day remains ones of the most used. There is an inherent simplicity and flexibility to being able to create database queries on the fly, so as to be able to refer to column names to work with results. There's also a real downside to this type of database integration. After all, consider what would happen to your code if a table name changed, or if columns were refactored and split, or if whole schemas were updated. Scary, huh? The downside of how most of us integrate databases into our ColdFusion apps is that we tend to write database specific code even at the client level, the code generating output or working with form fields, for example. In addition, we inevitably must write DBMS specific SQL, and that DBMS specific code needs to be managed by our applications.
Before I go any further, I must point out that <cfquery> is not going away, and it remains a very simple and powerful database integration option. For many of us, and for many apps, it is, and remains, highly appropriate. But, having said that, in ColdFusion 9 we're adding support for a newer way to think about database integration, leveraging Object Relational Mapping (ORM). ColdFusion's ORM support is built on Hibernate, the premier ORM implementation in Java development, and in fact, Hibernate is built right into the next version of ColdFusion, and exposed via ColdFusion Components and language elements.
ORM support in ColdFusion requires extensive coverage, and is way beyond the scope of this article, but here are the basics. In ORM, you never write SQL statements, and never really consider tables of rows and columns, at least not when writing client code. Instead, developers using ORM work with objects. For example, a table containing books with columns named title and author and ISBN, would have a corresponding object with the same properties. When using ORM, instead of retrieving a row from a table, you'd retrieve a book object (which is automatically populated by the contents of the table row). To retrieve all rows you'd not use a SELECT * FROM Books, instead you'd use Entity functions to request an array of book objects, populated and ready to use. And then instead of referring to column names in your code, you'd use Get methods in the returned objects. This is actually far less confusing than it sounds, so let's look at an example. First the Application.cfc:
 
<cfcomponent> <cfset this.ormenabled=true> <cfset this.datasource="cfbookclub"> </cfcomponent>
this.datasource is used to define the datasource to be used, and then this.ormenabled is set to TRUE to turn on ORM support.
Next we'll need an object that represents the table to be used. In ColdFusion, objects are implemented as ColdFusion Components (if you are working with multiple tables you'd have multiple CFCs, one for each table). Here's Books.cfc which maps to the Books table in the specified datasource:
 
<cfcomponent persistent="true"> <cfproperty name="BOOKID" column="BOOKID" datatype="integer" length="10" /> <cfproperty name="AUTHORID" column="AUTHORID" datatype="integer" length="10" /> <cfproperty name="TITLE" column="TITLE" datatype="string" length="255" /> <cfproperty name="BOOKDESCRIPTION" column="BOOKDESCRIPTION" datatype="clob" length="2147483647" /> <cfproperty name="BOOKIMAGE" column="BOOKIMAGE" datatype="string" length="50" /> <cfproperty name="THUMBNAILIMAGE" column="THUMBNAILIMAGE" datatype="string" length="50" /> <cfproperty name="ISSPOTLIGHT" column="ISSPOTLIGHT" datatype="character" length="1" /> <cfproperty name="GENRE" column="GENRE" datatype="string" length="50" /> </cfcomponent>
Notice that there are no methods (functions) in this CFC. There are only <cfproperty> tags that correspond to the table columns. And actually, the <cfproperty> tags are optional, and if omitted ColdFusion will read the database table to implicitly define the properties for you! Also, note that the CFC is named Books.cfc, so it maps to the Books table. If the table name changed, or if you needed to use a different CFC name, you could use the optional <cfcomponent> table attribute to specify the table name to use.
Oh, and it's worth noting that the new ColdFusion Builder (more on that in a moment) comes with wizards to generate these table CFCs for you.
And finally, here's how to retrieve the Books data. First we'll retrieve all books (this is equivalent to a SELECT *, but instead of returning a query, an array of Books objects is returned):
 
<!--- Get data ---> <cfset data=EntityLoad("Books")> <!--- Display titles ---> <cfloop from="1" to="#ArrayLen(data)#" index="i"> <cfoutput>#data[i].GetTitle()#<br></cfoutput> </cfloop>
Of course, sorting and filtering and more are all supported. For example, to retrieve books with an author id of 10 (equivalent to a SQL WHERE clause) you could use the following:
 
<!--- Get data ---> <cfset data=EntityLoad("Books", {authorid=10})>
Notice that to access the Title property, a GetTitle() method is used. This is a getter, and it is automatically created by ColdFusion. There are also setters, used to set properties within an object. To save a new or updated object you'd simple do the following:
 
<cfset EntitySave(data)>
This would work for an insert or an update. Just save the object and ColdFusion and Hibernate would figure out whether to update an existing row or insert a new one.
And we've even made it easier to bridge between using ORM and workign with queries (for example, to be able to use , and more), just use the EntityToQuery() function to convert basic arrays of entity objects to familiar ColdFusion queries.
There's a lot more to ORM and the Hibernate integration in ColdFusion. And ColdFusion developers get access to all of Hibernate, be it support for relational tables, lazy loading, caching, query optimization (by tweaking HQL directly), controlling all ORM settings, and much more, too. ColdFusion 9 gives you the power of Hibernate with the productivity that is uniquely ColdFusion.

 
The new ColdFusion LOCAL scope

ColdFusion developers have long known to ensure that local variables remain local by using the "var" keyword to define them. Here's an example:
 
<cffunction name="myFunction"> <cfset var mySafeVariable = 1> <cfset myUnsafeVariable = 1> </cffunction>
In the above snippet, two variables are created. The first variable uses the var keyword to ensure that the variable is local to the function, and if the same variable name existed elsewhere it won't be overwritten. The second variable does not use var, and as such is not local, and variable conflicts can indeed occur. And so, when creating user defined functions or ColdFusion Component methods, the rule has always been to always prefix local variables with "var".
But what exactly is a var variable? What scope is it in? And how is it accessed using explicit scope notation? The answers to these questions are somewhat unclear. This is partially because the local var scope is not used like any other ColdFusion scope, which is always designated by using a scope prefix (VARIABLES.myVariable, FORM.myVariable, SESSION.myVariable, etc.).
ColdFusion 9 simplifies the use of local scopes (without breaking existing code) by explicitly defining a local scope that it intuitively named LOCAL. Here is the same local variable set above, but using the LOCAL scope instead of var:
 
<cfset LOCAL.mySafeVariable = 1>
But what if you do indeed use var? Well, it'll just work. Look at the following two:
 
<CFSET> statements: <cfset local.myVar1=1> <cfset var myVar1=1>
Both of these code snippets do exactly the same thing. Variables with the var prefix are now automatically defined in the LOCAL scope. So a variable defined as:
 
<cfset var mySafeVariable = 1>
can be safely accessed as:
 
<cfoutput>#LOCAL.mySafeVariable#</cfoutput>
It's clean, it's simple, it's intuitive, and it's fully backwards compatible. And yes, if you have a variable named "local" it'll still work—the variable will just become LOCAL.local, and as the LOCAL scope is in the default evaluation chain it'll just work.
Oh, one other change. The "var" keyword used to only be supported at the top of functions and methods, so all local variables had to be defined upfront. This limitation has been removed in ColdFusion 9.

 
Simplified server management

ColdFusion 8 introduced server monitoring for single and multiple servers. This Flex-based app provided access to all sorts of ColdFusion internals, alerts, proactive problem management, and more.
ColdFusion 9 takes this a whole lot further with a new tool called "ColdFusion Server Manager". This AIR-based application allows you to monitor as many servers as needed (including individual ColdFusion instances on a multi-instance configuration) and even offers pop-up alerts when issues occur. It allows for remote server configuration (define a datasource, for example) and settings to be applied to multiple servers at once. It can clear the template caches, upload hot-fixes to one or more servers, and even allow you to select two ColdFusion servers to compare their configuration settings, highlighting any differences between them.
Oh, and before you ask, here are answers to the three most commonly asked questions:
  1. No, this is not a separately sold utility. It is part of ColdFusion itself (and installed via a link in the ColdFusion Administrator).
  2. ColdFusion Server Manager uses APIs added to ColdFusion 9, so no, this will not work with ColdFusion 8 or earlier.
  3. No, we've not made any decisions yet as to product editioning, so no decision as to whether this is an Enterprise only feature or not.

 
The big news: ColdFusion gets an IDE

If all of what we've looked at thus far were not enough, the next "feature" is probably the one that most excites ColdFusion developers. ColdFusion is getting its very own IDE, ColdFusion Builder. I introduce this new tool in my article "Introducing Adobe ColdFusion Builder", but here is a quick list of the important points to note:
  • ColdFusion Builder is built on Eclipse, the same platform which powers Flash Builder, and is actually designed to be installed along with Flash Builder for seamless integration.
  • ColdFusion Builder can work with local or remote servers, and can also stop, start, restart, manager, and monitor those servers.
  • ColdFusion Builder features rich code coloring, context sensitive help, tag attribute help, and more.
  • ColdFusion Builder features toolbars with shortcuts for things like wrapping a block of code in <cfoutput> and more.
  • RDS is integrated, as is a debugger, a log file viewer, and more.
  • ColdFusion Builder has an integrated extensibility model which allows extensions to be written in CFML (instead of Java, the way Eclipse extensions are usually written). And numerous ColdFusion Builder extensions have already been posted to http://www.riaforge.org.
ColdFusion Builder has been a long time coming, and we have big plans for how to further enhance it in the future.

 
Where to go from here

There's a lot more to ColdFusion 9 beyond what I've covered here. From SharePoint and portal server integration, to important language enhancements, to support for offline AIR applications, and more, ColdFusion 9 is an extensive release chock full of powerful and useful features intended to make you more productive than ever before.
If you want to learn more about ColdFusion 9 and ColdFusion Builder, your best bet is to download ColdFusion 9 and the ColdFusion Builder right now. And for a quick introduction to ColdFusion Builder, read my article Introducing Adobe ColdFusion Builder.