cffunction

Description

Defines a function that you can call in CFML. Required to define ColdFusion component methods.

History

ColdFusion MX 7: Added the XML value to the returntype attribute.

ColdFusion MX: Added this tag.

Category

Extensibility tags

Syntax

<cffunction
name = "methodName"
returnType = "dataType"
roles = "securityRoles"
access = "methodAccess"
description = "function description"
output = "yes" or "no"
displayName = "name"
Hint = "hint text">

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 - The argument must be a UUID or GUID of the form xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx where each x is a character representing a hexadecimal number (0-9A-F).
  • numeric
  • query
  • string
  • struct
  • uuid: the argument must be a ColdFusion UUID of the form xxxxxxxx-xxxx-xxxx-xxxxxxxxxxxxxxxx where each x is a character representing a hexadecimal number (0-9A-F).
  • variableName: a string formatted according to ColdFusion variable naming conventions.
  • void: does not return a value
  • xml: allows web service functions to return CFML XML objects and XML strings.
  • a component name: if the type attribute value is not one of the preceding items, ColdFusion treats it as the name of a ColdFusion component. When the function executes, it generates an error if the argument that is passed in is not a CFC with the specified name.

roles

Optional

"" (empty)

A comma-delimited list of ColdFusion security roles that can invoke the method. Only users who are logged in with the specified roles can execute the function. If this attribute is omitted, all users can invoke the method.

access

Optional

public

The client security context from which the method can be invoked:

  • private: available only to the component that declares the method and any components that extend the component in which it is defined.
  • package: available only to the component that declares the method, components that extend the component, or any other components 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.

description

Optional

 

Supplies a short text description of the function.

output

Optional

Function body is processed as standard CFML

Specifies under which conditions the function can generate HTML output.

  • yes: the entire function body is processed as if it were within a cfoutput tag. Variables names surrounded by number signs (#) are automatically replaced with their values.
  • no: the function is processed as if it were within a cfsilent tag

If you do not specify this attribute, the function body is processed as standard CFML. Any variables must be in cfoutput tags.

displayname

Optional

 

Meaningful only for CFC method parameters. A value to be displayed in parentheses following the function name when using introspection to show information about the CFC.

hint

Optional

 

Meaningful only for CFC method parameters. Text to be displayed when using introspection to show information about the CFC. The hint attribute value follows the syntax line in the function description.

Usage

The cffunction tag can define a function that you call in the same manner as a ColdFusion built-in function.

To define a ColdFusion component (CFC) method, you must use a cffunction tag.

The following example shows cffunction tag attributes for a simple CFC method that returns a ColdFusion Query object.

<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.">

For information on using the cffunction tag for ColdFusion components, see Building and Using ColdFusion Components in ColdFusion MX Developer's Guide.

If you specify a roles attribute, the function executes only if a user is logged in and belongs to one of the specified roles.

If you specify variableName for the returnType attribute, the function must return a string that is in ColdFusion variable name format; that is, the function must return a string that starts with a letter, underscore, or Unicode currency symbol, and consist of letters, numbers, and underscores (_), periods, and Unicode currency symbols, only. ColdFusion does not check whether the value corresponds to an existing ColdFusion variable.

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>

View comments in LiveDocs