Skip navigation
Currently Being Moderated

get lastID

Jul 10, 2012 1:04 PM

    <!---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

 
Replies
  • Currently Being Moderated
    Jul 10, 2012 7:32 PM   in reply to newcf

    Return it from your function.

     
    |
    Mark as:
  • Currently Being Moderated
    Jul 10, 2012 9:59 PM   in reply to newcf

    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

     
    |
    Mark as:
  • Currently Being Moderated
    Jul 11, 2012 11:02 AM   in reply to newcf

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

     
    |
    Mark as:
  • Currently Being Moderated
    Jul 11, 2012 11:22 AM   in reply to newcf

    Some suggestions:

     

    1) Give function a returntype.

     

    2)Use a select query to return the result set, not an insert query.

     

    3) It may be inefficient to pass the whole form object, when the function requires just one or two parameters from the form.

     

    4) You imply that form.backup is a list. It is poor design to pass a list as a form field.

     

    5) You loop through the list arguments.form.backup, storing each list element in the variable backup. However, backup occurs nowhere in the loop.

     

    Here follows a guess from me:

     

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

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

     

        <cfset var idList = "">

     

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

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

            ...insert goes here---

        </cfquery>

     

        <!--- my guess for select query --->

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

            SELECT identityCol

            FROM myTBL

            WHERE vou in (/* list goes here*/)

        </cfquery>

     

        <cfset idList = valueList(returnID.identityCol)>

     

        <!--- loop through IDs, inserting into each child table--->

        <cfloop index="id"  list="#idList#">

            <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="#id#" cfsqltype="cf_sql_integer" />           

                )

            </cfquery>

        </cfloop>

     

    <!---return last ID --->

    <cfreturn listLast(idList)>

    </cffunction>

     
    |
    Mark as:

More Like This

  • Retrieving data ...

Bookmarked By (0)

Answers + Points = Status

  • 10 points awarded for Correct Answers
  • 5 points awarded for Helpful Answers
  • 10,000+ points
  • 1,001-10,000 points
  • 501-1,000 points
  • 5-500 points