I have a subform page in Business Central that is using fields on the parent (header) page to conditionally fill in a temporary table with either line items from a purchase order or a extension table called brokerage shipment lines.
This works perfectly on the purchase order table. However, on the extension table it strangely duplicates the one table row up to ten times. The debugger also fails on me when using it in the cloud sandbox, so it is difficult to see what is going on.
Pictures below is the brokerage shipment. You can see only one line item.

Below is it working properly on a standard purchase order

And here are the repeated lines that occur if it is reading from the brokerage shipment.

Below is the core code from a procedure that is called from the parent page in the oncurrentrecord event and it passes through some key attributes.
procedure InitTempTable(var paramOrderReference: code[20]; var paramOrderType: Option; var paramLandedCostTemplate: code[20])
var
Line: Record "Purchase Line";
Order: Record "Purchase Header";
Item: Record Item;
Shipment: Record BrokerageShipment;
ShipmentLines: Record "Brokerage Shipment Line";
ContainerEntry: Record "Container Entry";
Vendor: Record Vendor;
BuyFromVendorNo: Code[20];
CodePricing: Codeunit CommonPricingLogic;
CodeLandedCost: Codeunit LandedCostLogic;
TotalWeight: Decimal;
TotalQuantity: Decimal;
TotalValue: Decimal;
LineWeight: Decimal;
TempLineAlloc: Decimal;
TempLineUnitAlloc: Decimal;
TempEstAlloc: Decimal;
TempTotal: Decimal;
TempPriceUnit: Option;
TempLines: Integer;
begin
case paramOrderType of
ContainerEntry.Type::"Brokerage Sale":
begin
Rec.Reset();
Rec.DeleteAll();
ShipmentLines.Reset();
ShipmentLines.SetFilter("Document No.", paramOrderReference);
LineNo := 0;
If ShipmentLines.FIND('-') THEN
REPEAT
Rec."Item Code" := ShipmentLines."Item No.";
Item.Get(ShipmentLines."Item No.");
If Shipment.Get(ShipmentLines."Document No.") then begin
BuyFromVendorNo := Shipment."Buy From Vendor No.";
Vendor.get(BuyFromVendorNo);
end;
Rec.OrderReference := paramOrderReference;
Rec.LineNo := LineNo + 10;
Rec."Item Description" := ShipmentLines.Description;
Rec.UnitOfMeasure := Item."Base Unit of Measure";
Rec.Quantity := ShipmentLines.Quantity;
Rec."Unit Cost" := 0;
LineWeight := ShipmentLines.Quantity * Item."Net Weight";
Rec."Price Unit" := Vendor."Vendor Price Unit"::MT;
Rec."Price Unit Cost" := ShipmentLines."Agreed Price";
CodePricing.CalculateUnitPriceByPriceUnit(ShipmentLines."Item No.", Item."Base Unit of Measure", BuyFromVendorNo, ShipmentLines."Agreed Price");
//Error gettig caught in loop on this field.
// Need to Add Code Unit to Landed Cost to Calculate and Distribute Costs given a landed cost
rec."Price Unit Alloc." := 0;
rec."Price Unit. Incl. Alloc." := 0;
rec."Line Allocation" := 0;
rec."Line Total" := ShipmentLines.Amount;
INSERT();
UNTIL Line.NEXT() = 0;
end;
ContainerEntry.Type::"Purchase Order":
begin
Order.SetRange("Document Type", Line."Document Type"::Order);
Order.SetRange("No.", paramOrderReference);
If Order.FindFirst() then begin
Vendor.Get(Order."Buy-from Vendor No.");
TempPriceUnit := Vendor."Vendor Price Unit";
end;
Rec.Reset();
Rec.DeleteAll();
Line.SetRange("Document Type", Line."Document Type"::Order);
Line.SetRange("Document No.", paramOrderReference);
Line.SetRange(Type, Line.Type::Item);
If Line.FINDSET() then
repeat
TotalQuantity += Line.Quantity;
TotalValue += Line.Amount;
TotalWeight += (Line."Net Weight" * Line.Quantity);
UNTIL Line.Next() = 0;
If Line.FINDSET() THEN
REPEAT
Rec."Item Code" := Line."No.";
Rec.OrderReference := paramOrderReference;
Rec.LineNo := Line."Line No.";
Rec."Item Description" := Line.Description;
Rec.UnitOfMeasure := Line."Unit of Measure Code";
Rec.Quantity := Line.Quantity;
Rec."Unit Cost" := Line."Unit Cost";
LineWeight := Line.Quantity * Line."Net Weight";
Rec."Price Unit" := TempPriceUnit;
Rec."Price Unit Cost" := CodePricing.CalculatePriceByPriceUnit(Line."No.", line."Unit of Measure Code", Line."Buy-from Vendor No.", line."Unit Cost");
//Error gettig caught in loop on this field.
// Need to Add Code Unit to Landed Cost to Calculate and Distribute Costs given a landed cost
TempLineAlloc := CodeLandedCost.AllocateCostsToLine(paramLandedCostTemplate, TotalWeight, TotalQuantity, TotalValue, LineWeight, Line.Amount, Line.Quantity);
TempLineUnitAlloc := TempLineAlloc / Line.Quantity;
TempEstAlloc := CodePricing.CalculatePriceByPriceUnit(Line."No.", line."Unit of Measure Code", Line."Buy-from Vendor No.", TempLineUnitAlloc);
TempTotal := TempEstAlloc + Rec."Price Unit Cost";
rec."Price Unit Alloc." := TempEstAlloc;
rec."Price Unit. Incl. Alloc." := TempTotal;
rec."Line Allocation" := TempLineAlloc;
rec."Line Total" := Line.Amount;
INSERT();
UNTIL Line.NEXT() = 0;
end;
end;
end;