0

Link record n:1

I use this code to automatically link records 1:n

let myID := ID;
let nMP := (create 'SecondTable');
nMP.('FirstTable' := myID);

What code can I use for the reverse, n:1?

THX

8 replies

null
    • Fred
    • 3 yrs ago
    • Reported - view

    What do you mean you want to do it in reverse? If it is a 1:N then wouldn't you always be in your FirstTable and be creating records from there, letting Ninox create the links for you?

     

    You have created a record in SecondTable that doesn't have a link to FirstTable and now you want to link them? What data would you use to link the two records?

    • Andrea.1
    • 3 yrs ago
    • Reported - view

    Hi Fred,

    I have two tables "SecondTable" and "FirstTable". The two tables have a relationship (N: 1). They have in common a field whose name is "CF". With a button in the "SecondTable" I would like to automatically create a record in the "FirstTable" and link it (always automatically) to the "SecondTable".

    • Fred
    • 3 yrs ago
    • Reported - view

    Hi Andrea -

     

    In your first post you stated that you have a field called FirstTable, so I'm guessing that is the reference field in SecondTable linking it to FirstTable.

     

    Go to your FirstTable table and click on a record, you will see a View element showing you all records in SecondTable linked to FirstTable. That View element is not a field so you can't switch your code around to search it or put data into it. I'm sure you have tried and it says "field is no data field: SecondTable".

     

    Which brings up a question about when do you see yourself creating a record first in SecondTable and then need to create a record in FirstTable. Currently if you are in your SecondTable and create a new record, you can then create a new record in FirstTable through the FirstTable field. Once you do that it will link the two records.

     

    Technically you can do something like:

     

    let curRec := this; <--gathers the currect record in SecondTable
    let n1T := (create FirstTable); <--creates a new record in FirstTable
    let c1T := last((select FirstTable) order by Id);<--finds the record with the highest Id number which we are assuming is the one we just created
    curRec.(FirstTable := c1T)<--links the current record in SecondTable to the newly created record in FirstTable

     

    But then it creates blank records in FirstTable. You could hard code data to enter into FirstTable, but what would be use in that.

     

    Let us know why you need to do this and we can figure something out.

    • Andrea.1
    • 3 yrs ago
    • Reported - view

    GREAT!!! Thank you so much!

    • Alain_Fontaine
    • 3 yrs ago
    • Reported - view

    I have an immoderate love for one-liners:

    FirstTable := (create FirstTable)

    If there is already a linked record, it will be replaced by the newly created one.

    • Fred
    • 3 yrs ago
    • Reported - view

    nice. didn't know that was possible.

    • yuxia_fan
    • 3 yrs ago
    • Reported - view

    nihao

    • Alain_Fontaine
    • 3 yrs ago
    • Reported - view

    The original script can also be reduced to:

    let myID := Id;
    (create SecondTable).(FirstTable := myID)

    Now, if some fields of the newly created record must receive a value, the temporary variable is still needed.

Content aside

  • 3 yrs agoLast active
  • 8Replies
  • 514Views