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 information.
Number range objects and number ranges have long been a standard feature across various SAP specialist modules. But what’s the current approach in ABAP Cloud? Do we still need these objects? In this article, we’ll explore the new concepts introduced in ABAP Cloud and how you can effectively work with them.
Introduction
In traditional on-premise systems, the SNRO transaction was your go-to tool for creating and managing number ranges. However, with ABAP Cloud, the landscape has changed transactions like SNRO are no longer available, and maintaining intervals directly is no longer an option. Instead, SAP now offers a set of ABAP APIs along with a dedicated Fiori app to handle number range management. In this post, we’ll take a closer look at these new tools and how you can use them effectively in the cloud environment.
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.
Created the ABAP class in RAP application using ADT to check SNRO number range object.
CLASS zcl_rap_snro_number_range DEFINITION
PUBLIC
FINAL
CREATE PUBLIC .
PUBLIC SECTION.
INTERFACES if_oo_adt_classrun.
PROTECTED SECTION.
PRIVATE SECTION.
ENDCLASS.
CLASS zcl_rap_snro_number_range IMPLEMENTATION.
METHOD if_oo_adt_classrun~main.
TRY.
zcl_sm_helper_class=>get_snro_id( EXPORTING iv_range_nr = ’01’
iv_object = ‘ZSM_USERID’
IMPORTING ev_num = DATA(lv_num) ).
CATCH cx_number_ranges INTO DATA(lx_obj).
out->write( lx_obj->get_text( ) ).
ENDTRY.
out->write( |EI{ lv_num+12(8) }| ).
ENDMETHOD.
ENDCLASS.
Next we have an helper class –>> ZCL_SM_HELPER_CLASS.
CLASS zcl_sm_helper_class DEFINITION
PUBLIC
FINAL
CREATE PUBLIC.
PUBLIC SECTION.
CLASS-METHODS get_snro_id IMPORTING iv_range_nr TYPE zsm_NRNR
iv_object TYPE zsm_NROBJ
EXPORTING ev_num TYPE zsm_num
RAISING cx_number_ranges.
PROTECTED SECTION.
PRIVATE SECTION.
ENDCLASS.
CLASS zcl_sm_helper_class IMPLEMENTATION.
METHOD get_snro_id.
TRY.
cl_numberrange_intervals=>read( EXPORTING nr_range_nr1 = iv_range_nr
* nr_range_nr2 =
object = iv_object
* subobject =
IMPORTING interval = DATA(ls_interval) ).
CATCH cx_nr_object_not_found INTO DATA(lx_obj1). ” TODO: variable is assigned but never used (ABAP cleaner)
CATCH cx_nr_subobject INTO DATA(lx_obj2). ” TODO: variable is assigned but never used (ABAP cleaner)
CATCH cx_number_ranges INTO DATA(lx_obj3). ” TODO: variable is assigned but never used (ABAP cleaner)
ENDTRY.
IF ls_interval IS INITIAL.
TRY.
cl_numberrange_intervals=>create(
interval = VALUE #( ( nrrangenr = ’01’ fromnumber = ‘00000001’ tonumber = ‘99999999’ ) )
object = iv_object ).
* subobject =
* option =
* IMPORTING
* error =
* error_inf =
* error_iv =
* warning =
CATCH cx_nr_object_not_found INTO DATA(lx_obj4). ” TODO: variable is assigned but never used (ABAP cleaner)
CATCH cx_number_ranges INTO DATA(lx_obj5). ” TODO: variable is assigned but never used (ABAP cleaner)
ENDTRY.
ENDIF.
TRY.
cl_numberrange_runtime=>number_get( EXPORTING
* ignore_buffer =
nr_range_nr = iv_range_nr
object = iv_object
* quantity =
* subobject =
* toyear =
IMPORTING number = ev_num
* returncode =
* returned_quantity =
).
CATCH cx_number_ranges INTO DATA(lx_obj).
RAISE EXCEPTION lx_obj.
ENDTRY.
ENDMETHOD.
ENDCLASS.
OUTPUT: We are getting serial numbers from SNRO in the expected format.
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 information. Number range objects and number ranges have long been a standard feature across various SAP specialist modules. But what’s the current approach in ABAP Cloud? Do we still need these objects? In this article, we’ll explore the new concepts introduced in ABAP Cloud and how you can effectively work with them. IntroductionIn traditional on-premise systems, the SNRO transaction was your go-to tool for creating and managing number ranges. However, with ABAP Cloud, the landscape has changed transactions like SNRO are no longer available, and maintaining intervals directly is no longer an option. Instead, SAP now offers a set of ABAP APIs along with a dedicated Fiori app to handle number range management. In this post, we’ll take a closer look at these new tools and how you can use them effectively in the cloud environment.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 CloudWhen 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.DomainIn 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. Created the ABAP class in RAP application using ADT to check SNRO number range object.CLASS zcl_rap_snro_number_range DEFINITION
PUBLIC
FINAL
CREATE PUBLIC .
PUBLIC SECTION.
INTERFACES if_oo_adt_classrun.
PROTECTED SECTION.
PRIVATE SECTION.
ENDCLASS.
CLASS zcl_rap_snro_number_range IMPLEMENTATION.
METHOD if_oo_adt_classrun~main.
TRY.
zcl_sm_helper_class=>get_snro_id( EXPORTING iv_range_nr = ’01’
iv_object = ‘ZSM_USERID’
IMPORTING ev_num = DATA(lv_num) ).
CATCH cx_number_ranges INTO DATA(lx_obj).
out->write( lx_obj->get_text( ) ).
ENDTRY.
out->write( |EI{ lv_num+12(8) }| ).
ENDMETHOD.
ENDCLASS. Next we have an helper class –>> ZCL_SM_HELPER_CLASS.CLASS zcl_sm_helper_class DEFINITION
PUBLIC
FINAL
CREATE PUBLIC.
PUBLIC SECTION.
CLASS-METHODS get_snro_id IMPORTING iv_range_nr TYPE zsm_NRNR
iv_object TYPE zsm_NROBJ
EXPORTING ev_num TYPE zsm_num
RAISING cx_number_ranges.
PROTECTED SECTION.
PRIVATE SECTION.
ENDCLASS.
CLASS zcl_sm_helper_class IMPLEMENTATION.
METHOD get_snro_id.
TRY.
cl_numberrange_intervals=>read( EXPORTING nr_range_nr1 = iv_range_nr
* nr_range_nr2 =
object = iv_object
* subobject =
IMPORTING interval = DATA(ls_interval) ).
CATCH cx_nr_object_not_found INTO DATA(lx_obj1). ” TODO: variable is assigned but never used (ABAP cleaner)
CATCH cx_nr_subobject INTO DATA(lx_obj2). ” TODO: variable is assigned but never used (ABAP cleaner)
CATCH cx_number_ranges INTO DATA(lx_obj3). ” TODO: variable is assigned but never used (ABAP cleaner)
ENDTRY.
IF ls_interval IS INITIAL.
TRY.
cl_numberrange_intervals=>create(
interval = VALUE #( ( nrrangenr = ’01’ fromnumber = ‘00000001’ tonumber = ‘99999999’ ) )
object = iv_object ).
* subobject =
* option =
* IMPORTING
* error =
* error_inf =
* error_iv =
* warning =
CATCH cx_nr_object_not_found INTO DATA(lx_obj4). ” TODO: variable is assigned but never used (ABAP cleaner)
CATCH cx_number_ranges INTO DATA(lx_obj5). ” TODO: variable is assigned but never used (ABAP cleaner)
ENDTRY.
ENDIF.
TRY.
cl_numberrange_runtime=>number_get( EXPORTING
* ignore_buffer =
nr_range_nr = iv_range_nr
object = iv_object
* quantity =
* subobject =
* toyear =
IMPORTING number = ev_num
* returncode =
* returned_quantity =
).
CATCH cx_number_ranges INTO DATA(lx_obj).
RAISE EXCEPTION lx_obj.
ENDTRY.
ENDMETHOD.
ENDCLASS. OUTPUT: We are getting serial numbers from SNRO in the expected format. Read More Technology Blog Posts by Members articles
#SAP
#SAPTechnologyblog