Skip to main content
Known Participant
July 10, 2012
Question

get lastID

  • July 10, 2012
  • 2 replies
  • 1535 views

    <!---cfc--->

    <cffunction name="add"  access="public">

        <cfargument name="form" type="struct" required="yes">

       

        <cfquery name="insert"   datasource="#request.dsn#" result="returnID">

        ...insert goes here---

        </cfquery>

            <cfset variables.newDRD_ID= returnID.IDENTITYCOL />   

        </cffunction>

       

        <!---cfm--->

        <!---insert into table--->

         <cfobject type="component" name="request.type" component="#request.cfc#.type">

         <cfset variables.addNewType = request.type.add(form) />

        

         after calling the object above to insert into the type, i want to display

         an id here and ID is retured from cfc.  Can any one show me how to have ID display

         in this cfm page?

        

         Thanks

This topic has been closed for replies.

2 replies

itisdesign
Inspiring
July 11, 2012

Hi newcf,

Dan is right.  Also, within cffunctions you should var/local-scope your variables.  Could you try this?

1) Change <cfquery name="insert"   datasource="#request.dsn#" result="returnID"> to <cfquery name="insert" datasource="#request.dsn#" result="local.returnID">

2) Change <cfset variables.newDRD_ID= returnID.IDENTITYCOL /> to <cfreturn local.returnID.IDENTITYCOL />

3) In the cfm, add <cfoutput>#variables.addNewType#</cfoutput>

Note: If you're on CF9+, you can change IDENTITYCOL to GENERATEDKEY which is not database-specific.

Thanks,

-Aaron

newcfAuthor
Known Participant
July 11, 2012

thanks for all your replies. the reason i did not have cfreturn because i want to get that new id and insert into the child tables.  if i have cfreturn from first query then i can't get the new id to insert into the second query,  any suggessions?

thanks

--insert into parent table-->

<cfquery name="insert"   datasource="#request.dsn#" result="returnID">

        ...insert goes here---

        </cfquery>

<!---<cfreturn local.returnID.IDENTITYCOL />--->

            <cfset variables.newID= returnID.IDENTITYCOL />   

        </cffunction>

---insert into child tables

<cfloop index="backup"  list="#arguments.form.backup#">

                <cfquery name="insert_backup"  datasource="#request.dsn#">

                    INSERT into backups (vou, type_id)

                    VALUES

                    (

                    <cfqueryparam value="#arguments.form.vou#" cfsqltype="cf_sql_varchar" maxlength="255" />,

   

                    <cfqueryparam value="#variables.newID#" cfsqltype="cf_sql_integer" />           

                    )

                </cfquery>

                </cfloop>

Inspiring
July 11, 2012

Return the new id from the first function and pass it as an argument to the function with the other queries.

Inspiring
July 11, 2012

Return it from your function.