Accessibility

Table of Contents

Employee Updater: Building a Client-Server Application with Macromedia Flash Data Connection Wizard and ColdFusion

The Back-End ColdFusion Web Service

For the application to obtain a list of users and a list of departments, create two CFC methods, named ListDepts and ListByDept, which will return the lists. Copy and paste the following code in employees.cfc in Code view:

<CFCOMPONENT>
<!--- Obtain department list --->
<CFFUNCTION NAME="ListDepts"
            HINT="List departments"
            ACCESS="remote"
            RETURNTYPE="query">
   <CFQUERY DATASOURCE="exampleapps"
            NAME="dept">
   SELECT *
   FROM tblDepartments
   ORDER BY DepartmentName
   </CFQUERY>

   <CFRETURN dept>
</CFFUNCTION>

<!--- Obtain department employees --->
<CFFUNCTION NAME="ListByDept"
            HINT="Get employee"
            ACCESS="remote"
            RETURNTYPE="query">
   <CFARGUMENT NAME="DepartmentID"
               TYPE="string"
               REQUIRED="yes">
   <CFQUERY DATASOURCE="exampleapps"
            NAME="emps">
   SELECT *
   FROM tblEmployees
   WHERE DeptIDFK = '#ARGUMENTS.DepartmentID#'
   ORDER BY LastName, FirstName
   </CFQUERY>

   <CFRETURN emps>
</CFFUNCTION>
</CFCOMPONENT>

The ListDepts method retrieves a list of departments from the tblDepartments table. The ListByDept method returns all the employees in a specified department from the tblEmployees table. Save both of these methods in employees.cfc. You'll add more methods to this component later; for now this is all the ColdFusion code you need.

Notice that both methods specify ACCESS="remote". You must use this attribute is required for Flash on the client to be able to invoke CFC methods on the server. The default ACCESS level is public, which allows any code on the server to access the CFCs, but not any code outside of the server. Setting ACCESS="remote" for specific methods makes those methods available remotely (to Flash, in this example).

Testing the Back-End Code

Before going further, verify that the ColdFusion component functions properly. Debugging CFML problems from the Flash client side gets rather complicated; as a rule always test your back-end code independently first.

The following simple CFML code invokes the employees.cfc component to display a list of departments and lets a user click a department to display department members.

<!--- Instantiate employee component --->
<CFOBJECT COMPONENT="employees" NAME="empObj">

<!--- Get departments --->
<CFSET depts=empObj.ListDepts()>

<H1>Departments</H1>
<UL>
   <CFOUTPUT QUERY="depts">
      <LI><A HREF="emplist.cfm?departmentid=#DepartmentID#&departmentname=#DepartmentName#">#DepartmentName#</A></LI>
   </CFOUTPUT>
</UL>

<!--- Employees, if department id was passed --->
<CFIF IsDefined("URL.DepartmentID")
      AND IsDefined("URL.DepartmentName")>
   <!--- Get employees --->
   <CFSET emps=empObj.ListByDept(URL.DepartmentID)>
   <CFOUTPUT>
   <H1>#URL.DepartmentName# Employees</H1>
   </CFOUTPUT>
   <UL>
   <CFOUTPUT QUERY="emps">
      <LI>#LastName#, #FirstName#</LI>
   </CFOUTPUT>
   </UL>
</CFIF>

Save this code in a new file named emplist.cfm in the application directory, and run it to verify that the CFC functions (your output will be similar to Figure 1). You are now ready to build a Flash front end to this same ColdFusion component.

HTML version of the employee by department application

Figure 1. HTML version of the "employee by department" application