Skip navigation
Currently Being Moderated

cfc return message

May 4, 2012 7:07 AM

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

 
Replies
  • Currently Being Moderated
    May 5, 2012 5:03 AM   in reply to kt03

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

     
    |
    Mark as:
  • Currently Being Moderated
    May 5, 2012 6:28 AM   in reply to kt03

    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

     
    |
    Mark as:

More Like This

  • Retrieving data ...

Bookmarked By (0)

Answers + Points = Status

  • 10 points awarded for Correct Answers
  • 5 points awarded for Helpful Answers
  • 10,000+ points
  • 1,001-10,000 points
  • 501-1,000 points
  • 5-500 points