0049

Wire a Document to No. Series

Basics
Medium
table
no-series
oninsert

Wire a Document to No. Series

Your workshop takes repair orders, and every order needs a unique document number — WO12-001, WO12-002, … — handed out by a number series the accountant can configure. Older code did this through the NoSeriesManagement codeunit; it is obsolete, and its replacement is the "No. Series" module from Business Foundation. You will wire a brand-new document table to it.

Requirements

  1. Create a table named "Workshop Order" with:
    • a field "No." of type Code[20] — the primary key
    • a field "No. Series" of type Code[20]
  2. In the table's OnInsert trigger, when "No." is blank (''):
    • assign the next number from the number series with code WORKSHOP, and
    • stamp "No. Series" with WORKSHOP, so every order records where its number came from.
  3. A caller that has already set "No." before insert keeps that number — and no number is drawn from the series, so the sequence stays gapless.
  4. Add a public procedure on the table:
procedure PeekNextOrderNo(): Code[20]

It returns the number the next automatically numbered order will get, without consuming it — peeking any number of times must not advance the series.

Use the "No. Series" codeunit from the Business Foundation module: GetNextNo draws a number, PeekNextNo only looks. Do not roll your own counter on top of the series tables — the module already handles incrementing and updating the series line for you.

What the tests check

The grading tests create the WORKSHOP series themselves with a randomly generated starting number, so hardcoded numbers cannot pass. They always insert with Insert(true) — table triggers do not run otherwise. They verify: the first order gets exactly the series' starting number (persisted under that key, not just in the variable); consecutive inserts number sequentially; "No. Series" is stamped on automatically numbered orders; PeekNextOrderNo returns the upcoming number; an insert right after peeking still gets that same number; a manually numbered order keeps its number; and a manual order does not consume anything from the series. They also check the declarations: both fields are Code (values stored uppercase) with a maximum length of exactly 20.

Learn More

Hint 1
The Business Foundation module ships Codeunit "No. Series" — GetNextNo draws the next number and advances the series, PeekNextNo only looks. The old NoSeriesManagement codeunit is obsolete.
Hint 2
In OnInsert, act only when "No." is blank: draw the number for series code WORKSHOP and stamp "No. Series" with the same code. A pre-set "No." must pass through with no series call at all.
Hint 3
PeekNextOrderNo is a one-liner around PeekNextNo. If you reach for GetNextNo there instead, every peek consumes a number and the order inserted right after gets a later one — the tests catch exactly that.
ALBusiness Central 28.4
Press Compile to check your code compiles — Submit runs the tests.
The code editor is desktop-only
Open this problem on a computer to write and run code. Reading the description, tests and discussion works fine here.