ABAP Cloud - Number ranges
How can you use number ranges in ABAP Cloud and does this actually still make sense? In this article we will look at some of the background informations.
Table of contents
Number range objects and number ranges are standard in the various SAP specialist modules. What is the situation in ABAP Cloud and do we still need such objects? In this article we will look at the new objects and how you can use them.
Introduction
So far you have mainly used the SNRO transaction on-premise to create and manage number ranges. In ABAP Cloud there are no more transactions and you can therefore no longer maintain the intervals. SAP provides various ABAP APIs and a Fiori app for this, which we would like to look at in detail.
Why do we actually need number ranges and consecutive numbers in the system? Data is stored in databases and records in relational databases usually require a unique key. To assign numbers automatically, we could always look at the database and calculate the current ID plus 1 to get a new unique key. However, since we are not alone on the system and several booking processes can run at the same time, this would not guarantee that we would always receive a unique number. Number range objects therefore manage the individual number range statuses and always give us a unique number via a function and take care of the increase.
ABAP Cloud
When developing with ABAP Cloud, we rely on the ABAP RESTful Programming Model, or RAP for short, which very often uses GUIDs to manage the keys. The GUIDs are generated randomly and we don't have to worry about managing the key. Furthermore, we can change any field in the data record in the workflow if we want to. If we need a readable or unique key in our data record, we can also use a semantic key, which we can also declare as unique, but we have to carry out the check ourselves.
However, with this method we cannot prove any sequence or gaps in the data. Therefore, in certain technical scenarios it still makes sense to use number ranges to create a semantic key.
Creation
You can easily create the number range object using the ABAP Development Tools. In the following steps we create a domain and then the appropriate object.
Domain
In order to create a number range, we first need a domain that determines the data type. To do this, we create a domain using the context menu; a new domain on our system.
We then give the domain a type that determines the number and options of the generated numbers.
Number Range Object (ADT)
We can now create a number range object. Here we have to make sure that the name is not too long, as the field currently only has 10 characters.
Finally, we enter the domain in the form; we can leave the rest of the configuration initial. If you would like further information on maintenance and the various fields, you can use F1 to call up the help in the ABAP Development Tools and receive further information.
Number Range Object (API)
If your system does not yet have the required release, you can also use the ABAP class CL_NUMBERRANGE_OBJECTS to create the object in the system. The class offers the various CRUD operations for the number range objects. The following example shows this:
cl_numberrange_objects=>create(
EXPORTING attributes = VALUE #( object = c_object
domlen = 'ZBS_DEMO_NURANGE'
percentage = 10
devclass = c_package
corrnr = c_transport )
obj_text = VALUE #( langu = 'E'
object = c_object
txtshort = 'Numbers (API)'
txt = 'Number range (API)' )
IMPORTING errors = DATA(lt_errors)
returncode = DATA(ld_return) ).
We create the new number range object and assign it to a package and transport. We specify the domain and the text for the object. Percent is a mandatory field and must be specified when creating the object; no default is set.
Maintenance
In the next step, we want to maintain the intervals for the number range object. There are two methods for this.
Fiori App
In the first step, we want to manage the number ranges using the Fiori app "Manage Number Range Intervals" (F4290), currently the app only seems to exist for S/4 HANA Cloud and the ABAP Environment.
In the app we also find the number range objects that we created via the API.
On the object page of the number range object we select the "Create" in the "Number Ranges" area. Button and create a new number range.
ABAP API
It is also possible to create a number range using the ABAP class CL_NUMBERRANGE_INTERVALS if the app is not available to you. With the following call we create the same setting in our API object:
cl_numberrange_intervals=>create(
EXPORTING interval = VALUE #( ( nrrangenr = '01' fromnumber = '100000' tonumber = '199999' procind = 'I' ) )
object = c_object
IMPORTING error = DATA(ld_error)
error_inf = DATA(ls_error)
error_iv = DATA(lt_error_iv)
warning = DATA(ld_warning) ).
Since this is customizing, we do not need transport when specifying the data, but must perform the maintenance on each system.
Assignment
If we now want to take a number from the number range and assign it, the released class CL_NUMBERRANGE_RUNTIME is available to us. The class also offers other methods to check whether a number is in the interval or is correct. With the NUMBER_GET method, we can determine the next number.
cl_numberrange_runtime=>number_get(
EXPORTING nr_range_nr = '01'
object = c_object
IMPORTING number = DATA(ld_number)
returncode = DATA(ld_rcode) ).
Since we get the number back as NUMC 20, we still have to do a cast to our data element/domain. If we now check the app after a few attempts, we also get information about the use of the range and the current number status.
Complete example
Here you can find the complete ABAP resources that we used in this example.
CLASS zcl_bs_demo_number_range DEFINITION
PUBLIC FINAL
CREATE PUBLIC.
PUBLIC SECTION.
INTERFACES if_oo_adt_classrun.
PRIVATE SECTION.
CONSTANTS c_object TYPE cl_numberrange_objects=>nr_attributes-object VALUE 'ZBSDMONUMA'.
CONSTANTS c_package TYPE cl_numberrange_objects=>nr_attributes-devclass VALUE 'ZBS_DEMO_NUMBERING'.
CONSTANTS c_transport TYPE cl_numberrange_objects=>nr_attributes-corrnr VALUE ''.
METHODS create_number_range_object
IMPORTING io_out TYPE REF TO if_oo_adt_classrun_out.
METHODS create_number_range
IMPORTING io_out TYPE REF TO if_oo_adt_classrun_out.
METHODS draw_number
IMPORTING io_out TYPE REF TO if_oo_adt_classrun_out.
ENDCLASS.
CLASS zcl_bs_demo_number_range IMPLEMENTATION.
METHOD if_oo_adt_classrun~main.
* create_number_range_object( out ).
* create_number_range( out ).
draw_number( out ).
ENDMETHOD.
METHOD create_number_range_object.
TRY.
cl_numberrange_objects=>create( EXPORTING attributes = VALUE #( object = c_object
domlen = 'ZBS_DEMO_NURANGE'
percentage = 10
devclass = c_package
corrnr = c_transport )
obj_text = VALUE #( langu = 'E'
object = c_object
txtshort = 'Numbers (API)'
txt = 'Number range (API)' )
IMPORTING errors = DATA(lt_errors)
returncode = DATA(ld_return) ).
CATCH cx_number_ranges INTO DATA(lo_error).
io_out->write( lo_error->get_text( ) ).
ENDTRY.
io_out->write( lt_errors ).
io_out->write( ld_return ).
ENDMETHOD.
METHOD create_number_range.
TRY.
cl_numberrange_intervals=>create(
EXPORTING interval = VALUE #( ( nrrangenr = '01' fromnumber = '100000' tonumber = '199999' procind = 'I' ) )
object = c_object
IMPORTING error = DATA(ld_error)
error_inf = DATA(ls_error)
error_iv = DATA(lt_error_iv)
warning = DATA(ld_warning) ).
CATCH cx_root INTO DATA(lo_error).
io_out->write( lo_error->get_text( ) ).
ENDTRY.
io_out->write( ld_error ).
io_out->write( ls_error ).
io_out->write( lt_error_iv ).
io_out->write( ld_warning ).
ENDMETHOD.
METHOD draw_number.
TRY.
cl_numberrange_runtime=>number_get( EXPORTING nr_range_nr = '01'
object = c_object
IMPORTING number = DATA(ld_number)
returncode = DATA(ld_rcode) ).
CATCH cx_root INTO DATA(lo_error).
io_out->write( lo_error->get_text( ) ).
ENDTRY.
io_out->write( ld_number ).
io_out->write( ld_rcode ).
ENDMETHOD.
ENDCLASS.
On-Premise
As long as the On-Premise app is not available to you, you can also maintain the number range intervals using the SNRO transaction and do not necessarily have to use the API.
Conclusion
There is a new app and some classes for number range objects and the administration of the areas. Inside the class, however, you will come across an old acquaintance called NUMBER_GET_NEXT. This is why this is not a complete redesign of the API, as is the case with the application jobs, for example.