How to create a number range in SAP using SNRO?

During ABAP development, sometimes we need to generate a unique ID which is needed as primary key for a custom table we created. Unlike in other databases such as mySQL that a field can be set as auto-increment once defined as primary key. In SAP environment, this feature is missing in table creation so we need to setup a number range that generates distinct number which increments and used as primary key. 

A Number Range is a sequence of numbers with a start and an end. It shows the current number that is being used in your program. Letters cannot be added as a prefix or suffix in generating number ranges like, A-001, A-002, etc.

So in this tutorial, I divided into four parts which I will discuss one by one. It includes the following topics:
  1. Setting the number range definition
  2. Setting the number range object
  3. Transporting the number range to other system
  4. Implementing the number range in a program
Steps:

Setting the Number Range Definition
First, type the tcode SNRO into the command prompt.

Then, enter a name in the input field for your number range object. Note that it must begin with Y or Z just like in other objects that indicates it is a customer development. After which, click the Create button .
















In this next screen, set the following parameters:
a. Short Text – short description for the number range (20 characters).
b. Long Text – long description for the number range (60 characters).
c. Number length domain – Name of the domain, which determines the length of the number range number
d. Warning % - Percentage of no remaining in a number range, upon reaching which in number assignment is given. Example, you have defined an interval of 1 to 1000. If you want to issue a warning at the number 900, enter 10% here.


Click on the Save button  in the toolbar menu to save the number range definition. A popup screen will be displayed which you need to add a package. But for this tutorial, just click the Local Object below.















Setting the Number Ranges Interval
After creating the number range definition, the next step is to set the intervals for this object. Go back to the main window, click the button Number Ranges button on the toolbar.


Then in this screen, click the button Change Intervals. 

Then click the Add Interval button on this screen.












Enter an integer for the column “No”, “From Number” and “To Number”. This is the number range interval set for this object. Then press enter and click the check button .
















Click on "Save" button. And the number range interval you have just declared will be listed in the Number Range object 's number intervals list. You can declare multiple intervals but be sure there will be no overlapping between intervals.



Transporting the number range to other system
The following warning screen is displayed once you save the number ranges. It states that the interval is not automatically added on the transport request no. So we need to add the intervals manually on the transport request number in order to be transported to other system.

Go back again into the main screen. On the file menu, click menu Interval->transport as seen below.


This transport message appears afterwards. Just click the Yes button below to include the intervals in a transport request no.





















After which, it will create a transport request number which adds the number range interval. Thereby, it can be transported now to other system.









Implementing the number range in a program
After defining the number range object, it’s time to discuss how to use number range object in ABAP code. The following function modules are used in implementing the number range object:
  1. NUMBER_RANGE_ENQUEUE -function sets a lock on the number range object. 
  2. NUMBER_GET_NEXT - function call the next number in the number range interval is read and passed into an ABAP variable.
  3. NUMBER_RANGE_DEQUEUE - function and removing the lock on the relate SAP number range object.
Complete Sample Code:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
DATA: gv_id TYPE i.
CONSTANTS:
   c_obj TYPE inri-object     VALUE 'ZNEWTEST',
   c_no  TYPE inri-nrrangenr  VALUE '01'.

START-OF-SELECTION.
  PERFORM f_getnextnum USING gv_id c_obj c_no.
  WRITE gv_id.

*&---------------------------------------------------------------------
*& Form f_getnextnum
*&---------------------------------------------------------------------
* text
*----------------------------------------------------------------------
FORM f_getnextnum USING
      p_nextnum TYPE i
      p_rangeobj TYPE inri-object
      p_rangeno TYPE inri-nrrangenr.

  CALL FUNCTION 'NUMBER_RANGE_ENQUEUE'
    EXPORTING
      object           = p_rangeobj
    EXCEPTIONS
      foreign_lock     = 1
      object_not_found = 2
      system_failure   = 3
      OTHERS           = 4.
  IF sy-subrc NE 0.
    MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
    WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
  ENDIF.

  CALL FUNCTION 'NUMBER_GET_NEXT'
    EXPORTING
      nr_range_nr             = p_rangeno
      object                  = p_rangeobj
    IMPORTING
      number                  = p_nextnum
    EXCEPTIONS
      interval_not_found      = 1
      number_range_not_intern = 2
      object_not_found        = 3
      quantity_is_0           = 4
      quantity_is_not_1       = 5
      interval_overflow       = 6
      buffer_overflow         = 7
      OTHERS                  = 8.
  IF sy-subrc <> 0.
    MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
    WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
  ENDIF.

  CALL FUNCTION 'NUMBER_RANGE_DEQUEUE'
    EXPORTING
      object = p_rangeobj.
  IF sy-subrc <> 0.
    MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
    WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
  ENDIF.
ENDFORM.  

How to delete Implicit Enhancement Implementation?

Yesterday, I am testing the implicit implementation that I did few weeks back. After all of a sudden, I noticed that the name of the implementation I created is not the one specified in the functional design. So if my functional counterpart will test it and find the name is not the one specified in the functional design, I might be in trouble then. 

I paused for a moment and think what to do. After a couple of minutes, I decided to delete my existing implementation and create a new one which name will be in accordance to the name specified in the functional document. But my problem is, how to delete this existing implicit implementation and replaced its name? If you see below, the name of the implementation is that in RED line just after the keyword ENHANCEMENT 318.














Now, I go back to the application menu to look for some links on how to delete this implementation and create a new one. But as you can see from this EDIT menu, you cant see any delete option for the existing implementation. What you see is CREATE, UNDO, REPLACE and CHANGE. All of these options are related to the ABAP codes and NOT on the name of the implementation.















After searching the net I find the solution. In order to solve this problem, you should go to transaction code SE80. Select package and enter the name of your package. From the list of object name is enhancement implementations. Scroll down and you will see the list of all enhancements under the same package. From the list, choose the name of your implementations.  Right click it using your mouse and from the context menu, select DELETE. Take note, it must be ACTIVE and it is NOT currently open.


This is a simplest way to delete implicit implementations. If you want to read also my blog on how to create a new implementations, please click it here.

Again, enjoy and keep on visiting my blogs. Until next post again... :)

How to implement an Implicit Enhancement in SAP objects?

Hello dearest ABAPers all over the world. It's Saturday today and its my rest day. Its me again sharing to you what I learned over the past few days of work. It was so busy week doing some developments on reports and enhancements as we are implementing a big project in Singapore. Well I do believe then that everyone of you have experiences now with these objects. 

Yesterday, I was assigned an object with our Project Manager. When I read the Functional design, it was an EXIT. Ohhh, it was an enhancement again. Upon checking the detail, I need to implement this exit - USEREXIT_NUMBER_RANGE. Since I am not sure with the project to implement, I made a google to find out what Include program handles this exit. Thats the amazing google can do for us... If you have questions, asks uncle Google not uncle Sam. :) hehehe....

In an instant, I got the result... It was MV45AFZZ handles this exit. Normally, an exit is an Include program called inside a Function Module. But in this case, this exit is a subroutine inside an Include program. 

So my next question now is, how can I modify this object (MV45AFZZ) since this is a SAP standard object? As you can see on the screenshot below, when clicking the edit button on the editor, it asks an access key. An access key is generated on the SAP market place.
But how about if you dont have an access in the SAP market place? Then how can you edit this exit then?

Implicit Enhancement Tutorial by ABAP Thinkers
So the most possible solution is Enhancement. It is a method develop by SAP to modify standard objects. So in this tutorial, I will discuss to you how to implement an enhancement. By the way, there are two types of enhancements. We have Implicit and Explicit enhancements. The simple difference I can say between the two are, Implicit enhancement is an enhancement that you declared (its not SAP who did). In short, this is a type of enhancement which has a Z* type object. Unlike with Explicit enhancement, it was SAP who defined this enhancement point. Do you get the difference between the two types of enhancement? I hope so. :) If not, just leave a message below and I will answer you the best I can.

So lets continue with this tutorial now. What I will discuss in details is how to implement an Implicit Enhancement. So just follow the steps and you will implement this type of enhancement into your next projects. Alright? So here are now the steps to do:

How to implement an Implicit Enhancement?
Steps:
1. In your editor, locate the "Enhance" button just like the one below. Click this button. Since what I am editing is an include program MV45AFZZ, I opened this in SE38 transaction code. By the way, you can implement any enhancements whether that is a Function Module, Class, Methods, BADI, BAPI, etc as long as you can see that "Enhance" button, implicit enhancement is possible.
Implicit Enhancement Tutorial by ABAP Thinkers

2. Once clicked the enhance button, the editor changes something like this one below. The editor looks grayed and the two buttons above Active<->Inactive and Enhancements appear in the application toolbar. The first button, Active<->Inactive is used to changed mode from the normal editor to enhancement editor. Try to click one at a time and you will see the difference between the two editor mode. The second button, Enhancement is used to activate any enhancement implementations. You will used this in the later part of this tutorial.
Implicit Enhancement Tutorial by ABAP Thinkers

3. The next step is to display those enhancement options. In order to do so, follow this path. On the menu, click Edit->Enhancement Operations -> Show Implicit Enhancement Options. This step is important because it shows where you can insert enhancement implementations. Otherwise, you cannot implement any implicit enhancement if you will not do this step.
Implicit Enhancement Tutorial by ABAP Thinkers
4. As you can see below, two bold arrows were shown at the left side of the editor. This signifies the start and end points where you can add enhancement points. However, please note that this step is only showing where an enhancement can be positioned. We can not add yet our own ABAP codes at this time. If you noticed, the editor is still grayed. We still need to create an enhancement implementations just like a normal EXIT or BADI. I know you are now eager to write your ABAP programs. Before you can create an enhancement implementation, position your cursor at the starting line just like this screenshot below. This is a MUST to-do since you cannot create a new enhancement implementations if your cursor is not positioned correctly.
Implicit Enhancement Tutorial by ABAP Thinkers

5. To create an enhancement implementation, go to the Menu again and follow this path.
Edit -> Enhancement Operations -> Create.
Implicit Enhancement Tutorial by ABAP Thinkers

6. On this pop up screen, it will first asks you the type of enhancement between declaration and code. Choose code in here as we will write ABAP codes. Honestly, I am not sure yet the difference between the two. But once I got a chance and time again, I will discover what this two is all about and add in this blog. But if you know now what makes them differ, please leave a message below for everyone else. I will be grateful to you.
Implicit Enhancement Tutorial by ABAP Thinkers

7. On this next pop up screen, you are required to select the enhancement implementation. Since this is a new implementation, we will create our own. Click the button at the right-bottom of the window. Its like a white folder paper. 
Implicit Enhancement Tutorial by ABAP Thinkers

8. This is the window where you will define the name of your enhancement implementation. Remember, it must begin with a Z* or Y* as this is a customized (Implicit) enhancement implementation. Also, give a short description for it. It must be descriptive. For the third parameter (composite enhancement implementation), just leave it blank. Then click the OK button after you are done.
Implicit Enhancement Tutorial by ABAP Thinkers

9. Since we are creating a new implicit enhancement implementation, it will ask you to save in a transport no. Just select your package and add this into your transport no if you want to move it to another system. But if you are just making a test, then saving as local object would suffice. 
Implicit Enhancement Tutorial by ABAP Thinkers

10. After adding it into your transport, select from the list of implementation the one you created. Just like in my case, I highlighted it then click the Ok buttom below. Congratulations as you are almost done implementing an Implicit enhancement.
Implicit Enhancement Tutorial by ABAP Thinkers

11. Finally and there you are, you can now add ABAP codes into our exit, USEREXIT_NUMBER_RANGE. As you can noticed, the grayed area while ago turns now into white which means we can write now our own ABAP Codes. Sorry but I did not write anything yet here since we will have a different logic to implement. You can also see the keywords ENHANCEMENT No. <name of enhancement implementation> and END ENHANCEMENT. Inside this keyword, you can write your own ABAP logic.
Implicit Enhancement Tutorial by ABAP Thinkers


12. And lastly, dont forget to click this button on the application toolbar. This is the activate button for the enhancement. Remember that any objects that is not activated will not take effect. Once done, you will see your object being activated. So if you want to go back to the normal editor, click the  button, Active<->Inactive button. 
Implicit Enhancement Tutorial by ABAP Thinkers


13. As a sidenote, If you want to go back again to your enhancement implementations, let say after one month there was a bug within your code, just do the same process. But rather clicking the create button, this time, click the change button to modify the existing implicit enhancement. Just follow this path to change:
Edit -> Enhancement Operations -> Change. 
Implicit Enhancement Tutorial by ABAP Thinkers


Congratulations. You can now implement an Implicit Enhancement into your projects. If you have further questions, please leave a message below. Until next week again. 

Enjoy :)

How to download the data structure of tables in SE11?

Good morning people around the world. Its me again doing some blogs. Sharing to you some tips and tricks in the SAP world. Well, before I s...