I have contracts table, that contains multiple products, there is a many-to-many relationship between contracts and products, this yields the link table [Contract-lineitem]: While converting a contract into a purchase order, I would like read all the products but also pick extra details from contract-lineitem table that are not contained in the products table: How do I go about this during the creation of the purchase order from contracts?
-
First create the purchase order:
-
Then iterate over the contract items and update purchase order items tables:
-
Check if product in the current contract exists in the newly created purchase order items
-
If not create it and return the newly created purchase order item in the view:
-
else update the existing products in the purchase order item with the correct details and return the updated record:
So here is what I have come up with, does anyone have a better approach?
(
vCurrent_Contract.Products->collect(p|
po.Products.add(p) -- add products to purchase order
);
--iterate over the contract items and update purchase order items
vCurrent_Contract.ContractItem->collect(cItem |
let poItem = po.PurchaseOrderItem->select(i|i.Products=cItem.Products)->first in
if poItem.isNull then
(
let nupoItem = PurchaseOrderItem.Create in
(
nupoItem.Quantity:=cItem.Quantity;
nupoItem.Disc:=cItem.Disc;
nupoItem.Subtotal:=cItem.Subtotal;
po.PurchaseOrderItem.add(nupoItem);
nupoItem --return the newly created purchase order item
)
)
else
(
poItem.Quantity := cItem.Quantity;
poItem.Subtotal:= cItem.Subtotal;
poItem.Disc = cItem.Disc;
poItem -- return the updated purchase order items
)
endif
);
po
)```
3