8 Replies Latest reply on Sep 18, 2012 2:15 PM by ion

    calling a cffunction from another cffunction

    ion Level 1

      i figured this would work, it doesn't throw an error but it doesn't create a log entry either. what am i doing wrong?

       

      <cfcomponent>

       

      <cffunction name="doSomething">

           <cfset var logThis="create a new log entry">

          <cfscript>

             logSomething(logThis);

          </cfscript>

      </cffunction>

       

      <cffunction name="logSomething">

           ... some code i know works...

           <cffile action="append" file="#localFile#" output="#txtVar#" addNewLine="yes"> 

      </cffunction>

       

      </cfcomponent>

       

       

      thanks

        • 1. Re: calling a cffunction from another cffunction
          Miguel-F Level 3

          It's hard to say what is wrong from the simple example given.  You can definitely call a function from within a function, no problem.  Are you sure no error is being thrown? 

          • 2. Re: calling a cffunction from another cffunction
            ion Level 1

            i've simplified it even more, returns "null" within the cfdiv

             

            this is the cfc

             

            <cfcomponent>

             

            <cffunction name="doSomething" access="remote">

                <cfscript>

                   logSomething();

                </cfscript>

            </cffunction>

             

            <cffunction name="logSomething">

                <cfreturn "doSomething just called me">

            </cffunction>

             

            </cfcomponent>

             

            and this is a cfm using it:

             

            <cfajaximport />

            <cfdiv bind="cfc:testfunc.doSomething()"></cfdiv>

            • 3. Re: calling a cffunction from another cffunction
              Miguel-F Level 3

              Oh I thought you had just posted a simple example here.  Didn't know that was your actual cfc.  It is returning "null" to your div because you are not specifying a returntype from your functions.  If you want it to return something, try this:

               

              <cffunction name="doSomething" access="remote" returnType="string">

                  <cfscript>

                     logSomething();

                  </cfscript>

                  <cfreturn "message was logged">

              </cffunction>

               

              <cffunction name="logSomething">

                  <cfreturn "doSomething just called me">

              </cffunction>

              • 4. Re: calling a cffunction from another cffunction
                Dan Bracuk Level 5

                In addition to Miguel's answer, if you are going to do this:

                logSomething(logThis);

                 

                Then the logSomething function needs an appropriate cfargument tag.

                • 5. Re: calling a cffunction from another cffunction
                  ion Level 1

                  it was a simple example, my real one is using a callbackhandler. i've added the returnType to this simple example and returns "message was logged", i don't know if it actually executes the second function, i would expect to see "doSomething just called me" too

                  • 6. Re: calling a cffunction from another cffunction
                    Miguel-F Level 3

                    It's very difficult to diagnose if we don't know what code is actually failing.

                     

                    You will not see "doSomething just called me" because that function has no returnType either and the calling function is not storing the result to return from it's call.  I'm afraid we will need to see your actual code to be of any help.  You can change any url, file system, usernames, passwords, databases, table names, etc in your code example that you post here.

                    • 7. Re: calling a cffunction from another cffunction
                      ion Level 1

                      if we can't make this simple example work, how are we gonna handle the other one? i don't want the first function to return anything, i just want it to call the second function, and that one, in turn, return a string. i've added Dan's suggestion, still nothing

                       

                      <cfcomponent name="testfunc">

                       

                      <cffunction name="doSomething" access="remote" output="false" returnType="string">

                          <cfset var firstMessage="message received from doSomething">

                          <cfscript>

                             logSomething(firstMessage);

                          </cfscript>

                      </cffunction>

                       

                      <cffunction name="logSomething" access="remote" output="false" returnType="string">

                          <cfargument name="firstMessage" type="string" required="yes">

                         

                          <cfreturn firstMessage>

                      </cffunction>

                       

                      </cfcomponent>

                      • 8. Re: calling a cffunction from another cffunction
                        ion Level 1

                        i couldn't make this work, however, it turns out i don't have to. while "returning" a string that comes from a different function doesn't seem to be possible, my real code actually works, provided i call the second function BEFORE using the cfreturn in the first

                         

                        however, the reason it wasn't working before,  was my var name, which was "writeLog". turns out writelog is a reserved word, it's an actual CF function

                        it looks like it might be able to do what i'm doing in my second function, however it didn't work when i tried in a cfm page:

                         

                        <cfscript>

                            function someLog() {

                             WriteLog(type="Info", file="myFile.log", text="someText");

                            }

                        </cfscript>