Codeunit 80 is the most-subscribed codeunit in Business Central and most developers have never read it. What happens between pressing Post and the G/L entries appearing, in order, with the events at each stage, so that your extension hooks the right one and never writes a ledger itself.
When a user posts a sales order, codeunit 80 Sales-Post runs. It is long, it has been refactored many times, and it is where most sales-side extension code wants to hook in. Knowing its shape means you subscribe to the right event, with the right data available, and you never try to reproduce what it does.
OnRun receives the Sales Header. OnBeforePostSalesDoc fires here (with IsHandled): the last point to stop the whole posting. Then CheckAndUpdate: status released, posting dates in range, customer not blocked, dimensions valid, VAT, item tracking, e-document rules. Most "cannot post because" errors come from here.OnAfterCheckSalesDoc and the line-level OnBeforePostSalesLines fire around here.OnBeforePostItemJnlLine, OnAfterPostItemJnlLine, OnPostSalesLineOnBeforeInsertShptLine and dozens of others fire per line.OnBeforeSalesInvHeaderInsert, OnAfterSalesInvLineInsert and so on: this is where your fields get copied to the posted tables (and why lengths must match).OnBeforePostCustomerEntry, OnBeforePostInvoicePostingBuffer (the invoice posting buffer is the grouped amounts by account and dimension set).OnAfterPostSalesDoc fires with the posted document numbers: the point for "send to courier", "email the invoice", "notify the web shop". By then it is committed; do not error here.OnBeforePostSalesDoc or the line-level before events, not after.OnAfterPostSalesDoc and must not throw.Insert into G/L Entry.Purchase posting (codeunit 90) is the mirror image. Read codeunit 80 once in the symbol browser with the stages above as a map; it will save you a week over the life of any sales extension. The events post covers the subscription mechanics.