0

Button to populate a separate table from another table (and sub table)

Hi,

I hope you can help. I'm new to Ninox and have read a lot of the manual and watched some youtube examples but I'm still struggling with a little of the syntax.

I have 3 tables

Purchase Orders

PO Items (composite sub-table of Purchase orders)

Stock table

When a New Purchase Order is created (including PO line Items) I want to create a new record in the Stock table using some of the fields on Purchase Orders and PO item. I have managed to get a lot of it working with the following code on a button...

let xPODate := 'Purchase Date';
let xPONum := 'PO Number';
let xPOID := number(this.Id);
for p in select 'PO Items' do
let xProduct := p.Product;
let xQuant := p.Quantity;
let xPricePerUnit := p.'Price Per Unit';
let xPandP := p.'P&P Per Unit';
let xSup := Suppliers.Name;
let i := (create Stock);
i.(Product := xProduct);
i.('Date Purchased' := xPODate);
i.('PO Number' := xPONum);
i.Seller.(Name := xSup);
i.(Quantity := xQuant);
i.('Price Paid' := xQuant * xPricePerUnit);
i.(Status := "AWAITING DELIVERY")
end

The Stock table update works fine, but I cannot restrict the PO Items on my select to be just the PO Items for the related Purchase Order. The above code populates all records from PO Items into Stock. I have tried adding a where clause to the select but am unsure what field to use to join between Purchase Order and its many PO Items.

How does a table link to its sub table? I cannot see any field on the PO Item table that shows the parent table id. I assume this field is hidden from view - is it possible to see the raw database info somehow?

Thanks,
Robin

5 replies

null
    • Ninox partner
    • RoSoft_Steven.1
    • 3 yrs ago
    • Reported - view

    Try this:

    let xPODate := 'Purchase Date';
    let xPONum := 'PO Number';
    let xPOID := number(this.Id);
    let me := this;
    for p in select 'PO Items' where 'Purchase Orders' = me do
    let xProduct := p.Product;
    let xQuant := p.Quantity;
    let xPricePerUnit := p.'Price Per Unit';
    let xPandP := p.'P&P Per Unit';
    let xSup := Suppliers.Name;
    let i := (create Stock);
    i.(Product := xProduct);
    i.('Date Purchased' := xPODate);
    i.('PO Number' := xPONum);
    i.Seller.(Name := xSup);
    i.(Quantity := xQuant);
    i.('Price Paid' := xQuant * xPricePerUnit);
    i.(Status := "AWAITING DELIVERY")
    end

    Steven

    • Orson & Welles
    • Robin_Yacoubian
    • 3 yrs ago
    • Reported - view

    Thanks Steven,

    I tried that but for one Purchase Order with two PO Item lines under it it only populated the Stock table with the last PO Item (and not both).

    Do you know why? IIs it possible to put any debugging info like msgbox or anything to help see which records it is going through?

    Thanks,
    Robin

    • Orson & Welles
    • Robin_Yacoubian
    • 3 yrs ago
    • Reported - view

    Sorry Steven, it does work! Thanks. One final bit that isn't working is the population of the Seller field. That is actual a table reference itself (from the Seller table) on the Stock table. Is it possible to populate a linking field this way? Thanks

    • Ninox partner
    • RoSoft_Steven.1
    • 3 yrs ago
    • Reported - view

    Try with change this ?:

    ....
    let xSup := Suppliers;
    ....

    i.(Seller := xSup);

    • Orson & Welles
    • Robin_Yacoubian
    • 3 yrs ago
    • Reported - view

    That works a treat! Fantastic. Thanks a lot Steven

Content aside

  • 3 yrs agoLast active
  • 5Replies
  • 621Views