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

Community site session details

Session Id :
Small and medium business | Business Central, N...
Answered

Adding serialized item tracking details for a Purchase Return Order line

(0) ShareShare
ReportReport
Posted on by 540

Similar scenario to my (thankfully resolved) post for Transfer Order lines -->    The goal is for the enduser to barcode scan or hand-type the Serial No. for an item into the No. field of the Purchase Return Order line. My AL code would then intercept that value before validation, backtrace to determine the corresponding Item No. value, and then plug that back into the No. field. So that the proper Item No. is referenced in the line.

The second step after validation would involve automatically adding the reservation entry for the serialized item. As was demonstrated in TeddyH 's post -->    . The issue is that my AL code doesn't even get to the point of fully inserting the full row contents into the Purchase Line table. Only partial data is contained in there when I look in SQL. When I debug in VS Code, just prior to my row insert I placed a breakpoint and can see all of the field values listed correctly. Yet they aren't hitting SQL so the record isn't complete.

Below is my AL code. I commented out the OnAfterValidate() trigger that handles the reservation entry for the serialized item, since the purchase line doesn't even list all of the proper field elements. This similar exercise worked fine for the Transfer Order line customization I implemented in my linked post above. Yet for the Purchase Return line it fails. Any suggestions? 

tableextension 50104 "DchApi_ReturnTableExt" extends "Purchase Line"
{
    fields
    {
        modify("No.")
        {
            trigger OnBeforeValidate()
            var
                Rec_ILE: Record "Item Ledger Entry";
                Rec_ReturnLine: Record "Purchase Line";
                Rec_Item: Record Item;
                Rec_ReturnHdr: Record "Purchase Header";
                Rec_Track: Record "Tracking Specification";
                ItemNo: Code[20];
                SerialNo: Code[20];
                ShortDim1Code: Code[20];
                ShortDim2Code: Code[20];
                Description: Text[100];
                GenProdPostGrp: Code[20];
                InvPostGrp: Code[20];
                ItemCatCode: Code[20];
                TaxAreaCode: Code[20];
                VatBusPostGroup: Code[20];
                VatProdPostGroup: Code[20];
                UoMCode: Code[10];
                UnitCost: Decimal;
                UnitPrice: Decimal;
                LineNo: Integer;
                LocationCode: Code[10];
                VendorCode: Code[20];
                AllowInvDisc: Boolean;
                RecalcInvDisc: Boolean;
                VendorItemNo: Text[100];
            begin
                if Rec."Document Type" = Rec."Document Type"::"Return Order" then begin
                    SerialNo := Rec."No.";
                    Rec_ILE.Reset();
                    Rec_ILE.SetRange("Entry Type", Rec_ILE."Entry Type"::Purchase);
                    Rec_ILE.SetRange("Item Tracking", Rec_ILE."Item Tracking"::"Serial No.");
                    Rec_ILE.SetFilter("Serial No.", '%1', SerialNo);
                    if Rec_ILE.FindFirst() then begin
                        ItemNo := Rec_ILE."Item No.";

                        Rec_Item.Reset();
                        Rec_Item.SetFilter("No.", ItemNo);
                        if Rec_Item.FindFirst() then begin
                            ShortDim1Code := Rec_ILE."Global Dimension 1 Code";
                            ShortDim2Code := Rec_ILE."Global Dimension 2 Code";
                            Description := Rec_Item.Description;
                            GenProdPostGrp := Rec_Item."Gen. Prod. Posting Group";
                            InvPostGrp := Rec_Item."Inventory Posting Group";
                            ItemCatCode := Rec_Item."Item Category Code";
                            UoMCode := Rec_Item."Base Unit of Measure";
                            UnitCost := Rec_Item."Last Direct Cost";
                            VendorCode := Rec_Item."Vendor No.";
                            UnitPrice := Rec_Item."Unit Price";
                            AllowInvDisc := Rec_Item."Allow Invoice Disc.";
                            if AllowInvDisc = true then
                                RecalcInvDisc := true
                            else
                                RecalcInvDisc := false;
                            VendorItemNo := Rec_Item."Vendor Item No.";
                            VatBusPostGroup := Rec_Item."VAT Bus. Posting Gr. (Price)";
                            VatProdPostGroup := Rec_Item."VAT Prod. Posting Group";

                            Rec_ReturnHdr.Reset();
                            Rec_ReturnHdr.SetRange("No.", Rec."Document No.");
                            if Rec_ReturnHdr.FindFirst() then begin
                                Rec_ReturnLine.Reset();
                                Rec_ReturnLine.SetRange("Document No.", Rec."Document No.");
                                if Rec_ReturnLine.FindLast() then
                                    LineNo := Rec_ReturnLine."Line No."   10000
                                else
                                    LineNo := 10000;

                                Validate(Rec."Document No.");
                                Validate(Rec."Document Type");
                                Validate(Rec.Type, Rec.Type::Item);
                                Validate(Rec."Line No.", LineNo);
                                Validate(Rec."No.", ItemNo);
                                Validate(Rec.Description, Description);
                                Validate(Rec."Description 2", SerialNo);
                                gblSerialNo := Rec."Description 2";
                                Validate(Rec."Variant Code", '');
                                Validate(Rec."Shortcut Dimension 1 Code", ShortDim1Code);
                                Validate(Rec."Shortcut Dimension 2 Code", ShortDim2Code);
                                Validate(Rec.Description, Description);
                                Validate(Rec."Gen. Prod. Posting Group", GenProdPostGrp);
                                Validate(Rec."Item Category Code", ItemCatCode);
                                Validate(Rec."VAT Bus. Posting Group", VatBusPostGroup);
                                Validate(Rec."VAT Prod. Posting Group", VatProdPostGroup);
                                Validate(Rec."Posting Group", InvPostGrp);
                                Validate(Rec."VAT Identifier", VatProdPostGroup);
                                Validate(Rec."Tax Area Code", 'ATLANTA, GA');   // Need to find reference link.
                                Validate(Rec."Prepayment Tax Area Code", 'ATLANTA, GA');    // Need to find reference link.
                                Validate(Rec.Quantity, 1);
                                Validate(Rec."Quantity (Base)", 1);
                                Validate(Rec."Outstanding Quantity", 1);
                                Validate(Rec."Outstanding Qty. (Base)", 1);
                                Validate(Rec."Qty. to Invoice", 1);
                                Validate(Rec."Qty. to Invoice (Base)", 1);
                                Validate(Rec."Return Qty. to Ship", 1);
                                Validate(Rec."Qty. to Receive (Base)", 1);
                                Validate(Rec."Unit of Measure Code", UoMCode);
                                Validate(Rec."Unit of Measure", 'Piece');   // Need to find reference link.
                                Validate(Rec."Qty. per Unit of Measure", 1);
                                Validate(Rec."Return Qty. to Ship", 1);
                                Validate(Rec."Return Qty. to Ship (Base)", 1);
                                Validate(Rec."Direct Unit Cost", UnitCost);
                                Validate(Rec."Unit Cost", UnitCost);
                                Validate(Rec."Unit Cost (LCY)", UnitCost);
                                Validate(Rec."VAT Base Amount", UnitCost);
                                Validate(Rec.Amount, UnitCost);
                                Validate(Rec."Amount Including VAT", UnitCost);
                                Validate(Rec."Line Amount", UnitCost);
                                Validate(Rec."Outstanding Amount", UnitCost);
                                Validate(Rec."Outstanding Amount (LCY)", UnitCost);
                                Validate(Rec."Outstanding Amt. Ex. VAT (LCY)", UnitCost);
                                Validate(Rec."Unit Price (LCY)", UnitPrice);
                                Validate(Rec."Allow Invoice Disc.", AllowInvDisc);
                                Validate(Rec."Recalculate Invoice Disc.", RecalcInvDisc);
                                Validate(Rec."Dimension Set ID", 30);   // Need to find reference link.
                                Validate(Rec."Pay-to Vendor No.", VendorCode);
                                Validate(Rec."Vendor Item No.", VendorItemNo);
                                Validate(Rec."Item Reference No.", "Vendor Item No.");
                                Validate(Rec."Item Reference Unit of Measure", UoMCode);
                                Validate(Rec."Item Reference Type", Rec."Item Reference Type"::Vendor);
                                Validate(Rec."Item Reference Type No.", VendorCode);
                                Validate(Rec."Allow Item Charge Assignment", true);
                                Validate(Rec."Price Calculation Method", Rec."Price Calculation Method"::"Lowest Price");
                                Rec_Track.SetFilter("Serial No.", gblSerialNo);
                                if Rec_Track.FindLast() then
                                    LocationCode := Rec_Track."Location Code"
                                else
                                    LocationCode := 'SAW';
                                Validate(Rec."Location Code", LocationCode);
                                Rec.Insert();
                                Commit();
                            end;
                        end;
                    end;
                end;
            end;

            /*trigger OnAfterValidate()
            var
                Rec_ResEnt: Record "Reservation Entry" temporary;
                Rec_ResEnt_Lu: Record "Reservation Entry";
                CreateReservEntry: Codeunit "Create Reserv. Entry";
                ReservStatus: Enum "Reservation Status";
                PurchaseDocType: Enum "Purchase Document Type";
            begin
                if Rec."Document Type" = Rec."Document Type"::"Return Order" then begin
                    Rec_ResEnt.Init();
                    Rec_ResEnt_Lu.Reset();
                    if Rec_ResEnt_Lu.FindLast() then
                        Rec_ResEnt."Entry No." := Rec_ResEnt_Lu."Entry No."   1
                    else
                        Rec_ResEnt."Entry No." := 1;
                    Rec_ResEnt."Expiration Date" := Today();
                    Rec_ResEnt.Quantity := -1;
                    Rec_ResEnt."Serial No." := gblSerialNo;
                    Rec_ResEnt."Source Subtype" := Rec_ResEnt."Source Subtype"::"5";
                    Rec_ResEnt."Item Tracking" := Rec_ResEnt."Item Tracking"::"Serial No.";
                    Rec_ResEnt.Insert();

                    CreateReservEntry.SetDates(0D, Rec_ResEnt."Expiration Date");
                    CreateReservEntry.CreateReservEntryFor(Database::"Purchase Line", PurchaseDocType.AsInteger(),
                        "Document No.", '', 0, "Line No.", "Qty. per Unit of Measure",
                        Rec_ResEnt.Quantity, Rec_ResEnt.Quantity * "Qty. per Unit of Measure", Rec_ResEnt);
                    CreateReservEntry.CreateEntry("No.", "Variant Code", "Location Code", '', "Expected Receipt Date", 0D, 0, ReservStatus::Surplus);
                end;
            end;*/
        }
    }
    var
        gblSerialNo: Code[20];
}
  

I have the same question (0)
  • Verified answer
    Marco Mels Profile Picture
    on at
    RE: Adding serialized item tracking details for a Purchase Return Order line

    Hello,

    We currently do not have dedicated Dev support via the Dynamics 365 Business Central forums, but I wanted to provide you some additional resources to assist. If you need assistance with debugging or coding I would recommend discussing this on one of our communities.

    www.yammer.com/dynamicsnavdev

    dynamicsuser.net/.../developers

    I will open this up to the community in case they have something to add.

    Thanks.

  • Greg Kujawa Profile Picture
    540 on at
    RE: Adding serialized item tracking details for a Purchase Return Order line

    The second link you sent appears to be unavailable or broken. The first one I followed and requested access, so thank you for the point in the right direction!

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

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Pallavi Phade – Community Spotlight

We are honored to recognize Pallavi Phade as our Community Spotlight honoree for…

Leaderboard > Small and medium business | Business Central, NAV, RMS

#1
OussamaSabbouh Profile Picture

OussamaSabbouh 2,214

#2
Sumit Singh Profile Picture

Sumit Singh 2,145

#3
YUN ZHU Profile Picture

YUN ZHU 1,887 Super User 2025 Season 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans