1 Reply Latest reply on Jun 26, 2013 9:46 AM by philmodjunk

    How to Auto populate Primary Key Into Foreign Key Field

    gjbrewer

      Title

      How to Auto populate Primary Key Into Foreign Key Field

      Post

           My Database consists of two tables, the "Comany" table and the "Contacts" table. My first layout allows me to create a new company with the basic company information. I have then created a button and a portal that allows me to create a new contact record in the contact table from the company table. My problem is that each time I create a new contact I have to manually align the foriegn key value to the primary key value of the company that the contact will be assosiated with. How do I set this up so that when I create a new contact under a given company that the primary key for that company automatically poplulates the foriegn key field for that new contact?

                      
                     Preferences
      §
      1
      2
      3
      4
      5
      6
      7
      8
      9
      0
      -
      =
      Backspace
                           
      Tab
      q
      w
      e
      r
      t
      y
      u
      i
      o
      p
      [
      ]
                                
      Return
                                
                           
      capslock
      a
      s
      d
      f
      g
      h
      j
      k
      l
      ;
      '
      \
                           
      shift
      `
      z
      x
      c
      v
      b
      n
      m
      ,
      .
      /
      shift
                           
                                
                               English
                                     
                                     
      alt
      alt
                           
                      
                     Preferences

           What I'd like to do is:

           Create a company, then create contacts for that company that automatically assosiate with that company and no other. Most cases I will have 6 or more contacts assosiated to each company.

           Thanks,

            

      Screen_Shot_2013-06-26_at_10.36.05_AM.png

        • 1. Re: How to Auto populate Primary Key Into Foreign Key Field
          philmodjunk

               If you enable "allow creation of records via this relationship" for the contacts table in your company to contacts relationship, you can add additional contacts without any button for it just by entering data directly into fields of the portal. And then the foreign key is entered automatically.

               But there's nothing wrong with using the button either. If you stick with the add contact button, have it perform a script similar to this:

               Freeze Window
               Set Variable [$CompID ; value: Company::__pkCompanyID ]
               Go To layout [ "Contacts" (Contacts) ]
               New Record/Request
               Set Field [Contacts::_fkCompanyID ; $CompID ]
               Go to Layout [Original Layout]