Skip to Content
Author's profile photo Former Member

Code for automatically generating Primary Key value for a Table

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

Img1.JPG

Now create Table Maintenance Generator and Save it

Img2.JPG

Now for working on the Table Actions we need to trigger a Table Event

GOTO > Environment > Modification > Events

Img3.JPG

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.

Img4.jpg

Here we can see 39 standard Event entries which we can use for any table operation:

Img5.jpg

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.

Img6.jpg

Now save it, after saving we will be provided with the Editor where we can write our logic.

Img7.jpg

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 TablenameFieldname ,
w_range
TYPE INRINRRANGENR VALUE ’01’ .

DATA : NUM(7) TYPE C ,
NUM1
(2) TYPE C VALUE ‘DT’ .

CALL FUNCTION ‘NUMBER_GET_NEXT’
EXPORTING
nr_range_nr                  
= w_range
object                       
= ‘ZTNRO’
*     QUANTITY                      = ‘1’
*     SUBOBJECT                     = ‘ ‘
*     TOYEAR                        = ‘0000’
*     IGNORE_BUFFER                 = ‘ ‘
IMPORTING
NUMBER                        = w_count
*     QUANTITY                      =
*     RETURNCODE                    =
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
.
CONCATENATE NUM1 w_count INTO NUM .
IF sysubrc = 0.
* Implement suitable error handling here
Tablename-fieldname
= NUM .
ENDIF.

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

Img8.jpg

Click on create

Img9.jpg

After saving we will get 2 options on our screen

Number Ranges and Change Document

Img10.jpg

GOTO > Number ranges > Intervals (Edit)

Img11.jpg

Now add an Interval for Number Range

Img12.jpg

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

Img13.jpg

Now it will go to the Screen Painter for that Overview screen

Img14.jpg

We need to write below code in the Include

Img15.jpg

Now create a new module in the Event PBO (Process Before Output)

The following code we need to write :-

IF SYUCOMM = ‘NEWL’ OR SYUCOMM = ‘SAVE’.

LOOP AT SCREEN.

IF SCREENNAME = ‘Tablename-fieldname’.

SCREENINPUT = 0 .
MODIFY SCREEN .

ENDIF.

ENDLOOP.

ENDIF.

NOTE: It is very important to activate the Function Group after the code.

GOTO > SE80 > Select the Function Group

Assigned Tags

      3 Comments
      You must be Logged on to comment or reply to a post.
      Author's profile photo Kesavadas Thekkillath
      Kesavadas Thekkillath

      I always do not prefer to modify the flow logic of generated table maintenance. It is useless when the maintenance is regenerated again. We have to use maintenance events.

      Author's profile photo Former Member
      Former Member

      Very amazing document with generate table document . i like your document please message me link like this document

      Author's profile photo Sun Andy
      Sun Andy

      Hello,

      This solution is very help full. But in my scenario, the error is occurred. Please help to how can I resolve this error.

      "Unable to interpret "OUTPUT". Possible causes of error include. incorrect spellings or comma errors."

      here is my code.

      MODULE CHANGE output.
      IF SY–UCOMM 'NEWL' OR SY–UCOMM 'SAVE'.
      LOOP AT SCREEN.
      IF SCREEN–NAME  =  "ZMCOM-COMPL_NO".
      SCREEN–INPUT .
      MODIFY SCREEN .
      ENDIF.
      ENDLOOP.
      ENDIF.
      ENDMODULE.