Code examples · Modern ABAP · Intermediate
Document numbers without gaps in logic: NUMBER_GET_NEXT and a number range object
The problem
A custom application creates tickets that need a unique, readable number such as 0000001001.
How we solve it
- 1DefineSNRO object
- 2TakeNUMBER_GET_NEXT
- 3Handle errorsexceptions
The result
Two calls give two consecutive unique numbers, and a wrong object name gives a clear message.
SAP objects in play
1. The requirement
A custom application creates tickets that need a unique, readable number such as 0000001001. Two users creating tickets at the same moment must never get the same number, and the numbering rules must be configurable without changing code.
2. How we design it
SAP does not count with a variable in the program, because two programs would count at the same time. A number range object (transaction SNRO) stores the interval, the length and the current number in the database. The function module NUMBER_GET_NEXT hands out the next number safely, with its own lock. The program only names the object and the range number. Because the number is taken before the ticket is saved, a failed save leaves a gap, and that is accepted: uniqueness matters, not continuity.
Why we decided it this way
- 1A number range object instead of MAX( ) + 1Reading the highest number and adding one gives two users the same number if they run at the same moment.
- 2Handle every exceptionThe function module can fail because the interval is full, the object does not exist or the range is wrong. Each case needs a different message.
- 3Accept gapsA number taken before a failed save is lost. Legal numbering that must be gap-free needs a different design and a documented process.
Before you start
- A number range object ZTICKETNR with one interval 01 from 0000001001 to 0000009999, internal numbering. Create it in transaction SNRO (object, interval, then 'Number ranges' > Intervals).
- A transport request or package for the object, or use $TMP in a sandbox.
3. The code, step by step
Step 1 of 3: Define the number range object once
The rules for the numbers are stored as data.
Object ZTICKETNR
Number length NUMC10 (10 digits)
Interval 01
From number 0000001001
To number 0000009999
Numbering internal (the system assigns the numbers)
Warning % 10
No interval rolling set
Part by part (plain text)
- What to enter in SNRO. Object name ZTICKETNR with a number length domain of ten digits. Then create an interval with number 01, from 0000001001 to 0000009999, internal numbering (no 'external' flag). The system keeps the current number for this interval in the database. Set 'No interval rolling': without it the interval starts again at the first number after the last one and hands out numbers a second time. Warning % tells the system to warn when only 10 percent of the numbers are left. No ABAP code is needed for this step.
Open in the portal
The SAP objects named in this code are linked in the code above and listed here. Each opens its page in the SAP Library.
Function modules
If it does not work
4. The final outcome
Two calls give two consecutive unique numbers, and a wrong object name gives a clear message.
What you should see
Ticket numbers: 0000001001, 0000001002 Wrong object name: number range object ZNOPE does not exist
Try it and check the result
- Create the object in SNRO with the interval 01, then activate the class and run it with F9. The first run prints 0000001001 and 0000001002, the next run prints the following numbers.
- In SNRO choose your object and 'Number ranges' > Display: the current number moved up with each call. These numbers are not given back.
- With No interval rolling set, set the interval end close to the current number and run until it is full: the interval_overflow case occurs.
Common mistakes
- Numbers taken by a program that later fails are lost. Do not promise customers 'no gaps' with this method.
- Buffering is a trade-off: a buffered number range is fast but can lose a block of numbers after a restart. Use it for tickets, not for legally gap-free documents.
- Do not create numbers with SELECT MAX( ) + 1. Two users at the same moment get the same number.
- Number range objects are configuration and have to be transported. Forgetting the transport breaks the program in the next system.