How do you call a cffunction in a cfc from another cfm page using cfscript?
Asked Answered
E

1

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>
Entreat answered 11/7, 2012 at 16:5 Comment(2)
What is the reason for not calling errorEmail with the cfinvoke tag?Guarneri
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.Entreat
A
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>
Archpriest answered 11/7, 2012 at 16:22 Comment(5)
You can shorten this by chaining the calls: createObject("component", "cfcPath").errorEmail();Ddene
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!Archpriest
Thanks guys this should help me test my functions quicker! Also, @Eric I didn't know you could chain like jQuery, thanks.Entreat
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.Ddene
If you are using CF9+ you can also go myReturn = new path.to.cfc().method();Correspondence

© 2022 - 2024 — McMap. All rights reserved.