Skip to Content
Technical Articles
Author's profile photo Vinu Kurian

Number Ranges – Business Technology Platform (SAP Cloud Platform)

Definition : A Number range is a Group of numbers to which the unique-Id created for an SAP Object belongs.

Why do we need number ranges ?  Each time , when you insert a new record into the table or to identify the record uniquely or just to give an orderly number we use number ranges . Number ranges are not just “MAX + 1 ” values inserted on every new record , Number ranges are objects which are created using a name , length ,minimum value , maximum value etc.. There are many options in number ranges like , setting warning after certain limit , resetting the number range after maximum value / every year , creating sub objects for number ranges etc..

ERP VS CLOUD : In SAP ERP Number ranges could be created and maintained by going to transaction SNRO (Simple way to remember is SAP Number Range Object).

But In SAP Cloud Platform ,

First we should create our Number range object using :

cl_numberrange_objects=>create().

Then we should  create interval for Number range object using : cl_numberrange_intervals=>create().

Then we should  use the Number range object to generate new number using:

cl_numberrange_runtime=>number_get().

And assign it to a local variable for use.

Creating Number Range / Interval Objects :

Create a class implementing interface : f_oo_adt_classrun and follow the code :

CLASS z_number_ranges DEFINITION
  PUBLIC	
  FINAL
  CREATE PUBLIC .

  PUBLIC SECTION.
  interfaces if_oo_adt_classrun.
  PROTECTED SECTION.
  PRIVATE SECTION.
ENDCLASS.



CLASS z_number_ranges IMPLEMENTATION.
METHOD if_oo_adt_classrun~main.

  DATA : lv_norange  TYPE REF TO cl_numberrange_objects,
         lv_interval TYPE REF TO cl_numberrange_intervals,
         lv_runtime  TYPE REF TO cl_numberrange_runtime.

  DATA : nr_attribute  TYPE cl_numberrange_objects=>nr_attribute,
         obj_text      TYPE cl_numberrange_objects=>nr_obj_text,
         lv_returncode TYPE cl_numberrange_objects=>nr_returncode,
         lv_errors     TYPE cl_numberrange_objects=>nr_errors,
         nr_interval   TYPE cl_numberrange_intervals=>nr_interval,
         st_interval   LIKE LINE OF nr_interval,
         nr_number     TYPE cl_numberrange_runtime=>nr_number,
         nr_interval1  TYPE cl_numberrange_runtime=>nr_interval,
         error         TYPE cl_numberrange_intervals=>nr_error,
         error_inf     TYPE cl_numberrange_intervals=>nr_error_inf,
         error_iv      TYPE cl_numberrange_intervals=>nr_error_iv,
         warning       TYPE cl_numberrange_intervals=>nr_warning.

  nr_attribute-buffer = 'X'.
  nr_attribute-object = 'Z_NUM_RANGE'. // object name 
  nr_attribute-domlen = 'Z_CHAR_10'. // domain to be used eg char 10
  nr_attribute-percentage = 10.
  nr_attribute-devclass = 'ZNUMBERRANGE' //Class where number range actually gets created
  obj_text-langu = 'E'.
  obj_text-object = 'Z_NUM_RANGE'. //object name 
  obj_text-txt = 'Testing Num Range'. //long description
  obj_text-txtshort = 'Test'. //short description

  st_interval-subobject = ''.
  st_interval-nrrangenr = '01'. //number range interval 
* st_INTERVAL-toyear
  st_interval-fromnumber  = '1000000'. // starting number 
  st_interval-tonumber    = '9999999'. // ending number
  st_interval-procind     = 'I'.
  APPEND st_interval TO nr_interval.


    TRY.
      cl_numberrange_objects=>create( // method to create object  
      EXPORTING
          attributes                = nr_attribute
          obj_text                  = obj_text
      IMPORTING
          errors = lv_errors
          returncode = lv_returncode )
         .
    CATCH cx_number_ranges INTO DATA(lx_number_range).
  ENDTRY.


    TRY.

      CALL METHOD cl_numberrange_intervals=>create // method to create interval
        EXPORTING
          interval  = nr_interval
          object    = 'Z_NUM_RANGE'. //object name
          subobject = ''
        IMPORTING
          error     = error
          error_inf = error_inf
          error_iv  = error_iv.
    CATCH  cx_nr_object_not_found INTO DATA(lx_no_obj_found).
    catch CX_NUMBER_RANGES into data(cx_number_ranges).

  ENDTRY.

ENDMETHOD.
ENDCLASS.

Activate and run the class once , after the successful execution our number range object will be created in the system for use .

Using Number Range Objects  :

Call the number range object to generate new number using the interval ,each time a number being created it will be saved in the buffer and next time when we call the method , the next number will be generated using the interval.

CLASS z_num_gen DEFINITION
  PUBLIC
  FINAL
  CREATE PUBLIC .

PUBLIC SECTION.
 INTERFACES if_oo_adt_classrun.
PROTECTED SECTION.
PRIVATE SECTION.
ENDCLASS.



CLASS z_num_gen IMPLEMENTATION.


METHOD if_oo_adt_classrun~main.
     DATA: nr_number     TYPE cl_numberrange_runtime=>nr_number.
      DATA: LV_JO TYPE z_number .
      DATA: LV_JO_OPT TYPE z_number.
TRY.
      CALL METHOD cl_numberrange_runtime=>number_get // generating number 
        EXPORTING
*         ignore_buffer     =
          nr_range_nr = '01'
          object      = 'Z_NUM_RANGE'. //object name 
          quantity    = 00000000000000000001
*         subobject   =
*         toyear      =
        IMPORTING
          number      = nr_number.
*     returncode        =
*     returned_quantity =

    CATCH cx_nr_object_not_found.
    CATCH cx_number_ranges.
  ENDTRY.
    LV_JO =  |{ nr_number ALPHA = out }|.
   LV_JO_OPT = |{ LV_JO ALPHA = in }|. // assigning number to local variable number
   out->write( LV_JO_OPT ).

 ENDMETHOD.
ENDCLASS.

Use  cl_numberrange_runtime=>number_get to generate a new number whenever needed , we could use this method in any class (Including behaviour implementations and custom entities ) or functions.

NB: The contents in the blog are purely based on my experiences and solutions, please make sure to point out mistakes through comments.

Assigned tags

      5 Comments
      You must be Logged on to comment or reply to a post.
      Author's profile photo Ravi Singh
      Ravi Singh

      Great content with very simple example. This will solve a lot of problems.

      Author's profile photo mayank kumar
      mayank kumar

      Hi Vinu Kurian,

      Thank You for providing the solution for number range. I tried in same way and able to create the number range object

      Number%20Range%20Object

      Number Range Object

      But unable to fetch the number range object using

      "CALL METHOD cl_numberrange_runtime=>number_get".

      I am passing below values to get the number .

      CALL METHOD cl_numberrange_runtime=>number_get
      EXPORTING
      * ignore_buffer =
      nr_range_nr = '01'
      object = 'TEST1'
      quantity = '10'
      " subobject = ''
      * toyear =
      IMPORTING
      number = nr_number.

       

      passing below attributes to create number range.

      nr_attribute-buffer = 'X'.
      nr_attribute-object = 'TEST'.
      nr_attribute-domlen = 'CHAR10'.
      nr_attribute-percentage = 10.
      nr_attribute-devclass = 'ZSAMPLE'.

      obj_text-langu = 'E'.
      obj_text-object = 'TEST'.
      obj_text-txt = 'Testing Num Range'.
      obj_text-txtshort = 'Test'.

      st_interval-nrrangenr = '01'.
      st_interval-fromnumber = '10000'.
      st_interval-tonumber = '99999'.
      st_interval-procind = 'I'.
      st_interval-nrlevel ='00000000000000000001'.
      APPEND st_interval TO nr_interval.

      Please let me know if i am missing anything here. Thank You.

      Author's profile photo Vinu Kurian
      Vinu Kurian
      Blog Post Author

      Try Changing quantity to : 00001

      Author's profile photo Joanne Rogers
      Joanne Rogers

      Good Day Vinu,

      Is there a Best Practice recommendation on whether to reset a number range (roll) or create a new range?   As an example if the RV_BELEG range for SD billing documents is reaching the limit should the invoice range be reused or created new?

      Author's profile photo Vinu Kurian
      Vinu Kurian
      Blog Post Author

      Will definitely check and revert .