How to post sales order on custom table in AL language

When we post the sales order in business central it creates the sales invoice. we want when we post-sales order this sales order post on the custom table same as like sales invoice

To create a custom table and populate it with data when a sales order is posted in Business Central, you can use an event subscriber. Here’s an example of how you can achieve this:

  1. Create a new table in Business Central with the necessary fields to store the sales order data.
  2. Create a new codeunit in Business Central and add a new function to it. This function will be used to insert data into the custom table.
  3. In the codeunit, create an event subscriber for the OnAfterPostSalesInvoice event. This event is triggered when a sales invoice is posted.
  4. In the event subscriber, check if the posted document is a sales order or a sales return order.
  5. If the posted document is a sales order, retrieve the necessary data from the sales order and insert it into the custom table using the function created in step 2.

Here’s some sample code to get you started:

codeunit 50100 SalesOrderEventSubscriber
{
trigger OnAfterPostSalesInvoice(var Rec: Record “Sales Invoice Header”)
var
SalesOrderHeader: Record “Sales Header”;
begin
if Rec.“Document Type” = Rec.“Document Type”::Order then
begin
SalesOrderHeader.Get(Rec.“Order No.”);
InsertDataIntoCustomTable(SalesOrderHeader);
end;
end;

procedure InsertDataIntoCustomTable(var SalesOrderHeader: Record "Sales Header")
var
    CustomTable: Record "Custom Sales Order Table";
begin
    CustomTable.INIT;
    CustomTable.Field1 := SalesOrderHeader.Field1;
    CustomTable.Field2 := SalesOrderHeader.Field2;
    CustomTable.Field3 := SalesOrderHeader.Field3;
    CustomTable.INSERT;
end;

}

In this example, “Sales Header” is the table that stores sales order data, and “Custom Sales Order Table” is the custom table you created to store the sales order data. “Field1”, “Field2”, and “Field3” are placeholders for the fields you want to populate in the custom table.

Note that this is just a basic example, and you may need to modify it to fit your specific requirements.