Hello @reinaerd
Achieving what You are looking for is complicated. I hope all that I put down here will make sense.
First, there are a few flows that we need to sort in Your logic to make sure You are not running into some unexpected error.
1. There is one available location per product, so the relationship One-To-One means You need to find a way to remove locations currently used by live products that were related to each location.
2. You must prevent any products from being added when Your Storage / Warehouse runs out of available Locations.
3. Also, You need to notify the user that's what happens so the user can investigate and make space if possible.
Here is my proposal for what You could implement:
To your List of Locations, add column Text called "Status" and make a mass edit to change all records to "Empty"
Next, apply this code to the Button that is currently patching your Scanner input:
Important make sure the names of Columns and Lists are maching and I did not make any mistakes I try place names to match Your Screens, but I could missspeal something.
If(
CountRows(
Filter(
Shelving,
Status = "Empty"
).Titel
) <= 0,
Notify("No Available Locations Make Space"),
Set(
varLoc,
Patch(
'Barcode Scans',
Defaults('Barcode Scans'),
{
Titel: BarcodeScanner2.Value,
'Barcode Type': BarcodeScanner2.Type,
'Scanned Person':User().FullName,
'Scanned time': Now(),
Locatie: First(
Shuffle(
Filter(
Shelving,
Status = "Empty"
)
)
).Titel
}
)
)
);
Patch(
Shelving,
LookUp(
Shelving,
Titel = varLoc.Locatie
),
{Status: "In Use"}
)
Now the only process You will need to implement is when You will change the status of the Location back to empty if this will be a manual process when someone doing an audit and changing the status, or it will be automated example when the product will run out I leave this to You.
hope this help