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::Page, Page::"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::Page, Page::"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::Page, Page::"Item Tracking Lines", 'OnAfterEntriesAreIdentical', '', false, false)]
local procedure ItemTrackingLinesOnAfterEntriesAreIdentical(ReservEntry1: Record "Reservation Entry"; ReservEntry2: Record "Reservation Entry"; var IdenticalArray: array[2] of Boolean)
begin
IdenticalArray[2] := IdenticalArray[2] and (ReservEntry1."AFT Grade" = ReservEntry2."AFT Grade");
end;
[EventSubscriber(ObjectType::Page, Page::"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::Codeunit, Codeunit::"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::Codeunit, Codeunit::"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
ItemLedgerEntry: Record "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;
*This post is locked for comments