Hi Everyone,
I have added a barcode field to the PO screen. The functionality is working fine, but I am facing an issue where, whenever I scan a product for the first time, the OrderQty is automatically set to 2. When I scan the same product again, the OrderQty increases to 3 which is correct .
But i for the first time it should be 1 not 2.
However, for a different product, the OrderQty is correctly set to 1 on the first scan. I have attempted to resolve this issue but have not been successful yet.
Please guide me on how to fix this.
Thanks!
Code:
// Check if a POLine for this InventoryID already exists
POLine existingLine = Base.Transactions
.Select()
.RowCast<POLine>()
.FirstOrDefault(l => l.InventoryID == item.InventoryID);
if (existingLine != null)
{
PXTrace.WriteInformation($"Existing Line Found: {existingLine.InventoryID}, Current Qty: {existingLine.OrderQty}");
// If first scan, ensure OrderQty starts at 1
if (existingLine.OrderQty == null || existingLine.OrderQty == 0)
{
PXTrace.WriteInformation("First Scan Detected, Setting OrderQty to 1");
existingLine.OrderQty = 1;
}
else
{
PXTrace.WriteInformation("Incrementing Quantity by 1");
existingLine.OrderQty += 1;
}
Base.Transactions.Update(existingLine);
}
else
{
PXTrace.WriteInformation("No Existing Line Found. Creating New Line.");
POLine newLine = new POLine
{
InventoryID = item.InventoryID,
OrderQty = 1,
CuryUnitCost = item.BasePrice
};
newLine = Base.Transactions.Insert(newLine);
Base.Transactions.Cache.SetValue<POLine.orderQty>(newLine, 1);
}