Hi,
I have created a tableextension with a field called "Serialnumber" and added a trigger OnValidate(). That field "Serialnumber" must be unique in the table.
When an user changes that field "Serialnumber" I want to check if that new value already exists in the database.
If that is the case then the new value may not be saved!
If the new value does not exist elsewhere in the table then save it.
For that I use the procedure find() in AL but no record is found.
When I use an existing field like "No." then the record is found.
What am I missing here or what do I wrong?
Here is the code I use and I test it in sandbox ( "platform": "20.0.39668.39849")
tableextension 50110 "Item Ext" extends Item { fields { field(50111; Serialnumber; Code[30]) { DataClassification = ToBeClassified; // "OnValidate" trigger executes when data is entered in a field. trigger OnValidate(); begin if (xRec."Serialnumber" = '') then begin Message('Serialnumber is empty.') end; if (Rec.Serialnumber <> xRec.Serialnumber) then begin Message('Serialnumber is changed.'); ItemRec.Reset(); ItemRec.SetCurrentKey(Serialnumber); //ItemRec.SetCurrentKey("No."); ItemRec.Serialnumber := Rec.Serialnumber; //ItemRec."No." := '1906-S'; //Message(Text003, ItemRec."No.", ItemRec.Serialnumber); if ItemRec.Find() then begin Message(Text000, ItemRec."No.", ItemRec.Description); // keep the old value Rec.Serialnumber := xRec.Serialnumber end else begin Message(Text001, ItemRec."No."); // Save current record with new Serialnumber end; ItemRec.SetCurrentKey("No."); end; end; } } keys { key(Serialnumber; "Serialnumber") { Enabled = true; } } var currentKey: Text; ItemRec: Record Item; Text000: Label 'Found: Item No. %1.\Description: %2.'; Text001: Label 'The item was not found. %1'; Text003: Label 'No.: %1.\Serienr: %2.'; }
pageextension 50120 "Item Card Serialnumber" extends "Item Card" { layout { addlast(Item) { field("Serialnumber"; Rec."Serialnumber") { ApplicationArea = All; ToolTip = 'Serialnumber'; } } } }