Skip to main content
Inspiring
December 12, 2008
Question

How to get something in return?

  • December 12, 2008
  • 2 replies
  • 1073 views
I'm passing values to my component for an insert into db.
Since this is an insert, my function doesn't return anything, so I wrote <cfreturn true>
without putting cfreturn I would get an error.
This is fine except that I need my function to return something, just as an indication to the calling template that the inserting process is done.
How can i do that?

This topic has been closed for replies.

2 replies

Inspiring
December 12, 2008
You could do this in your cfc.
<cftry>
query
<cfreturn true>
<cfcatch>
<cfreturn false>


Then you can use if/else logic in your function call. This is very similar to many cf native functions.
mega_LAuthor
Inspiring
December 15, 2008
Hi Dan,
I don't get it. How should I write the cfif statement on the calling page:
<CFIF ...???.... IS "true>
do something
<CFELSE>
do something else
</CFIF>
Inspiring
December 15, 2008
mega_L,

Please post the contents of your component and the code calling the component.

Can you give us some more information on what you're trying to accomplish with your component?

quote:

This is fine except that I need my function to return something, just as an indication to the calling template that the inserting process is done.

Why would using a function returning void not be appropriate for your situation? Even if the function doesn't return a value the calling page will normally wait for the function to finish before continuing processing unless your using cfthread or something similar.

Should your component throw an exception if the query fails or just return an indication of failure to the calling page?
Inspiring
December 12, 2008
Does your table use an auto-incremented ID? If so consider using that for a return value for the function.

See attached sample. This code has not been tested.
mega_LAuthor
Inspiring
December 15, 2008
I tried your technique but I got an error saying:
Element QUERYRESULTS.REQUEST_ID is undefined in LOCAL.

request_id is an auto-incremented field (identity is =1).
I added <cfset var local=StructNew() /> on my function and also I changed my return value
from <cfreturn true> to <cfreturn local.queryResults.request_id />

On the calling page I have:
<cfelse>
<!--- Do insert here: remember to use ## when passing a structure --->
<cfinvoke component="components.insert_request" method="InReq"
argumentCollection_1="#str_req_IndRes#" argumentCollection_2="#str_req_Ind#">

<cfdump var="#local#"><cfabort>

</cfif>

I'm working with CF8, Unix and Sybase
Inspiring
December 15, 2008
Instead of using REQUEST_ID as the name of the variable containing your new ID value you will need to use SYB_IDENTITY since you are using Sybase. Each database product has its own CF variable name to support returning the new identity value. See the cfquery documentation.

http://livedocs.adobe.com/coldfusion/8/htmldocs/help.html?content=Tags_p-q_17.html#1102316