5035

Gapless by Law

rollback
gapless-numbering
number-sequence
error-recovery

Gapless by Law

In most countries the tax auditor gets to run one very cheap query: sort the year's invoice numbers and look for holes. Every hole is a conversation — was an invoice destroyed, or did your software just burn the number when a posting crashed? The naive poster grabs the next number first and does the risky work afterwards, and every failed posting costs a number. The lawful poster assigns the final number at the last possible moment, inside the same indivisible write as the invoice itself, so a failed posting never consumes one. You build the lawful one.

The starter ships three finished data objects. Do not rename them or their fields — the grading tests bind to every name below character for character.

The objects you get

  • Table "Draft Invoice" — primary key "Draft No." (Code[20]); fields "Customer No." (Code[20]), Amount (Decimal), "Final Invoice No." (Integer, 0 until the draft is posted).
  • Table "Final Invoice" — primary key "Invoice No." (Integer); fields "Draft No." (Code[20]), "Customer No." (Code[20]), Amount (Decimal).
  • Table "Invoice Sequence" — primary key "Sequence Code" (Code[20]); field "Last Used No." (Integer).

Requirements

Implement both procedures in the codeunit "Gapless Poster":

procedure Post(DraftNo: Code[20]): Integer
procedure PostAll(): Integer

Rules:

  1. Numbering — final invoice numbers come from the "Invoice Sequence" row whose "Sequence Code" is INVOICE. Every posting attempt draws the row's current "Last Used No." plus one, and a successful posting saves the drawn number back to the row. If the row does not exist yet, the first successful posting uses 1 and creates the row with "Last Used No." 1.
  2. Posting a draft — a successful Post creates exactly one "Final Invoice" whose "Invoice No." is the drawn number, copies "Draft No.", "Customer No." and Amount from the draft, stamps the draft's "Final Invoice No." with the same number, and returns that number. The draft record stays — posting never deletes drafts.
The rest of the statement and 3 hints Unlock with Pro
ALBusiness Central 28.5
Pro
Pro problem

Solve it with Pro

You’re reading the opening of the statement. Pro unlocks the whole problem:

  • The full statement
  • Starter code in the browser editor
  • 16 grading tests, run on Business Central 28.5
  • 3 hints
  • The reference solution

Pro also opens all 92 Pro problems and lifts the daily limits on compile checks and submissions.

$10 / month
or $100 / year
Upgrade
This problem is part of Pro
Pro unlocks the full statement, tests, hints and solution. The code editor works on a computer.
Upgrade