Skip to main content
Inspiring
April 7, 2011
Question

Need help with CF component call, please

  • April 7, 2011
  • 2 replies
  • 530 views

Hi!

I've been working with component lately but still learning. In one of my logic I need to use an existing component and I'm not sure how to correctly call the function and get the return result from that.

Can anyone help me please?

Basically I created a component:

<cfcomponent displayname="X">

     <CFFUNCTION name="GetNames">

       <fargument name="1" type="String" required="TRUE">

       <cfargument name="2" type="String" required="TRUE">

       <!---- I do my work here ---->

        within my logic, I need to call an existing function located at

        in a different component And I  need to use the result returned by this function.

        (see below)

        I'm not sure how to correctly call that function from here

        Can I simply do:

        <cfinvoke component="the location of Y component" method="Compare" MyVar = "MyNames" Returnresult="Z">

        <CFIF #Z# NEQ 0>

            <!---  DO something --->

       <CFELSE>

          <!--- do something else --->

       </CFIF>

             

     </CFFUNCTION>

</cfcomponent>

This is the existing componen that I need to use. It's located at the same directory:

<cfcomponent displayname="Y">

<!--- assuming this is the function that I need to call --->

     <CFFUNCTION name="Compare" hint="verify names">

       <cfargument name="MyVar" type="String" required="TRUE">

         

      <cfscript>

            some logic here

            .......................  

            return return_struct;

     </cfscript>

  

     </CFFUNCTION>

</cfcomponent>

Please advice, thank you!

This topic has been closed for replies.

2 replies

Participating Frequently
April 7, 2011

I hopo the following example will be helpful for. Please let me know if you have any questions.

childComponent.cfc:

<cfcomponent displayname="childComponent">
    <cffunction name="childComponentFunction" access="remote" returntype="string">
        <cfargument name="myArgument" type="string" required="yes">
        <cfset var finalString="">
       
        <cfinvoke component="baseComponent" method="baseComponentFunction" returnvariable="finalString">
            <cfinvokeargument name="stringArgumentToReturn" value="#arguments.myArgument#">
        </cfinvoke>

        <cfreturn finalString>
    </cffunction>
</cfcomponent>

baseComponent.cfc:

<cfcomponent displayname="baseComponent">
    <cffunction name="baseComponentFunction" access="public" returntype="string">
        <cfargument name="stringArgumentToReturn" required="yes" type="string">
        <cfreturn arguments.stringArgumentToReturn>
    </cffunction>
</cfcomponent>

FYI - There several other methods through which we can call functions of different functions.

ilssac
Inspiring
April 7, 2011

Yes the same code that you would use to access a component and call a function in a CFML file, is what you would use to access an external component and call a function inside a CFC file.

The ONLY difference you might consider, is to initialize the out side component into a variable of the current component for Object Oriented Reusability Goodness.