0

Create / Update Record

Hi,

I am relatively new to Ninox, but finding it extremely powerful. I have however encountered a challenge that I am struggling with and hope someone can help point me in the right direction...

I have two, unrelated tables (Orders and Expenses). On the Order table there is an Postage field that is used to capture the cost of postage of a given order. When this field is completed, I would like to create a row in the Expenses table to record the date, description and amount. I have managed to achieve this by using this Trigger After Update script on the Order table:

if Postage != null then
   let d := Date;
   let desc := "Postage for Order: " + 'Order Number';
   let amt := Postage;
   let e := (create Expenses);
   e.(Date := d);
   e.(Description := desc);
   e.(Amount := amt)
end

This works fine, but the problem I have is that if I then go back into the Order record and update the postage field, it then creates a new record in the Expenses table. I understand why this is as both conditions are true (the table has been updated and the Postage filed is not null).

The behaviour I am trying to achieve is that when the Postage field is first completed a record in the Expenses table is created (as above). Should the postage be then updated, I would like to update the record in the Expenses table where the 'Order Number' is equal to the Order Number on the Orders table. Resulting in only 1 record in the Expenses table regardless of how many times it gets updated.

I hope this makes sense. Any help would be appreciated.

1 reply

null
    • John_Halls
    • 3 yrs ago
    • Reported - view

    Hi Dave

     

    I would create a relationship between Orders and Expenses and update your code to this:-

     

    let a := first(Expenses);
    if Postage != 0 and Postage != null then
    let d := Date;
    let desc := "Postage for Order: " + 'Order Number';
    let amt := Postage;
    if a = null then
    let b := this;
    let e := (create Expenses);
    e.(
    Orders := b;
    Date := d;
    Description := desc;
    Amount := amt
    )
    else
    a.(
    Date := d;
    Description := desc;
    Amount := amt
    )
    end
    else
    delete a
    end

     

    Note I am checking for null or 0 and it will delete the expenses record if Postage become null or 0.

     

    Regards John