Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

How to get something in return?

Participant ,
Dec 12, 2008 Dec 12, 2008
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?

TOPICS
Getting started
884
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Advisor ,
Dec 12, 2008 Dec 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.
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Dec 15, 2008 Dec 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
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Advisor ,
Dec 15, 2008 Dec 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
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Dec 15, 2008 Dec 15, 2008
I've changed it to SYB_IDENTITY and still got the same error:
Element QUERYRESULTS.SYB_IDENTITY is undefined in LOCAL.
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Dec 12, 2008 Dec 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.
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Dec 15, 2008 Dec 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>
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Advisor ,
Dec 15, 2008 Dec 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?
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Dec 15, 2008 Dec 15, 2008
Bob, I just need an indication that after calling the component and the insert is successful than the calling page would be notified.
I will then be able to write something simple on the user interface, something like:
Your submission is successful and then generate emails to different Departments.

Your suggestion to return an auto-incremented ID is a good idea since I can use that ID on the email.
I managed to do it by writing a: select Max(req_id) as MaxID from table, then
<CFSET NewID= queryname.MaxID>
Then <CFRETURN NewID>

On my calling template I did:
<CFIF IsDefined("NewID")> I did user notification that his'her submission is successfull and <cfmail> other departments</cfif>

Thank you for the suggestion.


Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Dec 15, 2008 Dec 15, 2008
LATEST
mega_L wrote:
> I managed to do it by writing a: select Max(req_id) as MaxID from table, then
> <CFSET NewID= queryname.MaxID>
> Then <CFRETURN NewID>
>

Just be aware that Max(id) may not return the correct id in a multiple
user environment. The problem is that in a heavily used system and|or a
long update process, user A can insert a record, then in the moment
before the select max(id) is run for user A, user B inserts another
record. Now the max(id) is for user B's record.

This is a well known issue and there are multiple solutions published
and easily search able. Some of them mentioned previously in this thread.
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Dec 15, 2008 Dec 15, 2008
mega_L wrote:
> Hi Dan,
> I don't get it. How should I write the cfif statement on the calling page:

<cfif myCFC.theFunction(...) IS true>

OR

<cfset result = myCFC.theFunction(...)>
<cfif result IS true>
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Resources