Skip to main content

Notifications

Announcements

No record found.

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

  • DA-12060614-0 Profile Picture
    4 on at
    Inserting data into a custom table
     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
  • isabtogumon Profile Picture
    99 on at
    RE: Inserting data into a custom table

    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;

  • Suggested answer
    Rehan Satti Profile Picture
    634 on at
    RE: Inserting data into a custom table

    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
    RE: Inserting data into a custom table

    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
    634 on at
    RE: Inserting data into a custom table

    Hi

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

  • isabtogumon Profile Picture
    99 on at
    RE: Inserting data into a custom table

    Thanks Preet

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

  • Verified answer
    PreetGor Profile Picture
    47 on at
    RE: Inserting data into a custom table

    Hello,

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

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

🌸 Community Spring Festival 2025 Challenge Winners! 🌸

Congratulations to all our community participants!

Adis Hodzic – Community Spotlight

We are honored to recognize Adis Hodzic as our May 2025 Community…

Kudos to the April Top 10 Community Stars!

Thanks for all your good work in the Community!

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

#1
YUN ZHU Profile Picture

YUN ZHU 313 Super User 2025 Season 1

#2
Mansi Soni Profile Picture

Mansi Soni 242

#3
Gerardo Rentería García Profile Picture

Gerardo Rentería Ga... 226 Most Valuable Professional

Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans