0

Button script: add new record from table B to current record in table A

I have linked tables in a somewhat complex database. I have a button that is running a script to create a record and other tasks, but the part that I am stuck on is how to tell Ninox to add the record that was just created in Table B to current record in Table A

let currentVehicle := this.Vehicle;
for i from 12 to 22 do
    let newVehicleService := (create 'Vehicle Service');
    newVehicleService.(
        Vehicle := currentVehicle;
        'Service Type' := i
    );
    let newServiceLog := (create 'Service Log');
    newServiceLog.('Vehicle Service' := newVehicleService);
    let this.'Service Log' := newServiceLog;
    void
end

If I leave 'this' from the beginning it will create a new Service Log, but it will not be linked to the current record. Adding 'this' produces a compile error. I've created a new Vehicle Service and made it part of the new Service Log, but I can't seem to handle adding the new Service Log to the current record.

3 replies

null
    • John_Halls
    • yesterday
    • Reported - view

    Hi

    Do you mean, turn this

    let newServiceLog := (create 'Service Log');
    newServiceLog.('Vehicle Service' := newVehicleService);
    let this.'Service Log' := newServiceLog;
    

    into this

    let newServiceLog := (create 'Service Log');
    newServiceLog.('Vehicle Service' := newVehicleService);
    currentVehicle.'Service Log' := newServiceLog;
    

    let statements define a variable, they can't be used to assign a value to a field.

    If I'm off the mark you might need to come back with a more detailed description of what you are trying to achieve.

    Regards John

      • svelte_untruth_0c
      • 18 hrs ago
      • Reported - view

       Thank you for the clarification on let. The table I am working in (the 'this' table) is an 'Inspection' table. So I have one or more 'Inspection's linked to a 'Vehicle'. More than one 'Service Log' linked to an 'Inspection'. One 'Vehicle Service' (a join table) linked to a 'Service Log'. One 'Vehicle' and one 'Service Type' linked to each 'Vehicle Service'

      There are a lot more tables in the database, but these are the relevant ones here. So I am trying to prefill 'Service Log's on the inspection that I can use as a to do list. The button is on the Inspection form. Right now I am able to create 'Vehicle Service' record and 'Service Log' record and add the 'Vehicle Service' record to the 'Service Log'. I just need to add the 'Service Log' to the current 'Inspection'

      I hope that is more clear.

    • Fred
    • 12 hrs ago
    • Reported - view
     said:
    The table I am working in (the 'this' table) is an 'Inspection' table. So I have one or more 'Inspection's linked to a 'Vehicle'. More than one 'Service Log' linked to an 'Inspection'.

    With this info you can try:

    let currRec := this;
    for i from 12 to 22 do
        let newVehicleService := (create 'Vehicle Service');
        newVehicleService.(
            Vehicle := currRec.Vehicle;
            'Service Type' := i
        );
        let newServiceLog := (create 'Service Log');
        newServiceLog.(
            'Vehicle Service' := newVehicleService
             Inspection := currRec
        );
        void
    end
    

    You originally wrote:

        let newServiceLog := (create 'Service Log');
        newServiceLog.('Vehicle Service' := newVehicleService);
        let this.'Service Log' := newServiceLog;
    

    It looks like you are trying to link the newServiceLog record to the current Inspection record by starting in the Inspection record.That is not how Ninox does it. Also what John said about 'let'. Since Service Log is the N (many) of the relationship you can not link a record, through code, from the 1 side.

    Which means you have to use the Inspection link on the Service Log side to link the records.

    You can not use 'this' when linking so I changed the first line to put the current record into a variable. And once you do that you can reference any field in the record you need, so I removed the currentVehicle variable and you can see how I changed line 5.

    Then you can see line 11 is where I link the new Service Log record to the current Inspection record through the new variable.

    Give it a try and let us know how it turns out.