I have written a OnModify trigger on Table Extension Item, so whenever the Standard Cost changes then I need to copy the Old Standard cost of an item to a new table.
I have created a new table called - "Standard Cost Entry" with structure as below.
Error I am getting - "The record in table Standard Cost Entry already exist. Identification fields & values: Entry No.=0"
"
table 50016 Test_StandardCostEntry
{
DataClassification = ToBeClassified;
Caption = 'Standard Cost Entry';
LookupPageId = Test_StandardCostEntries;
DrillDownPageId = Test_StandardCostEntries;
fields
{
field(1; Test_EntryNo; Integer)
{
DataClassification = CustomerContent;
Caption = 'Entry No.';
}
field(2; Test_ItemNumber; Code[20])
{
DataClassification = CustomerContent;
Caption = 'No.';
}
field(3; Test_NewStandardCost; Decimal)
{
DataClassification = CustomerContent;
Caption = 'New Standard Cost';
}
field(4; Test_StartDate; Date)
{
DataClassification = CustomerContent;
Caption = 'Start Date';
}
field(5; Test_EndDate; Date)
{
DataClassification = CustomerContent;
Caption = 'End Date';
}
}
keys
{
key(pk; Test_EntryNo)
{
Clustered = true;
}
}
}
"
My Code -
"codeunit 50031 Test_StdCostEntryUpdate
{
trigger OnRun()
begin
UpdateStdCostEntryTable();
end;
local procedure UpdateStdCostEntryTable()
var
StdCostWksh: Record "Standard Cost Worksheet";
Item: Record Item;
StdCostEntry: Record Test_StandardCostEntry;
LineNo: Integer;
begin
StdCostWksh.SetFilter("Standard Cost Worksheet Name", 'TEST');
StdCostWksh.SetFilter("Type", 'Item');
StdCostWksh.SetFilter("No.", Item."No.");
If StdCostWksh.FindSet() then begin
repeat
StdCostEntry.Init();
StdCostEntry.Test_EntryNo := 0;
StdCostEntry.Test_ItemNumber := StdCostWksh."No.";
StdCostEntry.Test_NewStandardCost := StdCostWksh."Standard Cost";
StdCostEntry.Test_StartDate := Today();
StdCostEntry.Test_EndDate := Today();
StdCostEntry.Insert();
until StdCostWksh.Next() = 0;
end;
end;
}"
Executing from Item Table Extension
"
tableextension 50004 Test_ItemExt extends Item
{
Fields
{
}
trigger OnModify()
begin
Codeunit.Run(Codeunit::Test_StdCostEntryUpdate);
end;
}
"