Skip to main content
November 10, 2007
Question

simple CFC question

  • November 10, 2007
  • 3 replies
  • 410 views
So I can finally get around to learning proper usage of cffunctions and CFCs, I'm working on a quick test app to get the basics down. My question has to do with return types.

For example, I have a function called "updateUser". A form passes the usual info along with a userID into the function.

If the update is successful, I want to display a message "You have updated the user."

I'm getting hung up on the return types - specifically the use of a boolean return. Assuming if the update goes ok, the boolean return would be true. How can I test for this back on the update page?

If return type is true - display success message
else if it's false - display an error message

I guess I could put some additional cfreturns in the function and return a message string, but this just doesn't seem right?



This topic has been closed for replies.

3 replies

Inspiring
November 10, 2007
You could make your returntype a string. But Craig's answer is better.
Inspiring
November 10, 2007
To expand a bit on what Simon mentioned, here's how you might structure the CFC Method/Function to return a boolean:
<cffunction name="updateUser" access="public" returntype="boolean">
<cfargument name="userId" type="numeric" required="true"/>
<cfargument name="username" type="string" required="true"/>
<cfargument name="email" type="string" required="true"/>
<cfset updateOk = true />
<cftry>
<cfquery datasource="dsn" name="qryUpdateUser">
update user set username='#arguments.username#' where userId = #arguments.userId#
</cfquery>
<cfcatch type="database">
<cfset updateOk = false />
</cfcatch>
</cftry>
<cfreturn updateOk/>
</cffunction>

If the query above runs without an error, the value of updateOk remains true. If there is an error in the DB, the CFCATCH block will be run, setting the value of updateOk to false.

At the end of the function, the value of updateOk is returned to the calling page/code block.

Then, with the code Simon provided, you can use this in your calling page:
<cfif updateUser(userID, name,. email)>
Success
<cfelse>
Failure
</cfif>

Wasn't sure if you were just having trouble on the calling page (where you want to output the success/failure message or with how to return the boolean value from the function as well. Hope this helps.
Participating Frequently
November 13, 2007
You can make the returntype a struct and return both a boolean and a string.

Inspiring
November 10, 2007
The return type describes what the function returns. So with a return type of boolean you can only return a true or false (or something that cf can convert to a boolean).

So you can do:-

<cfif updateUser(userID, name,. email)>
Success
<cfelse>
Failure
</cfif>