write a function in a codeunit or write a new one:
Use Job queue to call this function
First we need to define the variables.
SalesHeader: Record "Sales Header";
SalesPost: Codeunit "Sales-Post";
Once you declare the variable, you can start writing the logic. The following code will post both the shipment and invoice.
case SalesHeader."Document Type" of
SalesHeader."Document Type"::Order:
SalesHeader.Ship := true; //false if you do not want to post shipment
SalesHeader."Document Type"::"Return Order"
SalesHeader.Receive := true; //false if you do not want to post return receipt
end;
SalesHeader.Invoice := true; //false if you do not want to post the invoice
Clear(SalesPost);
SalesPost.Run(SalesHeader);