How to call a function in coldfusion

3.1k Views Asked by At

I a test.cfc I have created a component in Application.cfc using

<cffunction name="onApplicationStart">
   <cfset application.api = {tst= createObject( "component", "com.Test" )} />   
   <cfreturn true />
</cffunction>

Now how do I call a method in Test.cfc in my cfm file ?

1

There are 1 best solutions below

1
On

This code

<cffunction name="onApplicationStart">
   <cfset application.api = {tst= createObject( "component", "com.Test" )} />   
   <cfreturn true />
</cffunction>

defines the variable application.api.tst. You want define a variable by calling a method in this object (CFC).

You can define the variable list in two ways:

<cfinvoke component="#application.api.tst#" method="doSomething" returnVariable="list" />

as Sean describes or by using a simple CFSET

<cfset list = application.api.tst.doSomething() />

The value of list is based on whatever was returned by the function doSomething, which is a public function inside the component com.Test that exists in the application variable.

You should be able to output of CFDUMP the value of list at this point. If the variable does not have the value you expected, then you need to verify what the function doSomething is returning.