Code for automatically generating Primary Key value for a Table:
Create Table Maintenance Generator for the table
Create Table – Transaction SE11
Now GOTO > Utilities > Table Maintenance generator
Now create Table Maintenance Generator and Save it
Now for working on the Table Actions we need to trigger a Table Event
GOTO > Environment > Modification > Events
After clicking on Events screen below appears, check for the available Table Maintenance Dialog Events by clicking F4 help for 1st field. We will get the list of all the standard events defined in the SAP system.
Here we can see 39 standard Event entries which we can use for any table operation:
Here we are taking the value 05(Create a new entry), this event will trigger whenever we will try to create an entry for the table/try to insert new value in the table.
Now in next field we need to provide the form routine name, in our case we are providing as EVE.
Now save it, after saving we will be provided with the Editor where we can write our logic.
Here we can see, now we will be able to write code inside the editor, just click on it and we will be prompted for new include, click and we will be in the editor.
Now write the below code.
DATA: w_count type Tablename–Fieldname ,
w_range TYPE INRI–NRRANGENR VALUE ’01’ .
DATA : NUM(7) TYPE C ,
NUM1(2) TYPE C VALUE ‘DT’ .
CALL FUNCTION ‘NUMBER_GET_NEXT’
nr_range_nr = w_range
object = ‘ZTNRO’
* QUANTITY = ‘1’
* SUBOBJECT = ‘ ‘
* TOYEAR = ‘0000’
* IGNORE_BUFFER = ‘ ‘
NUMBER = w_count
* QUANTITY =
* RETURNCODE =
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
CONCATENATE NUM1 w_count INTO NUM .
IF sy–subrc = 0.
* Implement suitable error handling here
Tablename-fieldname = NUM .
In this code as we can see we need to pass a number range object
Create a number range object in Transaction SNRO
GOTO > Transaction SNRO
Click on create
After saving we will get 2 options on our screen
Number Ranges and Change Document
GOTO > Number ranges > Intervals (Edit)
Now add an Interval for Number Range
Click on Insert and save it
Now we have to pass the same Number Range Object name in the function module NUMBER_GET_NEXT
Now go to the Maintenance Screen of the Table Maintenance Generator of the table
Double Click on screen number of Overview screen
Now it will go to the Screen Painter for that Overview screen
We need to write below code in the Include
Now create a new module in the Event PBO (Process Before Output)
The following code we need to write :-
IF SY–UCOMM = ‘NEWL’ OR SY–UCOMM = ‘SAVE’.
LOOP AT SCREEN.
IF SCREEN–NAME = ‘Tablename-fieldname’.
SCREEN–INPUT = 0 .
MODIFY SCREEN .
NOTE: It is very important to activate the Function Group after the code.
GOTO > SE80 > Select the Function Group