Janet
Janet

Reputation: 363

ColdFusion - reference current component in cfscript's invoke

I have a CFC that's mostly tags, but I wanted to use the structEach function, which uses closures, so I opened a cfscript:

<cfscript>
                structEach(res,function(key,value){

                    /*<cfinvoke method="searchByNumberFromName" returnvariable="numRes">
                        <cfinvokeargument name="showActive" value="#formStruct.showActive#" />
                        <cfinvokeargument name="dsn" value="#c#" />
                        <cfinvokeargument name="cusNum" value="#res.c#" />
                    </cfinvoke>*/

                    var argsStruct=structNew();
                    structInsert(argsStruct,"showActive",formStruct.showActive);
                    structInsert(argsStruct,"dsn",key);
                    structInsert(argsStruct,"cusNum",value);

                    var holderObj=createObject("component","dupCheck");
                    numRes=invoke(holderObj,"searchByNumberFromName",argsStruct);
                    WriteDump(numRes,"browser","html",true,"numRes from namesearch");
                }); //end structEach
            </cfscript>

The commented-out code is the tag equivalent of what I want to do, (and c is a leftover from when this was in a that was making me crazy.) After some unhelpful Googling, (I KNOW how to type , thank you...) I tried to create an object with the current component...which makes NO sense, but whatever, I was desperate. I also tried specifying "this" as the component, but that doesn't work, either. can be used with just a method name, but how do I use its equivalent with just a method name? Adobe's CFML docs for CF10 indicate that the component name isn't optional like it is in tags.

FWIW, I directly copy-pasted the function's name from its declaration later on. I'm using onError in application.cfc to e-mail myself the exception thrown, and the message lists the path to the component correctly, at least in terms of how you would navigate there in Windows, if not on the web, (\\hq-devfs\development$\...\myProject\cfc\dupCheck.cfc,) but it's saying that the method was not found in the component???

And yes, everything exists and etc. It blows up on the invoke.

In case it helps, the component is created thus:

<cfcomponent displayname="dupCheck" output="yes" namespace="http://schemas.xmlsoap.org/wsdl/http/" style="document" hint="">

Thanks in advance!

EDIT: H'okay, after editing in light of the answer below, correct turned out to be: numRes=invoke("","searchByNumberFromName",argsStruct); so the "gatcha" here is that you DO still have to pass in that empty string; Adobe's documentation wasn't kidding about that being optional. (If I omit it, I get the "Complex object types cannot be converted to simple values" error, but an empty string apparently defaults to the current CFC.)

Upvotes: 1

Views: 378

Answers (1)

Matt Busche
Matt Busche

Reputation: 14333

yourObject would be the name of the object that contains the method searchByNumberFromName

numRes = yourObject.searchByNumberFromName(showActive = formStruct.showActive, dsn = c, cusNum = res.c);

If this is in function make sure you var numRes to keep it thread safe

Upvotes: 2

Related Questions