Skip to main content

Notifications

Dynamics 365 Community / Blogs / Jesús Almaraz blog / Add fields in Item tracking...

Add fields in Item tracking lines with events

The base of this entry is Olof Simren outstanding post http://www.olofsimren.com/add-fields-to-the-item-tracking-lines/ In This post Olof Simren showed us how to add a field in the Item tracking line. If you have worked with Item tracking lines you know what I mean. Sometimes we need an additional field, and without previous knowledge was very difficult to do and understand, and this is that Olof Simren post gives us.

In this example, I go to add the field alcoholic strength (I call it grade, more used in this market). For people of the food and drinks market we know how important , could be this information, and in some products could be variable for each lot.
I will follow the same steps of original Post.

Adding fields in the tables and pages

We have to add the new field in  these tables: Item Journal Line, Reservation Entry, Item Ledger Entry and "Tracking Specification":
tableextension 69001 "AFT Tracking Specification" extends "Tracking Specification"
{
    fields
    {
        field(69001; "AFT Grade"; Decimal)
        {
            DataClassification = CustomerContent;
            Caption = 'Grado';
            DecimalPlaces = 2 : 2;
The Modified Pages must be these: "Item Journal", "Item Ledger Entries" and "Item Tracking Lines":
pageextension 69001 "AFT Item Tracking Lines" extends "Item Tracking Lines"
{
    layout
    {
        addlast(Control1)
        {
            field(Grade; "AFT Grade")
            {
                ApplicationArea = All;
                Visible = true;

Subscriptions.

We only need one more object: a Codeunit to Subscribe to Page Item tracking lines and Codeunit "Item Jnl.-Post Line":
    [EventSubscriber(ObjectType::PagePage::"Item Tracking Lines", 'OnRegisterChangeOnAfterCreateReservEntry''', false, false)]
    local procedure ItemTrackingLinesOnRegisterChangeOnAfterCreateReservEntry(var ReservEntry: Record "Reservation Entry"; OldTrackingSpecification: Record "Tracking Specification")
    begin
        ReservEntry."AFT Grade" := OldTrackingSpecification."AFT Grade";
        ReservEntry.Modify();
    end;
 
    [EventSubscriber(ObjectType::PagePage::"Item Tracking Lines", 'OnAfterCopyTrackingSpec''', false, false)]
    local procedure ItemTrackingLinesOnAfterCopyTrackingSpec(var DestTrkgSpec: Record "Tracking Specification"; var SourceTrackingSpec: Record "Tracking Specification")
    begin
        DestTrkgSpec."AFT Grade" := SourceTrackingSpec."AFT Grade";
    end;
 
    [EventSubscriber(ObjectType::PagePage::"Item Tracking Lines", 'OnAfterEntriesAreIdentical''', false, false)]
    local procedure ItemTrackingLinesOnAfterEntriesAreIdentical(ReservEntry1: Record "Reservation Entry"; ReservEntry2: Record "Reservation Entry"; var IdenticalArray: array[2of Boolean)
    begin
 
        IdenticalArray[2] := IdenticalArray[2and (ReservEntry1."AFT Grade" = ReservEntry2."AFT Grade");
    end;
 
    [EventSubscriber(ObjectType::PagePage::"Item Tracking Lines", 'OnAfterMoveFields''', false, false)]
    local procedure ItemTrackingLinesOnAfterMoveFields(var ReservEntry: Record "Reservation Entry"; var TrkgSpec: Record "Tracking Specification")
    begin
        ReservEntry."AFT Grade" := TrkgSpec."AFT Grade";
    end;
 
    [EventSubscriber(ObjectType::CodeunitCodeunit::"Item Jnl.-Post Line", 'OnBeforeInsertSetupTempSplitItemJnlLine''', false, false)]
    local procedure ItemJnlPostLineOnBeforeInsertSetupTempSplitItemJnlLine(var TempTrackingSpecification: Record "Tracking Specification"; var TempItemJournalLine: Record "Item Journal Line")
    begin
        TempItemJournalLine."AFT Grade" := TempTrackingSpecification."AFT Grade"
    end;
 
    [EventSubscriber(ObjectType::CodeunitCodeunit::"Item Jnl.-Post Line", 'OnAfterInitItemLedgEntry''', false, false)]
    local procedure ItemJnlPostLineOnAfterInitItemLedgEntry(var NewItemLedgEntry: Record "Item Ledger Entry"; ItemJournalLine: Record "Item Journal Line")
    begin
        NewItemLedgEntry."AFT Grade" := ItemJournalLine."AFT Grade";
    end;
 

The final Cut

I want that the tracking line page when I am selling a lot number, brings the Grade value of this lot. For this purpose we add two new functions in the Codeunit:
local procedure TrackingSpecificationLotNoOnAfterValidateEvent(var Rec: Record "Tracking Specification")
    begin
        rec."AFT Grade" := GetGradeFirst(Rec);
    end;
 
    local procedure GetGradeFirst(TrackingSpecication: Record "Tracking Specification")Decimal
    var
        ItemLedgerEntryRecord "Item Ledger Entry";
    begin
        with ItemLedgerEntry do begin
            SetCurrentKey("Item No.", "Lot No.", "Posting Date", "Entry No.");
            SetRange("Item No.", TrackingSpecication."Item No.");
            SetRange("Lot No.", TrackingSpecication."Lot No.");
            if FindFirst() then
                exit("AFT Grade");
        end;
 
    end;
If we do a new sales order when I pick the lot the Grade field is filled with the Grade value of the first entry:
All the Code is available in this repo:
 

 Extra tip November 2021: correction

If you see comments bellow, there are one or two reamaining questions about new Item tracking fields posting. First one is correction. How correction should work with a new Item tracking field? We must recover fields from previous corrected entry and put them in the correcting Item Journal Line. There are many ways to do this but I sugest this suscription (feel free to sugest any alternative solution):

    [EventSubscriber(ObjectType::Codeunit, Codeunit::"Item Jnl.-Post Line", 'OnBeforePostItemJnlLine', '', false, false)]
    local procedure GetCorrectionfields(var ItemJournalLine: Record "Item Journal Line")
    var
        OldItemLedgerEntry: Record "Item Ledger Entry";
    begin
        if not ItemJournalLine.Correction then
            exit;
        if not OldItemLedgerEntry.get(ItemJournalLine."Applies-from Entry") then
            if not OldItemLedgerEntry.get(ItemJournalLine."Applies-to Entry") then
                exit;
        ItemJournalLine."AFT Grade" := OldItemLedgerEntry."AFT Grade";
    end;
 

Comments

*This post is locked for comments

  • Jalmaraz Profile Picture Jalmaraz 667
    Posted at
    And here you are: that is the solution to correction post. Only a warning: my SAAS does not work properly today, so I can not test it. I am sure 99% the code works, but I do not feel confortable hiding information. Thanks for the comments.
  • Jalmaraz Profile Picture Jalmaraz 667
    Posted at
    Yes this week I will fix correction issue.
  • burn19 Profile Picture burn19 10
    Posted at
    and can you show me how to fix the Undo item ledger entries.
  • burn19 Profile Picture burn19 10
    Posted at
    Never mind , i messed up the field ids....It works just fine. Thank you
  • Jalmaraz Profile Picture Jalmaraz 667
    Posted at
    burn19: May be something is missing. I know two issues for these code: - Transfer receipts. This will be hard to fix. - Undo item ledger entries. I know how to fix this and I am going to do it. When I finish some new features in my vscode extensions I will update the post. Is your problem related with one of these situations?
  • burn19 Profile Picture burn19 10
    Posted at
    I tried your code with two fields added and it didn't work for me. Is there something missing?
  • burn19 Profile Picture burn19 10
    Posted at
    I tried your code with two fields added and it didn't work for me. Is there something missing?