Skip to main content

Designing CFCs for use with Flex

I have a design pattern for CFCs that I like to use, but recently I ran into an annoying problem where I was trying to use some of my already in-use CFC with a client written in flex, consuming the CFCs as web services.

By finding a nice tip on Kyle Hayes' blog (see here), I was able to get things to working just the way I had hoped, and this blog entry will show how it all comes together.

Here is the basic structure of my standard CFC  template:


<cfset my = StructNew()>

     <cfset my.getList = structNew()/>

my.getList.error = 0/>


          <cfquery name="qrsRESULTS" dataSource="MY_DATASOURCE">

SELECT col1,col2,col3


WHERE somevalue = '#ARGUMENTS.somevalue#'


          <cfset my.getList.result = qrsRESULTS>

          <cfcatch type="any">

<cfset my.getList.error = 1>

<cfset my.getList.message = CFCATCH.message>



     <cfreturn my.getList>


<cffunction name="getStuffAsQuery" returntype="struct" access="remote">

     <cfargument name="somevalue" type="string" required="true">


Note that the return value is "Struct" even though I am wanting to return a Query object (which Flex will auto-interpret to be an ArrayCollection).  Also note that I am constructing a structure with the named "my.{function name}" where all the values are stored. 

The main reason for making your CFC this way is so that you have a predictable way to check if each and every method call succeeds and a predictable way to get the results.  You can check the .error value of every call for success, and in your application (whether it be CF or Flex), take the appropriate error-handling steps.

In Flex, you would define your webservice for the above CFC like so:

<mx:WebService id="wsdlService1" wsdl="" showBusyCursor="true">

     <mx:operation name="getStuffAsQuery" result="handleMyResult(event)" fault="errorHandler(event)">

<mx:request xmlns="">

<somevalue>{this you have to provide}</somevalue>




To execute this web service call, you would do the following:


When this call completes successfully, it will call the "handleMyResult()" function, and if it fails, it will call the "errorHandler()" function (see above code for these references).

Lets look at the "handleMyResult()" function:


public function handleMyResult(event:ResultEvent):void


      if (event.result.ERROR == 0)


  "Data received properly!");

            // query results are in variable "event.result.RESULT"

            // Flex will automatically interpret this as an ArrayCollection

            // so you can assign it as the data provider for a DataGrid, etc.




  "There was an error loading data: " + event.result.MESSAGE);



In this function, the result event is passed in.  "result.event" contains whatever came back from the web service, which in our case is a ColdFusion struct.  Flex will interpret this as an untyped Object, just like a Flash Object.  The struct members from ColdFusion are there, but are object properties now.  The important thing to note here is that the names of the object properties that came from our ColdFusion struct are now all caps.  In the CFC code, we had a property called "error" -- but in Flex, we have to reference that as "ERROR."  Likewise, the RESULT and the MESSAGE properties are from our ColdFusion struct.

More later, and if you have questions, feel free to ask.

Popular posts from this blog

Installing python 3.4.x on OSX El Capitan

I love "brew" package manager, but sometimes being too progressive breaks things.  I have several python apps that I maintain that get deployed to AWS using Elastic Beanstalk.  AWS eb can deploy with python 2.7 or 3.4.  Any recent 'brew install python3" will get 3.5.1. #annoying

Making Macbook Air with 128GB SSD usable with Bootcamp

I recently got a new Macbook Air 11" (the 2012 version) and loaded it with goodies like 8GB ram and 2GHz Core i7.  What I DIDN'T upgrade was the internal SSD.  My config came with 128GB SSD and I refused to pay $300+ to upgrade it to 256GB.  Yeah I know, some call me cheap, but SSds cost $75-$150 for 240GB, so adding another 128GB for $300 seemed way too steep for me.  I figured "ok, I'm going to make 128G work!"

Here is the story of how that went...

Getting Started with OpenVAS on CentOS - an open source vulnerability scanner

The Open Vulnerability Assessment System (OpenVAS) is a framework of several services and tools offering a comprehensive and powerful vulnerability scanning and vulnerability management solution. (Taken from the OpenVAS website, which is at )

This blog entry will introduce OpenVAS version 3.1, walk through installation on CentOS and is intended as a "getting started" guide. I'll also do a guide for installing on Ubuntu later.