I'm trying to determine how to cfreturn an 'output...
# cfml-beginners
p
I'm trying to determine how to cfreturn an 'output inserted' value within a function, so I can display it on screen. Thanks.
d
Copy code
function getOutput(){
   return "output inserted";
}
then:
Copy code
<cfoutput>#getOutput()#</cfoutput>
p
so I'm doing an insert, and using output inserted to retrieve an autogenerated identifier, and then need to cfreturn that value for display.
<cffunction name="createSite" displayName="createSite" hint="Creates the base settings for a new app." access="public" output="no" returntype="void"> <cfargument name="SiteTitle" required="no" default=""> <cfquery name="createSite" datasource="settings"> insert into SiteSettings ( siteTitle ) output inserted.sitesettingsidentifier as newThing values ( <cfqueryparam value="#arguments.siteTitle#" cfsqltype="cf_sql_varchar"> ) </cfquery> <cfreturn newThing> </cffunction>
but that throws an error - newThing is undefined
d
uhhh. first change your returntype to "string" or atleast "any"
p
ah, makes sense. still undefined though.
d
secondly, your query doesn't look right.
"output inserted.sitesettingsidentifier as newThing" is this a comment?
p
no, I'm using that to retrieve the value of the sitesettingsidentifier that was autogenerated by this insert.
d
oh i think you are trying to get the ID of the newly created record.
r
FYI, you generally don't need to use output inserted if your auto generated identity isn't a UUID. You should be able to get it from your query result using result.generatedKey.
d
yes exactly.
p
in other words, <cfreturn result.generatedKey>?
d
<cfquery name="xxx" result="qryResult"...> <cfreturn qryResult.GENERATEDKEY>
r
Otherwise, I think
<cfreturn crateSite.newThing>
will work with your existing query.
p
okay, so generatedkey wasn't working, but <cfreturn createSite.newThing> does. I didn't realize I needed to reference the query name. that's what I was looking for. Thanks guys.
d
Your original question could have been a lot closer to what you wanted. But no worries. I learned something new here which is the "output" clause. I always just relied on cfquery result. In stored procedures I always use
select scope_identity()
p
I was under the impression that scope_identity() only works for the primary key?
a
Over and above everything else said here, @Paul Costello you need to better understand where your code is running. Your
output
statement is running on the database server, your CFML code is running on a CFML server. The CFML server can't see what yer "outputting" on the DB server, and whether or not the outputted value is available to other code running on the DB, doesn't mean it's available on the CFML server. The CFML server sends an SQL statement (the stuff between the
<cfquery>
tags) to the DB server, the DB server runs that statement, and then returns a result.
output inserted.sitesettingsidentifier as newThing
sets a value called
newThing
on your DB server. However all that takes place on the DB server. The CFML server has no idea about that. All it's doing is sitting there waiting for the end result of the overall SQL statement (whatever it is; in your case an
INSERT
). It never knows/cares how anything else that SQL string you have in your`<cfquery>` tag might be interpretted on the DB server. For all intents and purposes the content between the
<cfquery>
tags might as well be
!! !! !!!!! !!!!!!!! !!!!! !!!
. It doesn't understand it, and doesn't care.

https://i2.paste.pics/da8d27f6c17bad70758c25dd7817a23d.png

a
I'm certain that no language I've encountered has given junior developers more trouble with that exact issue -- where is the code running and when did it run -- than ColdFusion and I don't get why.
a
I think it's because the code is sitting in front and in the same file as CFML code that does run on the CFML server. Same thing as when a CFML dev expects JS code to know about CFML variables because the JS code is munged in with CFML code in the one file. "this is a CFML file, therefore all code in it is executed by the CFML server".