Ranges, Not If-Chains
The dispatch team's courier app has two small classification rules, and both misbehave. The shipping zone is worked out from the postcode the customer typed, and since the last release every parcel is quoted as Zone 3 — city-centre deliveries included. The monthly courier grade comes from an on-time score, and a courier whose score was keyed in as 101 was awarded an A. Both rules are "more than two choices" logic, which is exactly what AL's case statement is for: a value set can be a single value, a comma-separated list (1, 2, 9) or an inclusive range (10..100), and the else arm catches whatever no other arm matched.
The starter is the current code. Fix both rules.
Requirements
Complete the codeunit "Dispatch Rules" with these two public procedures:
procedure ShippingZone(PostCode: Code[10]): Integer
procedure Grade(Score: Integer): TextRules:
ShippingZonereturns 1 for the city-centre outward codesEC1,EC2,EC3,EC4,WC1andWC2; 2 for the inner-ring codesE1,N1,NW1,SE1,SW1andW1; and 3 for every other postcode. Only these exact codes count:EC5,SW19,E10,EC1AandEC10are on neither list, so they are Zone 3 — do not match on a prefix, and do not write the zones as ranges (EC1AandEC10sort betweenEC1andEC4as text).- Postcodes are typed by hand, so
ec1,Ec1andEC1are the same postcode and must land in the same zone. The parameter is aCode[10], and aCodevalue is uppercased (and trimmed) the moment it is assigned, so the procedure never sees a lowercase letter. The starter still never returns 1 or 2, and the reason is documented behaviour, not a runtime bug: when the expression of acasestatement is aCodevariable, the value sets are not converted toCode— they are compared exactly as written, so a lowercase literal can never match an uppercased postcode. Grademaps a score to a letter: 90 to 100 isA, 75 to 89 isB, 60 to 74 isC, 40 to 59 isDand 0 to 39 isF. Every band includes both of its ends: 89 is aB, 90 is anA.- A score outside every band — negative, or above 100 — returns the text
Invalid. The starter's if-chain has no upper bound and no branch for such a score, which is why 101 grades asAand -5 asF. - Comparisons are exact: return exactly the single uppercase letter, or exactly
Invalid.
Write each rule as one case statement: value lists for the zones, ranges for the grade bands, and an else arm in both. Pick your object ID in the 50100–50199 range and reference other objects by name, never by ID.
What the tests check
The grading tests call ShippingZone with each of the six Zone 1 codes and each of the six Zone 2 codes in uppercase, then with lowercase and mixed-case spellings (ec1, sw1, Wc2, nW1), and expect the exact zone number; the near misses EC5, SW19, E10, EC1A and EC10, an unlisted BR1, and a generated six-letter code must all return 3. Grade is checked at both ends of every band (0, 39, 40, 59, 60, 74, 75, 89, 90 and 100) plus a generated score inside each band, and with 101, -1 and generated scores above 100 and below 0, which must all return Invalid. Every comparison is exact.
Learn More
- AL control statements — case statements — the syntax of value lists, ranges and
else, and Example 10 showing why aCodeexpression never matches a lowercase value set. - Code data type — what happens to a value the moment it is assigned to a
Codevariable or parameter. - Work with conditional statements — the training module's walkthrough of
if-then-elseversuscase, with the layout conventions for acasestatement.
case arm can hold a single value, a comma-separated list of values, or an inclusive range written low..high. The else arm runs when no other arm matched — that is where a score outside every band and a postcode on neither list belong.ShippingZone never returns 1 or 2 because its value sets are lowercase. A Code parameter arrives uppercased (ec1 is already EC1 inside the procedure), but the literals in a case whose expression is a Code variable are compared exactly as written — they are not converted. Write every value set in uppercase.Grade is one case Score of with five range arms — 90 to 100, 75 to 89, 60 to 74, 40 to 59, 0 to 39 — each exiting its letter, plus an else that exits Invalid. The starter's if-chain only ever checks lower bounds, so nothing stops 101 from being an A.