Expand my Community achievements bar.

Dive into Adobe Summit 2024! Explore curated list of AEM sessions & labs, register, connect with experts, ask questions, engage, and share insights. Don't miss the excitement.

GET LAST INSERTED RECORD

Avatar

Former Community Member
I am fairly new to flex. So please bear with me no this
question.

I would like to get MYSQL ID of the last record I inserted.
MSQL has as function LAST_INSERT_ID() that is connection based and
will return the last id of the last inserted record. My thought was
in the function contained within the cfc to do 2 queries insert the
record and return the last id directly after (see below). However,
I don't know how I would read the results back into flex. I am
unsure how to write the event handler. Thanks



<cffunction name="insertUser" access="remote"
returntype="query" >

<cfargument name="SSN" required="false" type="string"
hint="SSN"/>

<cfargument name="FNAME" required="false" type="string"
hint="FNAME"/>

<cfargument name="LNAME" required="false" type="string"
hint="LNAME"/>

<cfargument name="CLIENTID" required="false"
type="string" hint="NEW CLIENTID"/>



<cfquery name="insUser" datasource="DBT">

INSERT INTO CLIENT (SSN, FNAME, LNAME) VALUES (

<cfqueryparam value="#ARGUMENTS.SSN#"
cfsqltype="CF_SQL_VARCHAR" maxlength="12"/>,

<cfqueryparam value="#ARGUMENTS.FNAME#"
cfsqltype="CF_SQL_VARCHAR" maxlength="20"/>,

<cfqueryparam value="#ARGUMENTS.LNAME#"
cfsqltype="CF_SQL_VARCHAR" maxlength="30"/>)

</cfquery>

<cfquery name="qRead" datasource="DBT">

SELECT last_insert_id() AS CLIENTID

</cfquery>

<cfreturn qRead >

</cffunction>
1 Reply

Avatar

Level 2
I assume you have LCDS installed with CF and you are using
the remote object service,



<mx:RemoteObject

id="myService"

destination="ColdFusion"

source="full.path.to.cfc"

showBusyCursor="true">

<mx:method name="insertUser"
result="insertUser_result(event)" />

</mx:RemoteObject>

...

<mx:Scrpit>

...

private function insertUser_result(event:ResultEvent) {

trace(event.result);

}



event.result will be the id of the newly inserted record.



BTW, I would highly recommend you looking into the data
management servcie. the SQLAssembler can spare you of the CF code
for simple CRUDs like this.