0074

Top 5 Entries by Amount

filtering
Easy
sorting
setcurrentkey
list

Top 5 Entries by Amount

The sales team runs a quarterly contest: every closed deal is logged in a small custom table, and a Role Center tile is supposed to show the five biggest deals of the quarter. Right now the tile shows the five oldest deals instead — the records simply come back in entry-number order, and nothing ever lines them up by amount.

Requirements

The starter contains two files — submit both:

  • the table "Sales Contest Entry" with the fields "Entry No." (the primary key), Description and Amount — leave its name, fields and keys unchanged; the grading tests seed it and read it by name;
  • the codeunit "Sales Contest Leaderboard", whose single procedure you implement:
procedure GetTopFiveEntryNos(): List of [Integer]

Rules:

  1. Return the "Entry No." values of the five entries with the largest Amount, largest amount first.
  2. When two entries have the same Amount, the newer entry — the one with the higher "Entry No." — ranks first.
  3. The tie rule also decides who makes the list at all: when the fifth-largest amount is shared by two entries, the newer one takes the last spot and the older one is off the board.
  4. When the table holds fewer than five entries, return all of them, under the same ordering rules.
  5. When the table is empty, return an empty list — the procedure never raises an error.
  6. Amount can be zero or negative; such entries rank like any other value.

What the tests check

Each grading test empties the table, seeds its own entries with known entry numbers (the amounts are generated at run time in some tests, so the answers can't be hardcoded), calls GetTopFiveEntryNos and compares your list against the expected one as a whole — the values and their order. One test seeds eight entries and expects exactly five back; one seeds three; one seeds none; one puts three equal amounts inside the top five; one makes fifth place itself a tie. A failing test prints both lists in full, so you can see exactly what your code returned.

Learn More

Hint 1
Records come back in the order of the current key — the primary key unless you change it. That is why the starter's loop sees the oldest entries first: nothing about it ever looks at Amount.
Hint 2
SetCurrentKey chooses the fields that drive the sort, and Ascending(false) flips the direction. Sort by Amount, walk the records, and stop once you have collected five entry numbers.
Hint 3
The tie rule costs you nothing: to keep sorting deterministic, BC appends the primary key to whatever sort you pick, and Ascending(false) reverses that part too — so equal amounts arrive newest entry first, exactly the order the tests expect. A descending walk that stops at five is the whole solution.
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.