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...
Answered

Inserting data into a custom table

(0) ShareShare
ReportReport
Posted on by 99

Hi all, 

I have a custom table, this table is made up of some fields from other tables like 112, 113, 27 and 480. The intention is that after capturing lines on the Sales Order page, the same lines are inserted into the custom table.

Any suggestion on how to insert data into that table?

Thanks

I have the same question (0)
  • Verified answer
    PreetGor Profile Picture
    47 on at

    Hello,

    You can add code on sales line, that when sales line got inserted the same values get inserted in your custom table

  • isabtogumon Profile Picture
    99 on at

    Thanks Preet

    Something like that I thought, what code should I use to make it work?

  • Suggested answer
    Rehan Satti Profile Picture
    636 on at

    Hi

    You can subscribe the Oninsert event trigger, write code OnInsert Trigger of Sales line Table.

  • isabtogumon Profile Picture
    99 on at

    Thanks Rehan,

    I subscribed the Oninsert event. I validated with a message and it is displayed on the screen.

    Could you tell me what code should I use to fill the custom table? I share some of the code I have written

    codeunit 50109 Codeunit_50109_PKT

    {

       [EventSubscriber(ObjectType::Table, Database::"Sales Line", 'OnAfterInsertEvent', '', true, true)]

       local procedure setJobSalesLine()

       var

           ReserveSalesLine: Codeunit "Sales Line-Reserve";

           Rec: Record "Sales Line";

           xRec: Record "Sales Line";

           SalesHeader: Record "Sales Header";

           Text056: Label 'You cannot add an item line because an open inventory pick exists for the Sales Header and because Shipping Advice is %1.\\You must first post or delete the inventory pick or change Shipping Advice to Partial.';

       begin

           //xSalesLine.TestStatusOpen();

           if Rec.Quantity <> 0 then begin

               OnBeforeVerifyReservedQty(Rec, xRec, 0);

               ReserveSalesLine.VerifyQuantity(Rec, xRec);

           end;

           Rec.LockTable();

           SalesHeader."No." := '';

           if Rec.Type = Rec.Type::Item then

               if SalesHeader.InventoryPickConflict(Rec."Document Type", Rec."Document No.", SalesHeader."Shipping Advice") then

                   Error(Text056, SalesHeader."Shipping Advice");

           if (Rec."Deferral Code" <> '') and (Rec.GetDeferralAmount <> 0) then

               Rec.UpdateDeferralAmounts();

       end;

       [IntegrationEvent(false, false)]

       local procedure OnBeforeVerifyReservedQty(var SalesLine: Record "Sales Line"; xSalesLine: Record "Sales Line"; CalledByFieldNo: Integer)

       begin

       end;

       var

           xSalesLine: Record "Sales Line";

    }

  • Suggested answer
    Rehan Satti Profile Picture
    636 on at

    Hi, 

    You can use some think like the following code. 

    [EventSubscriber(ObjectType::Table, 37, 'OnAfterInsertEvent', '', False, False)]
    local procedure InsertCustomSalesLineEntry(VAR Rec: Record "Sales Line"; RunTrigger: Boolean)
    var
    changeLogSalesLine: record "Custom Sales Line table";
    begin
    IF Rec.IsTemporary then
    exit;
    changeLogSalesLine.Init();
    changeLogSalesLine.Validate("Document Type", Rec."Document Type");
    changeLogSalesLine.Validate("Document No.", Rec."No.");
    changeLogSalesLine.Validate(Quantity, Rec.Quantity);
    .
    .
    . (and other fields you are interested in)
    changeLogSalesLine.Insert();

    end;

  • isabtogumon Profile Picture
    99 on at

    Thanks Rehan,

    Does the code change if the event subscribes to an action, for example the Publish action on the Sales Order page?

    I have written this code based on your recommendation, but this process does not fill the custom table. Can you help me make it work?

    [EventSubscriber(ObjectType::Page, 42, 'OnAfterActionEvent', 'Post', false, false)]

        procedure InsertCustomSalesLineEntry()

        var

            changeLogSalesLine: Record "Sales Report";

            SalesInvoiceHeader: Record "Sales Invoice Header";

            SalesInvoiceLine: Record "Sales Invoice Line";

            Item: Record Item;

            DimensionSetEntry: Record "Dimension Set Entry";

        begin

            if SalesInvoiceHeader.IsTemporary then

                exit;

            changeLogSalesLine.Init();

            changeLogSalesLine.Validate("Salesperson Code", SalesInvoiceHeader."Salesperson Code");

            changeLogSalesLine.Validate("Sell-to Customer No.", SalesInvoiceHeader."Sell-to Customer No.");

            changeLogSalesLine.Validate("Bill-to Name", SalesInvoiceHeader."Bill-to Name");

            changeLogSalesLine.Validate("Bill-to City", SalesInvoiceHeader."Bill-to City");

            changeLogSalesLine.Validate(Description, SalesInvoiceLine.Description);

            changeLogSalesLine.Validate("Vendor No.", Item."Vendor No.");

            changeLogSalesLine.Validate("Shortcut Dimension 1 Code", SalesInvoiceLine."Shortcut Dimension 1 Code");

            changeLogSalesLine.Validate("Dimension Code", DimensionSetEntry."Dimension Code");

            changeLogSalesLine.Validate("Posting Date", SalesInvoiceHeader."Posting Date");

            changeLogSalesLine.Validate("Order No.", SalesInvoiceHeader."Order No.");

            changeLogSalesLine.Validate("No.", SalesInvoiceHeader."No.");

            changeLogSalesLine.Validate("Quantity (Base)", SalesInvoiceLine."Quantity (Base)");

            changeLogSalesLine.Validate(Amount, SalesInvoiceLine.Amount);

            changeLogSalesLine.Insert()

        end;

  • DA-12060614-0 Profile Picture
    4 on at
     procedure MyCustomProcedure()
        var
            LocalData: Record CustomTable;
            serverData: Record Customer;
            IdCheck: Integer;
        begin
            LocalData.DeleteAll();
            if LocalData.FindLast() then
                IdCheck := LocalData.CustomID
            else
                IdCheck := 0;
            if serverData.FindSet() then begin
                repeat
                    IdCheck += 1;
                    LocalData.CustomID := IdCheck;
                    LocalData.Name := serverData.Name;
                    LocalData.PhoneNumber := serverData."Phone No.";
                    LocalData.Contact := serverData.Contact;
                    serverData.CalcFields(Balance);
                    LocalData.Balance := serverData.Balance;
                    serverData.CalcFields("Balance Due (LCY)");
                    LocalData.BalanceDue := serverData."Balance Due (LCY)";
                    serverData.CalcFields("Sales (LCY)");
                    LocalData.Sales := serverData."Sales (LCY)";
                    serverData.CalcFields("Payments (LCY)");
                    LocalData.Payment := serverData."Payments (LCY)";
                    LocalData."Currency Code" := serverData."Currency Code";
                    LocalData.Insert();
                until serverData.Next() = 0;
            end;
        end;
    You can use like that in which you can give them id it will auto-increment when the new data is inserted

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!

Meet the Microsoft Dynamics 365 Contact Center Champions

We are thrilled to have these Champions in our Community!

Congratulations to the March Top 10 Community Leaders

These are the community rock stars!

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

#1
OussamaSabbouh Profile Picture

OussamaSabbouh 1,947 Super User 2026 Season 1

#2
YUN ZHU Profile Picture

YUN ZHU 1,120 Super User 2026 Season 1

#3
Khushbu Rajvi. Profile Picture

Khushbu Rajvi. 628 Super User 2026 Season 1

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans