Question

I am using ColdFusion 9.0.1.

Let me start by stating that I may not be asking the right question. Since each function works independently and fails only when one function calls another, I am thinking that the problem is in how the function is called.

I am creating an application variable that contains a structure. The structure contains the reference to an object, orders.cfc.

if (not isDefined("APPLICATION.AppInfo") or not isStruct(APPLICATION.AppInfo)) {
    APPLICATION.AppInfo = structNew();
    APPLICATION.AppInfo.objOrders = createObject("component", "globaladmin.orders");
}

I am able to successfully access the methods in the orders.cfc like this:

OrderItemList = APPLICATION.AppInfo.objOrders.orderItemList(URL.Customer);

I have methods in the orders.cfc that call other methods in the order.cfc, kind of like this (faked for simplicity):

<cffunction name="orderItemList">
    <cfscript>
          LOCAL.RandomNumber = getRandomNumber();
          return LOCAL.RandomNumber;
    </cfscript>
</cffunction>

<cffunction name="getRandomNumber">
    <cfscript>
        LOCAL.SomeNumber= randRange(0,10);
        return LOCAL.SomeNumber;
    </cfscript>
</cffunction>

I get this error:

Entity has incorrect type for being called as a function. The symbol you provided getRandomNumber is not the name of a function.

I figured maybe I can't reference a function within the same CFC without creating an object first, so I do this:

<cffunction name="orderItemList">
    <cfscript>
          LOCAL.RandomNumber = APPLICATION.AppInfo.objOrders.getRandomNumber();
          return LOCAL.RandomNumber;
    </cfscript>
</cffunction>

Then, I'd get this error:

Either there are no methods with the specified method name and argument types, or the method getRandomNumber is overloaded with arguments types that ColdFusion can't decipher reliably. If this is a Java object and you verified that the method exists, you may need to use the javacast function to reduce ambiguity.

How should I call a second function within the same CFC?

Was it helpful?

Solution

The first thing I would try is var scoping your all your variables within your functions:

<cffunction name="orderItemList">
    <cfscript>
          var RandomNumber = getRandomNumber();
          return RandomNumber;
    </cfscript>
</cffunction>

<cffunction name="getRandomNumber">
    <cfscript>
        var SomeNumber= randRange(0,10);
        return SomeNumber;
    </cfscript>
</cffunction>

If that doesn't solve the problem, let me know and we can explore further.

edit Okay, now that the local scope issue is resolved, try this:

<cffunction name="orderItemList">
    <cfscript>
          LOCAL.RandomNumber = THIS.getRandomNumber();
          return LOCAL.RandomNumber;
    </cfscript>
</cffunction>

<cffunction name="getRandomNumber">
    <cfscript>
        LOCAL.SomeNumber= randRange(0,10);
        return LOCAL.SomeNumber;
    </cfscript>
</cffunction>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top