2 Replies Latest reply on May 5, 2012 6:28 AM by Adam Cameron.

    cfc return message

    kt03 Level 1

      Hi, in my cfc, i have insert query into the table.  I want it to return the message and send it back to cfm page but don't know how, can you please help? Thanks  KT

       

       

      <!---my cfc--->

      <cfcomponent name="list_queries">

          <cffunction name="add"  returntype="query">

          <cfargument name="form" required="yes" type="struct">

          <cftry>

          <cfquery name="insertMessage" datasource="#request.dbsource#" result="returnMessageID">

              INSERT INTO admin_Message

                   (Title,Description)

               VALUES(

                     <cfqueryparam value="#trim(arguments.form.title)#" cfsqltype="cf_sql_varchar" maxlength="255" />,

                    <cfqueryparam value="#trim(arguments.form.description)#" cfsqltype="cf_sql_longvarchar" />,

                    )

              

              </cfquery>

              <!--- if insert fails --->

              <cfcatch type="database">

                  <cfset isSuccessful=false>

              </cfcatch>

          </cftry>

          </cffunction>

      </cfcomponent>

       

      <!---my cfm--->

      <cfobject type="component" name="request.outages" component="cfcs.outage">

      <cfset variables.add = request.outages.add(form)>

      i want to diplay message here after the inserted from cfc

        • 1. Re: cfc return message
          Dan Bracuk Level 5

          I don't see a cfreturn tag in your cfc.

          • 2. Re: cfc return message
            Adam Cameron. Level 5

            This is kind of a rookie approach we all do at some point, but it's good to get away from as soon as you can.

             

            Java/CF already provides a mechanism to bubble back success/fail situations to the calling code: it's called an exception.  If your code is successful, it will return to the mainline code when it's done.  If it's unsuccessful, it'll raise an exception.  It's then up to your calling code to decide what to do with that.  Your method should simply be "add the record", not "add the record and return a message as to the operation's success".  As soon as you need to use the word "and" in the description of a method's functionality, you're probably trying to do too much with the method.

             

            A sensible return value from an add() method might be to return the ID of the object just added.  But returning success/fail will seldom be a good approach to handling success/failure situations.  Use the mechanism provided: the exception.

             

            --

            Adam