Alpha Software Mobile Development Tools:   Alpha Anywhere    |   Alpha TransForm subscribe to our YouTube Channel  Follow Us on LinkedIn  Follow Us on Twitter  Follow Us on Facebook

Announcement

Collapse

The Alpha Software Forum Participation Guidelines

The Alpha Software Forum is a free forum created for Alpha Software Developer Community to ask for help, exchange ideas, and share solutions. Alpha Software strives to create an environment where all members of the community can feel safe to participate. In order to ensure the Alpha Software Forum is a place where all feel welcome, forum participants are expected to behave as follows:
  • Be professional in your conduct
  • Be kind to others
  • Be constructive when giving feedback
  • Be open to new ideas and suggestions
  • Stay on topic


Be sure all comments and threads you post are respectful. Posts that contain any of the following content will be considered a violation of your agreement as a member of the Alpha Software Forum Community and will be moderated:
  • Spam.
  • Vulgar language.
  • Quotes from private conversations without permission, including pricing and other sales related discussions.
  • Personal attacks, insults, or subtle put-downs.
  • Harassment, bullying, threatening, mocking, shaming, or deriding anyone.
  • Sexist, racist, homophobic, transphobic, ableist, or otherwise discriminatory jokes and language.
  • Sexually explicit or violent material, links, or language.
  • Pirated, hacked, or copyright-infringing material.
  • Encouraging of others to engage in the above behaviors.


If a thread or post is found to contain any of the content outlined above, a moderator may choose to take one of the following actions:
  • Remove the Post or Thread - the content is removed from the forum.
  • Place the User in Moderation - all posts and new threads must be approved by a moderator before they are posted.
  • Temporarily Ban the User - user is banned from forum for a period of time.
  • Permanently Ban the User - user is permanently banned from the forum.


Moderators may also rename posts and threads if they are too generic or do not property reflect the content.

Moderators may move threads if they have been posted in the incorrect forum.

Threads/Posts questioning specific moderator decisions or actions (such as "why was a user banned?") are not allowed and will be removed.

The owners of Alpha Software Corporation (Forum Owner) reserve the right to remove, edit, move, or close any thread for any reason; or ban any forum member without notice, reason, or explanation.

Community members are encouraged to click the "Report Post" icon in the lower left of a given post if they feel the post is in violation of the rules. This will alert the Moderators to take a look.

Alpha Software Corporation may amend the guidelines from time to time and may also vary the procedures it sets out where appropriate in a particular case. Your agreement to comply with the guidelines will be deemed agreement to any changes to it.



Bonus TIPS for Successful Posting

Try a Search First
It is highly recommended that a Search be done on your topic before posting, as many questions have been answered in prior posts. As with any search engine, the shorter the search term, the more "hits" will be returned, but the more specific the search term is, the greater the relevance of those "hits". Searching for "table" might well return every message on the board while "tablesum" would greatly restrict the number of messages returned.

When you do post
First, make sure you are posting your question in the correct forum. For example, if you post an issue regarding Desktop applications on the Mobile & Browser Applications board , not only will your question not be seen by the appropriate audience, it may also be removed or relocated.

The more detail you provide about your problem or question, the more likely someone is to understand your request and be able to help. A sample database with a minimum of records (and its support files, zipped together) will make it much easier to diagnose issues with your application. Screen shots of error messages are especially helpful.

When explaining how to reproduce your problem, please be as detailed as possible. Describe every step, click-by-click and keypress-by-keypress. Otherwise when others try to duplicate your problem, they may do something slightly different and end up with different results.

A note about attachments
You may only attach one file to each message. Attachment file size is limited to 2MB. If you need to include several files, you may do so by zipping them into a single archive.

If you forgot to attach your files to your post, please do NOT create a new thread. Instead, reply to your original message and attach the file there.

When attaching screen shots, it is best to attach an image file (.BMP, .JPG, .GIF, .PNG, etc.) or a zip file of several images, as opposed to a Word document containing the screen shots. Because Word documents are prone to viruses, many message board users will not open your Word file, therefore limiting their ability to help you.

Similarly, if you are uploading a zipped archive, you should simply create a .ZIP file and not a self-extracting .EXE as many users will not run your EXE file.
See more
See less

Need to grab the pk of the record that was just inserted

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    Need to grab the pk of the record that was just inserted

    I am inserting a new record and after that record is created I need to perform an insert into a secondary table based on information that was submitted in the original submit.

    How do I grab the key after the record is inserted so that i can use it for the second insert? Any help is appreciated.

    Is there a way to do this with CRUD operations/action javascript? Or does something like this need to be handled with an xbasic function/callback?

    Thanks!

    #2
    Re: Need to grab the pk of the record that was just inserted

    Please see this discussion.
    http://www.alphasoftware.com/alphafo...nserted+record
    Regards,

    Kotin Karwak
    Developer Edition
    Version 12.3 Build 2684
    System Addins: Build 4438
    Build machine Windows Vista
    Skype: mateso08

    Comment


      #3
      Re: Need to grab the pk of the record that was just inserted

      Just done the same thing today in a UX.

      I have a child record. This is a contact in the contact table
      Once the record is created I need to write it to the parent table as a link.

      My problem was that I did not want the user to see I was updating two tables. I just wanted one save button.

      If this is what you want I can make a video explaining the process. It is super simple and requires nearly no code.

      Michael

      Comment


        #4
        Re: Need to grab the pk of the record that was just inserted

        For me

        I am creating a prospect in the prospects table and inserting answers to survey questions in a surveyanswers table. I need the prospectID that gets created when I insert the new record into the prospects table so that I can pass it in as an argument when I insert into the surveyanswers table.

        Comment


          #5
          Re: Need to grab the pk of the record that was just inserted

          Ok will make a video tomorrow morning.

          Comment


            #6
            Re: Need to grab the pk of the record that was just inserted

            if this is from a grid, then the primary key will be available in e.lastIdentityValue in after insert event
            or
            if this is from a dialog then it will be available in
            rtc.a_sections[1].rows[1].alias[1].primaryKeyValue[1].value

            then you can assign to some variable and use it in the next insert to the survey table.
            Last edited by GGandhi; 06-24-2015, 06:45 AM. Reason: spelling error
            thanks for reading

            gandhi

            version 11 3381 - 4096
            mysql backend
            http://www.alphawebprogramming.blogspot.com
            [email protected]
            Skype:[email protected]
            1 914 924 5171

            Comment


              #7
              Re: Need to grab the pk of the record that was just inserted

              Ok as promised

              https://www.youtube.com/watch?v=7JdLpB17Ey0

              Please note Gandhi's method is typical of Alpha, there is generally more than one way of achieving things. His method also gives you much more control of when you perform the action.

              Michael

              Comment


                #8
                Re: Need to grab the pk of the record that was just inserted

                please take a look at this screencast,
                http://screencast.com/t/uTwIsYtLT9D
                this is how I do it either in grid ( rare for me ) or in dialog ( usual for me )
                thanks for reading

                gandhi

                version 11 3381 - 4096
                mysql backend
                http://www.alphawebprogramming.blogspot.com
                [email protected]
                Skype:[email protected]
                1 914 924 5171

                Comment


                  #9
                  Re: Need to grab the pk of the record that was just inserted

                  Ghandi,

                  This is a ajax callback/custom xbasic function. It doesn't look like the variable rtc.a_sections[1].rows[1].alias[1].primaryKeyValue[1].value exists in this context

                  Comment


                    #10
                    Re: Need to grab the pk of the record that was just inserted

                    I can use conn.LastInsertedIdentity()

                    The only problem with that is it's always possible that I could pull a record that was inserted by another user after my insert...It's unlikely but still possible. If there is a more reliable way of dealing with this please let me know.

                    Thanks!

                    Comment


                      #11
                      Re: Need to grab the pk of the record that was just inserted

                      Originally posted by coleresources View Post
                      I can use conn.LastInsertedIdentity()

                      The only problem with that is it's always possible that I could pull a record that was inserted by another user after my insert...It's unlikely but still possible. If there is a more reliable way of dealing with this please let me know.

                      Thanks!
                      If you look at my example the primary key is returned on saving in the first (Parent ) table. Then it is moved with javaScript to the link field. Then the second (Child) table is saved.
                      You don't have to reinvent the wheel Alpha have done it for you.

                      If you don't like this approach then write a trigger in SQL.


                      Michael

                      Comment


                        #12
                        Re: Need to grab the pk of the record that was just inserted

                        Originally posted by coleresources View Post
                        I can use conn.LastInsertedIdentity()

                        The only problem with that is it's always possible that I could pull a record that was inserted by another user after my insert...It's unlikely but still possible. If there is a more reliable way of dealing with this please let me know.

                        Thanks!
                        no I don't think you are correct. every connection keeps their last inserted id to themselves in all the sql servers.
                        take a look at the documentation in sql, any sql.

                        from MySQL documentation
                        For LAST_INSERT_ID(), the most recently generated ID is maintained in the server on a per-connection basis. It is not changed by another client. It is not even changed if you update another AUTO_INCREMENT column with a nonmagic value (that is, a value that is not NULL and not 0). Using LAST_INSERT_ID() and AUTO_INCREMENT columns simultaneously from multiple clients is perfectly valid. Each client will receive the last inserted ID for the last statement that client executed.
                        Last edited by GGandhi; 07-03-2015, 04:52 AM.
                        thanks for reading

                        gandhi

                        version 11 3381 - 4096
                        mysql backend
                        http://www.alphawebprogramming.blogspot.com
                        [email protected]
                        Skype:[email protected]
                        1 914 924 5171

                        Comment


                          #13
                          Re: Need to grab the pk of the record that was just inserted

                          Thanks for clarifying Ghandi and thank you Kotin for sharing that post. Made it very easy.

                          Comment


                            #14
                            Re: Need to grab the pk of the record that was just inserted

                            I found this thread as I needed to do something similar ie use the primary key from an explicit insert in some xBasic code I developed to update secondary, linked tables in MS SQL Server.

                            Unfortunately, I hit a snag in that the key returned was not the last entry to the table I was inserting into! After much puzzlement, I discovered that the key returned was from an audit file that was updated via a trigger associated with my main table.

                            So, it seems that SQL does indeed return the last primary key for an insertion associated with your connection BUT if that insertion triggers another table entry, it is the key from the triggered entry that is sent back!

                            I solved this by adding a query for the maximum key of the main table for records that included my user id stored in a "created by" field thus getting the correct result ie the primary key from the last record I inserted.

                            Hopefully this will help others with the same issue.

                            Cheers

                            Bob

                            Comment

                            Working...
                            X