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

Announcements

No record found.

News and Announcements icon
Community site session details

Community site session details

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

Data isn't populating into another table using insert() function

(0) ShareShare
ReportReport
Posted on by

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;
}
"

I have the same question (0)
  • Suggested answer
    Teddy Herryanto (That NAV Guy) Profile Picture
    14,300 Super User 2026 Season 1 on at

    The issue is on StdCostEntry.Test_EntryNo := 0;

    You need to increase your entry no. for every repetition.

    You can't insert record with same entry no because entry no is your primary key.

  • Community Member Profile Picture
    on at

    Hi Teddy,

    I have also tried the below code but still the same error.

    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();

    Thanks

  • Suggested answer
    Avinash B Profile Picture
    1,455 on at

    Hi

    You must increase the Entry No. for each record.

    Try this.

    If StdCostEntry.FindLast() then;

    StdCostEntry.Init();

    StdCostEntry.Test_EntryNo += 1;

    This code try to find the last entry number used and increament by 1.

    Regards

    Avinash B

  • Suggested answer
    Teddy Herryanto (That NAV Guy) Profile Picture
    14,300 Super User 2026 Season 1 on at

    You need to increase your entry no. Adding zero to it will always result on the same number.

    StdCostEntry.Test_EntryNo += 1;

    You also need to make sure that you get the last Entry No.

    The system will not do it for you.

  • Suggested answer
    keoma Profile Picture
    32,729 on at

    StdCostEntry.Test_EntryNo := 0;

    makes always a line with entry no. = 0.

    either you make entry no an autoincrement field or you increase the value with every run.

    e.g. if findlast then

     entryno := entryno + 1

    else

     entry := 1

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

Introducing the 2026 Season 1 community Super Users

Congratulations to our 2026 Super Stars!

Congratulations to our 2025 Community Spotlights

Thanks to all of our 2025 Community Spotlight stars!

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

#1
OussamaSabbouh Profile Picture

OussamaSabbouh 2,033 Super User 2026 Season 1

#2
Dhiren Nagar Profile Picture

Dhiren Nagar 1,105 Super User 2026 Season 1

#3
YUN ZHU Profile Picture

YUN ZHU 1,035 Super User 2026 Season 1

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans