Passing parameters to methods by using the cfinvoke tag



When you use the cfinvoke tag, ColdFusion provides several methods for passing parameters to CFC methods:

  • As cfinvoke tag attributes, in name="value" format

  • In the cfinvoke tag argumentcollection attribute

  • In the cfinvoke tag body, using the cfinvokeargument tag

You can use any combination of these methods in a single invocation. If you use the same name in two or three of these methods, ColdFusion uses the value based on the following order of precedence:

  1. cfinvokeargument tags

  2. cfinvoke attribute name-value pairs

  3. argumentcollection arguments

Passing parameters by using attribute format

You can pass parameters in the cfinvoke tag as tag attribute name-value pairs, as the following example shows:

<cfinvoke component="authQuery" method="getAuthSecure" 
    lastName="#session.username#" pwd="#url.password#">

In the example, the parameters are passed as the lastName and pwd attributes.

Note: The cfinvoke tag attribute names are reserved and cannot be used for parameter names. The reserved attribute names are: component, method, argumentCollection, and returnVariable. For more information, see the CFML Reference.

Passing parameters in the argumentCollection attribute

If you save attributes to a structure, you can pass the structure directly using the cfinvoke tag’s argumentCollection attribute. This technique is useful if an existing structure or scope (such as the Forms scope) contains values that you want to pass to a CFC as parameters, and for using conditional or looping code to create parameters.

When you pass an argumentCollection structure, each structure key is the name of a parameter inside the structure.

The following example passes the Form scope to the addUser method of the UserDataCFC component. In the method, each form field name is a parameter name; the method can use the contents of the form fields to add a user to a database.

<cfinvoke component="UserDataCFC" method="addUser" argumentCollection="#Form#">

Passing parameters by using the cfinvokeargument tag

To pass parameters in the cfinvoke tag body, use the cfinvokeargument tag. Using the cfinvokeargument tag, for example, you can build conditional processing that passes a different parameter based on user input.

The following example invokes the corpQuery component:

<cfinvoke component="corpQuery" method="getEmp"> 
    <cfinvokeargument name="lastName" value="Wilder"> 
</cfinvoke>

The cfinvokeargument tag passes the lastName parameter to the component method.

In the following example, a form already let the user select the report to generate. After instantiating the getdata and reports components, the action page invokes the doquery component instance, which returns the query results in queryall. The action page then invokes the doreport component instance and uses the cfinvokeargument tag to pass the query results to the doreport instance, where the output is generated.

<cfobject component="getdata" name="doquery"> 
<cfobject component="reports" name="doreport">  
    <cfinvoke component="#doquery#" method="#form.whichreport#" returnvariable="queryall"> 
    <cfinvoke component="#doreport#"method="#form.whichreport#"> 
        <cfinvokeargument name="queryall" value="#queryall#"> 
    </cfinvoke>