Post sales order on custom table in AL language

340 Views Asked by At

When we post sales order it create an sales invoice but we want when we post sales order it should post the sales order on custom table same as sales invoice in Business central.

I am trying the event subscriber in codeunit the event is onafterpostsalesdoc function but it cannot work. Any other method to post the sales order on custom table please describe this method.

[EventSubscriber(ObjectType::Codeunit, codeunit::"Sales-Post", 'OnAfterPostSalesDoc', '', false, false)] 
procedure OnAfterPostSalesDoc(var SalesHeader: Record "Sales Header"; SalesInvHdrNo: Code[20]) 
var 
    CustomsalesHeader: Record "MZNNE Sales Header"; 
    SalesLine: Record "MZNNE Sales Line"; 
    Sales_Line: Record "Sales Line"; 
    LineCount: Integer; 
    i: Integer; 
begin 
    CustomsalesHeader.Init(); 
    CustomsalesHeader.INSERT(true); 
end;
2

There are 2 best solutions below

0
kaspermoerch On

You are basically just inserting af blank record in your custom table.

You need to copy the data from the Sales Header record to the record you want to insert in your custom table.

Provided that the field ID's are the same in both tables you can do the following:

[EventSubscriber(ObjectType::Codeunit, codeunit::"Sales-Post", 'OnAfterPostSalesDoc', '', false, false)] 
procedure OnAfterPostSalesDoc(var SalesHeader: Record "Sales Header"; SalesInvHdrNo: Code[20]) 
var 
    CustomSalesHeader: Record "MZNNE Sales Header"; 
begin 
    CustomSalesHeader.TransferFields(SalesHeader);
    CustomSalesHeader.Insert(true); 
end;
0
Rob On

In addition to the above answer; Make sure you the field names and ID's are identical, as Transferfields will only copy matching fields. If you also want to copy the KEY fields as well, don't forget to use

CustomSalesHeader.TransferField(SalesHeader,TRUE).

else the Transferfields will omit the key fields.

If the field names or ID's don't match, you need to copy the fields one by one.

like so:

[EventSubscriber(ObjectType::Codeunit, codeunit::"Sales-Post", 'OnAfterPostSalesDoc', '', false, false)] 
procedure OnAfterPostSalesDoc(var SalesHeader: Record "Sales Header"; SalesInvHdrNo: Code[20]) 
var 
    CustomSalesHeader: Record "MZNNE Sales Header"; 
begin 
    CustomSalesHeader.Init();
    CustomSalesHeader."Document Type" :=  SalesHeader."Document Type";
    CustomSalesHeader."No." :=  SalesHeader."No.";
    ... Other Fields you'd like to copy ...
    CustomSalesHeader.Insert(true); 
end;