Skip to content

Commit 8378292

Browse files
fix: reset TempOrderLine between iterations in SetAndInsertOrderLines to prevent Tip flag from persisting across order lines (#7787)
Fixes #6811 ## Problem In codeunit 30161 Shpfy Import Order, the procedure SetAndInsertOrderLines iterates over a JsonArray of order lines and passes TempOrderLine by reference to SetOrderLineValuesFromJson. When a line named Tip with a null product is encountered, the Tip field is set to true on the record. Because TempOrderLine is never re-initialized between iterations, the next line in the loop inherits all field values including Tip = true from the previous iteration. This causes every order line that follows a Tip line to be incorrectly classified as a Tip, even standard item lines. The stale-value problem only manifests for new lines. When LocalOrderLine.Get succeeds (the line already exists in the database), OrderLine.Copy(LocalOrderLine) overwrites all fields. For brand-new lines, no such reset occurs, so the previous iteration's values persist. ## Solution Add TempOrderLine.Init() at the start of the foreach loop body in SetAndInsertOrderLines. This resets all fields to their default values before SetOrderLineValuesFromJson populates them from the JSON token, ensuring each iteration starts from a clean state. ## Changes - ShpfyImportOrder.Codeunit.al: Wrapped the foreach body in a begin...end block and added TempOrderLine.Init() before calling SetOrderLineValuesFromJson. - ShpfyOrdersAPITest.Codeunit.al: Added UnitTestTipFlagNotPropagatedToSubsequentOrderLines, which imports a Tip line followed by a regular line using ImportCreateAndUpdateOrderLinesFromMock and asserts that only the Tip line has Tip = true. ## How to Test 1. In a Business Central environment with the Shopify connector, create a Shopify order containing a Tip item followed by one or more regular items. 2. Sync the order to Business Central. 3. Open the imported Shopify Order and inspect the order lines. 4. Expected: only the Tip line has the Tip indicator set. Subsequent lines do not inherit the Tip flag. Alternatively, run the automated test UnitTestTipFlagNotPropagatedToSubsequentOrderLines in codeunit 139608 Shpfy Orders API Test. The test fails without the fix and passes with it.
1 parent 142447a commit 8378292

2 files changed

Lines changed: 53 additions & 1 deletion

File tree

src/Apps/W1/Shopify/App/src/Order handling/Codeunits/ShpfyImportOrder.Codeunit.al

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -636,11 +636,13 @@ codeunit 30161 "Shpfy Import Order"
636636
var
637637
JOrderLine: JsonToken;
638638
begin
639-
foreach JOrderLine in JOrderLines do
639+
foreach JOrderLine in JOrderLines do begin
640+
TempOrderLine.Init();
640641
if SetOrderLineValuesFromJson(JOrderLine, OrderId, TempOrderLine) then begin
641642
TempOrderLine.Insert();
642643
DataCaptureDict.Add(TempOrderLine."Line Id", JOrderLine);
643644
end;
645+
end;
644646
end;
645647

646648
internal procedure TranslateCurrencyCode(ShopifyCurrencyCode: Text): Code[10]

src/Apps/W1/Shopify/Test/Order Handling/ShpfyOrdersAPITest.Codeunit.al

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1486,6 +1486,56 @@ codeunit 139608 "Shpfy Orders API Test"
14861486
LibraryAssert.AreEqual(OrderHeader."Shopify Order No.", SalesHeader."No.", 'Sales Invoice number should equal Shopify Order No.');
14871487
end;
14881488

1489+
[Test]
1490+
procedure UnitTestTipFlagNotPropagatedToSubsequentOrderLines()
1491+
var
1492+
OrderLine: Record "Shpfy Order Line";
1493+
ImportOrder: Codeunit "Shpfy Import Order";
1494+
JOrderLines: JsonArray;
1495+
JEmptyArray: JsonArray;
1496+
JTipLine: JsonObject;
1497+
JRegularLine: JsonObject;
1498+
JNull: JsonValue;
1499+
GidLbl: Label 'gid://shopify/LineItem/%1', Locked = true, Comment = '%1 = Line Id';
1500+
OrderId: BigInteger;
1501+
TipLineId: BigInteger;
1502+
RegularLineId: BigInteger;
1503+
begin
1504+
// [SCENARIO] When an order contains a Tip line followed by a regular line, the Tip flag must not carry over to the regular line.
1505+
Initialize();
1506+
1507+
OrderId := LibraryRandom.RandIntInRange(100000, 999999);
1508+
TipLineId := LibraryRandom.RandIntInRange(10000, 49999);
1509+
RegularLineId := LibraryRandom.RandIntInRange(50000, 99999);
1510+
1511+
// [GIVEN] A Tip order line in JSON form
1512+
JNull.SetValueToNull();
1513+
JTipLine.Add('id', StrSubstNo(GidLbl, TipLineId));
1514+
JTipLine.Add('name', 'Tip');
1515+
JTipLine.Add('product', JNull);
1516+
JTipLine.Add('discountAllocations', JEmptyArray);
1517+
1518+
// [GIVEN] A regular order line in JSON form that follows the Tip line
1519+
JRegularLine.Add('id', StrSubstNo(GidLbl, RegularLineId));
1520+
JRegularLine.Add('name', 'Product');
1521+
JRegularLine.Add('discountAllocations', JEmptyArray);
1522+
1523+
JOrderLines.Add(JTipLine);
1524+
JOrderLines.Add(JRegularLine);
1525+
1526+
// [WHEN] Order lines are imported from the JSON array
1527+
ImportOrder.ImportCreateAndUpdateOrderLinesFromMock(OrderId, JOrderLines);
1528+
Commit();
1529+
1530+
// [THEN] The Tip line has the Tip flag set to true
1531+
LibraryAssert.IsTrue(OrderLine.Get(OrderId, TipLineId), 'Tip order line must exist');
1532+
LibraryAssert.IsTrue(OrderLine.Tip, 'Tip flag must be set on the Tip order line');
1533+
1534+
// [THEN] The regular line does not have the Tip flag carried over from the previous Tip line
1535+
LibraryAssert.IsTrue(OrderLine.Get(OrderId, RegularLineId), 'Regular order line must exist');
1536+
LibraryAssert.IsFalse(OrderLine.Tip, 'Tip flag must not be propagated to the subsequent regular order line');
1537+
end;
1538+
14891539
local procedure CreateTaxArea(var TaxArea: Record "Tax Area"; var ShopifyTaxArea: Record "Shpfy Tax Area"; ShopParam: Record "Shpfy Shop")
14901540
var
14911541
ShopifyCustomerTemplate: Record "Shpfy Customer Template";

0 commit comments

Comments
 (0)