11

I have a test.cfm page and would like to call a cfc with a <cffunction> named errorEmail using <cfscript> from that page (test.cfm) instead of

<cfinvoke component = "#cfcPath#" method = "errorEmail" returnVariable = "myReturn" 
    description = "get list of projman">
</cfinvoke> 

I have tried:

<cfscript>
   errorEmail(cfcPath);
</cfscript>
James A Mohler
  • 10,562
  • 14
  • 41
  • 65
isurfbecause
  • 959
  • 2
  • 15
  • 30
  • What is the reason for not calling `errorEmail` with the cfinvoke tag? – Francis P Jul 11 '12 at 16:07
  • That's the way I test my functions in cfcs. I make a test.cfm page and call the functions in the cfc I am testing. Then I usually do cfdumps in test.cfm to make sure the functions work. – isurfbecause Jul 11 '12 at 16:14

1 Answers1

13

I do this all the time.

1) Create the object:

<cfscript>
    // CREATE OBJECT 
    TheCFC = createObject("component", "thecfc");
</cfscript>

2) Call the function:

<cfscript>
    // CALL THE FUNCTION
    SomeVariable = TheCFC .theFunction();
</cfscript>

Your version would look like this

<cfscript>
    // CREATE OBJECT 
    TheObject = createObject("component", "cfcPath");
    // CALL THE FUNCTION
    myReturn = TheObject.errorEmail();
</cfscript>
Evik James
  • 9,645
  • 15
  • 67
  • 118
  • 4
    You can shorten this by chaining the calls: createObject("component", "cfcPath").errorEmail(); – Eric Belair Jul 11 '12 at 16:37
  • 2
    Yes, you can do that. Typically, I don't. I create the object at the top of the page and may refer to it several times throughout the page. Good idea though! – Evik James Jul 11 '12 at 16:39
  • Thanks guys this should help me test my functions quicker! Also, @Eric I didn't know you could chain like jQuery, thanks. – isurfbecause Jul 11 '12 at 16:48
  • I agree with @Evik. If you're reusing an object more than once in a template, then store in a variable and re-use it. But, if you're creating an object only for one purpose, chaining method calls is preferred. – Eric Belair Jul 12 '12 at 02:40
  • 1
    If you are using CF9+ you can also go `myReturn = new path.to.cfc().method();` – Mike Causer Jul 19 '12 at 01:58