Link to home
Start Free TrialLog in
Avatar of dawes4000
dawes4000Flag 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>
Avatar of gdemaria
gdemaria
Flag of United States of America image


 You forgot to ask a question or state the problem
Avatar of 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>

 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)

ASKER CERTIFIED SOLUTION
Avatar of Netminder
Netminder

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial