web
You’re offline. This is a read only version of the page.
close
Skip to main content

Announcements

No record found.

News and Announcements icon
Community site session details

Community site session details

Session Id :
Business Central forum

Strange behaviour reading from table and inserting into temporary table

(0) ShareShare
ReportReport
Posted on by

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.

Screenshot-at-Mar-07-20_2D00_29_2D00_15.png

Below is it working properly on a standard purchase order

Screenshot-at-Mar-07-20_2D00_52_2D00_53.png

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

Screenshot-at-Mar-07-20_2D00_54_2D00_56.png

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;



Categories:
  • Suggested answer
    Stefano Demiliani Profile Picture
    37,172 Most Valuable Professional on at

    I see a strange loop (that seems incorrect):

    If ShipmentLines.FIND('-') THEN

    ...

    until Line.NEXT = 0;

  • Rufus Profile Picture
    on at

    In hindsight the cut and paste error where only one symbol was changed looks obvious. Thank you.

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

November Spotlight Star - Khushbu Rajvi

Congratulations to a top community star!

Forum Structure Changes Coming on 11/8!

In our never-ending quest to help the Dynamics 365 Community members get answers faster …

Dynamics 365 Community Platform update – Oct 28

Welcome to the next edition of the Community Platform Update. This is a status …

Leaderboard > Business Central

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans