0

Using Trigger to auto populate appointment in parent table based on entries in child table

Hi,

I“m still trying to figure out doing things the ninox way, but somewhat I”m stuck. Suppose I have a Table (Trips) and a child table (Stopovers). What I am trying to achieve ist to autopopulate Trips.Appointment with first and last stopover on trigger “add new record”. Basically I want to have the begin and end of a trip autocalculated and inserted in the record of the parent table.

 

So Trips -> TripID, Name, FromUntil

Stopovers -> TripID, DateTime, Location

In Metacode what I want to achieve is something like:

every time a new record is added to Stopover,

   this TripID = TripID of parent record

    let p:= select first(Stopover.DateTime) of this TripID;

    let q:= select last(Stopover.DateTime) of this TripID;

    populate (ParentRecord)Trips.FromUntil with appointment(p, q)

 

Been trying to copy TripID from parent table to stopover table in order to reference the correct parent record, but no luck. 

this is the current version of the code (triggered by new record in stopovers table):

TripID := Trips.TripID;
let p := first(select Stopovers where TripID = Trips.TripID);
let q := last(select Stopovers where TripID = Trips.TripID);
let z := appointment(p.DateTime, q.DateTime);
Trips.FromUntil := z

somewhat I seem to miss the correct method of referencing the parent record or my select statement is incorrect. First line works (copying calling parent record ID to the new stopover record). So the basic question is how do I back-reference the “calling record” when adding new records to the child table? 

I would be appreciating any pointers helping me in the right direction!

regards,

    Chris

1 reply

null
    • Developer
    • Disponent
    • 4 yrs ago
    • Reported - view

    Long story, short answer :-)

    I found the reasong for my problems - had to learn that in child tables, trigger functions MUST be located in the “on update” trigger, not in the “on create” trigger. Reference found in training video 400.08- Working With Triggers from Nioxus starting at approx. 30“00”“

    Not really extremely logical though, I wonder how many people have stumbled over this already....