View comments | RSS feed

cffunction

Description

Defines functionality that you build in CFML.

Category

Extensibility tags

Syntax

<cffunction
  name = "methodName"
  returnType = "dataType"
  roles = "securityRoles"
  access = "methodAccess"
  output = "yes" or "no" >

See also

cfargument, cfcomponent, cfinvoke, cfinvokeargument, cfobject, cfproperty, cfreturn

Attributes

Attribute Req/Opt Default Description
name
Required

A string; a component method that is used within the cfcomponent tag.
returnType
Required for a web service; Optional, otherwise.
�any
String; a type name; data type of the function return value.
  • any
  • array
  • binary
  • boolean
  • date
  • guid
  • numeric
  • query
  • string
  • struct
  • uuid
  • variableName
  • void (this option does not return a value)
  • a return type
If the value is not a recognized type, ColdFusion processes it as a component name.
roles
Optional
"" (empty)
This attribute is used only for a component.
A comma-delimited list of ColdFusion security roles that can invoke the method. If this attribute is omitted, all roles can invoke the method.
access
Optional
public
This attribute is used only for a component.
The client security context from which the method can be invoked:
  • private: available only to the component that declares the method
  • package: available only to the component that declares the method or to another component in the package
  • public: available to a locally executing page or component method
  • remote: available to a locally or remotely executing page or component method, or a remote client through a URL, Flash, or a web service. To publish the function as a web service, this option is required.
output
Optional
Function is processed as standard CFML
This attribute is used only for a component.
  • yes: the function is processed as if it were within a cfoutput tag
  • no: the function is processed as if it were within a cfsilent tag

Usage

Components that are stored in the same directory are members of a component package.

Within a cffunction tag, if you specify the roles attribute, the method executes only if a user is logged in and belongs to one of the specified roles.

For more information, see the "Building and Using ColdFusion Components" chapter in Developing ColdFusion MX Applications with CFML.

The following example shows a typical use of this tag:

<cffunction
  name="getEmployees" 
  access="remote" 
  returnType="query" 
  hint="This query returns all records in the employee database. It can
drill-down or narrow the search, based on optional input parameters.">

Example

<cfcomponent>
  <cffunction name="getEmp">
     <cfquery 
        name="empQuery" datasource="ExampleApps" >
        SELECT FIRSTNAME, LASTNAME, EMAIL
        FROM tblEmployees
     </cfquery>
     <cfreturn empQuery>
  </cffunction>
  <cffunction name="getDept">
    <cfquery 
name="deptQuery" datasource="ExampleApps" >
       SELECT *
       FROM tblDepartments
     </cfquery>
     <cfreturn deptQuery>
  </cffunction>
</cfcomponent>

ColdFusion MX 7 | ColdFusion MX 6.1 | ColdFusion MX | Forums | Developer Center | Bug Reporting

Version 6

Comments are no longer accepted for ColdFusion MX. ColdFusion 8 is the current version.

Comments


pfreitag said on Oct 7, 2002 at 5:12 AM :
When access="private" in a base component, components that inherit from the base component have access to the private function. This is known as protected access in most OOP languages. If this is the intended functionality then the documentation should be more clear.
sundvor said on Jul 19, 2002 at 5:51 AM :
the optional hint attribute is not documented; it is used by the component browser to generate the reports on the components used by the system, and should be documented as such along with the other attributes.
pfreitag said on Aug 24, 2002 at 9:05 PM :
The output attribute, and the roles attribute can be used by UDF's as well as components, the documentation specifies that they can only be used with components.
mdunnOutlaw said on Sep 6, 2002 at 5:30 PM :
Working with the CFFUNCTION tags and have found that you can access variables set in the application.cfm file if those variables are numeric, however if the variable is a string then you get an error and you have to send it in via the struct. Seems to me that the application.cfm file is either available to CFFUNCTION or it's not, doesn't seem like it should be able to pick and choose which variables it will access and which ones it won't.
mdunnOutlaw said on Sep 6, 2002 at 5:30 PM :
Working with the CFFUNCTION tags and have found that you can access variables set in the application.cfm file if those variables are numeric, however if the variable is a string then you get an error and you have to send it in via the struct. Seems to me that the application.cfm file is either available to CFFUNCTION or it's not, doesn't seem like it should be able to pick and choose which variables it will access and which ones it won't.
igneous said on Sep 19, 2002 at 6:08 PM :
It appears that functions are limited to the things they can do. I get a plethora of errors when I tried to put a CFX tag in a function (for a web service) would like to see more documentation (and a work around!!!) to this.
pir2 said on Sep 29, 2002 at 7:15 PM :
Note that Dreamweaver MX fills the output attribute with true / false which doesn't work. it has to be yes/no.
starrunner said on Dec 5, 2002 at 5:49 PM :
When access="remote" a webservice is created. When additionally returnType="array" an array should be returned. Consuming this webservice with cfmx I got a class cast exception. Every other return type was possible but array.
I miss more webservice examples.
starrunner said on Dec 5, 2002 at 5:52 PM :
your adding comment page doesn't work. Everytime you add a comment and submit you will be asked for additional user information. After that you are trapped in a circle.
starrunner said on Dec 5, 2002 at 5:55 PM :
according to my last statement: probably this is only the case when you create a user. When you already have I user profile adding comments work fine.
drac123 said on Jul 17, 2003 at 7:21 PM :
Interesting things happen when you create a function foo() in module boo. When in the page you do this #boo.foo# you get an output (no error) of function location in the server.
JamesC01 said on Sep 6, 2004 at 11:11 AM :
SELECT * is considered bad coding.
No screen name said on Oct 25, 2007 at 3:39 PM :
Look out for errant #'s when migrating code into a cffunction so that you can reuse it on multiple pages.

I just spent several minutes tracking down a "Context validation error for tag cffunction." error. Turns out there was an unescaped #...

Not so bad if the error had made more sense to me.

 

RSS feed | Send me an e-mail when comments are added to this page | Comment Report

Current page: http://livedocs.adobe.com/coldfusion/6/CFML_Reference/Tags-pt145.htm