Avatar of dawes4000
dawes4000
Flag for United States of America asked on

How do I return a variable from a CFC function and then know what is returned in a CFM form?

I have a form that invokes a function in a CFC, in the this CFC function I query for a record, if the record exists I want a return variable "true" sent to my CFM form, and then make decisions based on what was returned. Here is some code below:

<cfif #form.btnSubmitUser# EQ "Submit User">
   <cfif #form.dealerID# NEQ "-1">
      <cfinvoke component="ccCompany" method="AddUser"
      CompanyID="#form.dealerID#"
      UserID="#form.UserID#"
      Fname="#form.fName#"
      Lname="#form.lName#" returnvariable="ValidID">
       <cfif returnvariable.Valid EQ "true">
          User has been added
        <cfelseif returnvariable.Valid EQ "false">
          UserID is in use
        </cfif>
   <cfelse>
      <cfoutput>
        <table class="fontRegular">
           <tr>
      <td>
      User was not submitted.<br>
      When adding a User make sure you Select a Dealer.
      </td>
           </tr>
       </table>
      </cfoutput>
   </cfif>                                          
</cfif>

<!--- BELOW IS THE FUNCTION -->
<cffunction name="AddUser" access="public">
      <cfargument name="UserID" required="yes">
      <cfargument name="CompanyID" required="yes">
      <cfargument name="Fname" required="yes">
      <cfargument name="Lname" required="yes">
      <cfquery name="qryVerifyID" datasource="#session.connString#">
            SELECT UserID FROM tblUser WHERE UserID = '#arguments.UserID#'
      </cfquery>
      <cfif qryVerifyID.recordcount EQ 0>
            <!--- ADD USER and PWD --->
            <cfquery name="qryAddUser" datasource="#session.connString#">
                  Insert into tblUser (UserID, fName, lName, companyID, rights) VALUES ('#arguments.UserID#', '#arguments.fName#', '#arguments.lName#', '#arguments.CompanyID#', '1')
            </cfquery>
            <cfquery name="qryAddPWD" datasource="#session.connString#">
                  Insert into tblPWD (UserID, PWD) VALUES ('#arguments.UserID#','#arguments.UserID#')
            </cfquery>
            <cfreturn true>
      <cfelse>
            <!--- UserID is taken --->
            <cfreturn false>
      </cfif>
</cffunction>
Web Servers

Avatar of undefined
Last Comment
Netminder

8/22/2022 - Mon
gdemaria


 You forgot to ask a question or state the problem
dawes4000

ASKER
I figured it out. I needed to declare a return type in the CFC function and test only on #ValidID#

<cfif #form.btnSubmitUser# EQ "Submit User">
   <cfif #form.dealerID# NEQ "-1">
      <cfinvoke component="ccCompany" method="AddUser"
     CompanyID="#form.dealerID#"
     UserID="#form.UserID#"
     Fname="#form.fName#"
     Lname="#form.lName#" returnvariable="ValidID">
       <cfif #Valid# EQ "true">
          User has been added
        <cfelseif #Valid# EQ "false">
          UserID is in use
        </cfif>
   <cfelse>
      <cfoutput>
        <table class="fontRegular">
           <tr>
     <td>
     User was not submitted.<br>
     When adding a User make sure you Select a Dealer.
     </td>
           </tr>
       </table>
      </cfoutput>
   </cfif>                                  
</cfif>

<!--- BELOW IS THE FUNCTION -->
<cffunction name="AddUser" access="public" returntype="string">
     <cfargument name="UserID" required="yes">
     <cfargument name="CompanyID" required="yes">
     <cfargument name="Fname" required="yes">
     <cfargument name="Lname" required="yes">
     <cfquery name="qryVerifyID" datasource="#session.connString#">
          SELECT UserID FROM tblUser WHERE UserID = '#arguments.UserID#'
     </cfquery>
     <cfif qryVerifyID.recordcount EQ 0>
          <!--- ADD USER and PWD --->
          <cfquery name="qryAddUser" datasource="#session.connString#">
               Insert into tblUser (UserID, fName, lName, companyID, rights) VALUES ('#arguments.UserID#', '#arguments.fName#', '#arguments.lName#', '#arguments.CompanyID#', '1')
          </cfquery>
          <cfquery name="qryAddPWD" datasource="#session.connString#">
               Insert into tblPWD (UserID, PWD) VALUES ('#arguments.UserID#','#arguments.UserID#')
          </cfquery>
          <cfset result = "true">
          <cfreturn result>
     <cfelse>
          <!--- UserID is taken --->
          <cfset result = "false">
          <cfreturn result>
     </cfif>
</cffunction>
gdemaria


 Glad you got it.

 Post a message here asking them to delete this question.

 https://www.experts-exchange.com/Community_Support/

 (include a link to this question)

Your help has saved me hundreds of hours of internet surfing.
fblack61
ASKER CERTIFIED SOLUTION
Netminder

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question