Application Development Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 

Generate the primary key automatically while records are saved

Former Member
0 Kudos

Hi experts,

I need to generate the primary key of a custom table automatically when a new record is saved. Pleas help asap. Urgent.

1 ACCEPTED SOLUTION

Former Member
0 Kudos

Hi Sriram,

You need to add the given code, in the function group program, which was generated when you create the table maintenance for your table..

Find the event which execute the save command of your table maintenance and add the given code..

Thanks and Best Regards,

Vikas Bittera.

**Points for usefull answers**

25 REPLIES 25

Former Member
0 Kudos

do you want to generate a random number..?

data: ran_int like qf00-ran_int.

data: ran_alp like qf00-ran_int.

data: ran_int_str type string.

data: ran_alp_str type string.

data: ran_str_out(10) type c.

do 50 times.

call function 'QF05_RANDOM_INTEGER'

exporting

ran_int_max = 99999

ran_int_min = 10000

importing

ran_int = ran_int.

ran_int_str = ran_int.

call function 'QF05_RANDOM_INTEGER'

exporting

ran_int_max = 99999

ran_int_min = 10000

importing

ran_int = ran_alp.

ran_alp_str = ran_alp.

concatenate ran_int_str ran_alp_str into ran_str_out.

condense ran_str_out no-gaps.

call function 'SCRAMBLE_STRING'

exporting

source = ran_str_out

importing

target = ran_str_out.

write:/ ran_str_out.

enddo.

Thanks

Mahesh

Former Member
0 Kudos

hi,

Please explain your problem again.

thanks.

0 Kudos

Hi vijay,

I have a custom created table with already some records in there. It has a character field of leght 10 as a primary key. My requirement is that whenever I save a new entry using sm30 table maintenance generator primary key should be genrated automatically

Former Member
0 Kudos

Well, what is the key you are trying to generate?

Rob

0 Kudos

Hi Rob,

Its a character field of length 10. Its a cusotm field.

0 Kudos

Then define a table maintenance generator for the table and at event 01(before save), call FM NUMBER_GET_NEXT. check the documentation on the FM.

Rob

0 Kudos

Hi Rob,

Can you plz explain in detail. Your solutions sounds Iike I will be solivng the problem. Thanks,

0 Kudos

Have you checked the doumentation for the FM yet?

Rob

Former Member
0 Kudos

Hi Sriram,

This is very simple..

Use this logic.

1) Select the highest value of the primary key in the table

SELECT MAX(key)

FROM xxxx

INTO w_count.

2) Create new key number adding 1 to this max number and use as a new key when creating a new record.

w_count = w_count + 1.

e_wa-key = w_count.

MODIFY xxxx FROM e_wa.

Thanks and Best Regards,

Vikas Bittera.

**Points for Usefull answers **

0 Kudos

Hi vikas,

I need to update the records in SM30 . Not in an ABAP program. Please give a solution to update from sm30.

thanks

0 Kudos

Hi vikas,

When I am using your code ,

Before the select statements I used data: statament to declare w-count and e_wa. But its giving an error saying select statement is not accessible. Plz tell me what to do.

thanks

Former Member
0 Kudos

Hi Sriram,

You need to add the given code, in the function group program, which was generated when you create the table maintenance for your table..

Find the event which execute the save command of your table maintenance and add the given code..

Thanks and Best Regards,

Vikas Bittera.

**Points for usefull answers**

0 Kudos

Hi vikas,

How to access the function group program. Plz tell me in detail as I am totally new to this. Thanks.

0 Kudos

Hi Rob,

I am looking into the fn.module. Plz let me know where to find the event 01

0 Kudos

You have to create the table maintenance generator first. Have you done that?

Rob

0 Kudos

Hi Rob,

Yes, I have created the table maintenance generator . I could not find the events you were talking about. thanks.

0 Kudos

Good - on the table maintenance generator screen, take the menu path:

Environment->Modification->Events

There is more documentation there.

Rob

0 Kudos

Hi vikas,

I found the event but could not find the exact location where to add it. Plz guide me. thanks.

0 Kudos

You don't have to worry about "where". SAP will take care of that. You just have to add the code.

Press "new entries" and add event '01'. this will create an include where you can place the call to the FM.

Rob

0 Kudos

Hi Rob,

When I am puttin some piece of code in the include rouitne. It is saying statment not accessible. Plz help in this regard.

Thanks

0 Kudos

You have to put your code in a form. It will be called dynamically.

Rob

0 Kudos

Hi Rob and vikas,

Thanks for your answers . Both of the solutions you mentioned were syntactically correct and activated but they are not populating the numbers.In case of Rob's soultion I guess I am not able to catch the return number. I am giving the code below. So Please look into it,

FORM gen_manu_siteid .

data : number type zsiteid value 155.

CALL FUNCTION 'NUMBER_GET_NEXT'

EXPORTING

NR_RANGE_NR = '02'

OBJECT = 'zmanusite'

QUANTITY = '1'

  • SUBOBJECT = ' '

  • TOYEAR = '0000'

  • IGNORE_BUFFER = ' '

IMPORTING

NUMBER = number

  • 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

  • .

*IF SY-SUBRC <> 0.

  • MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO

  • WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.

*ENDIF.

*

ztblqm_sqm-zsiteid = number .

ENDFORM. " gen_manu_siteid

When this did not work. i tried Vikas's soution and the code i used for this is

FORM gen_manu_siteid .

data : w_count like ztblqm_sqm-zsiteid,

e_wa like ztblqm_sqm occurs 0 with header line.

select max( zsiteid ) from ztblqm_sqm into w_count .

w_count = w_count + 1.

e_wa-zsiteid = w_count.

modify ztblqm_sqm from e_wa.

ENDFORM. " gen_manu_siteid

Please look into this and give me a solution.

NB: I have tried them at the event 01 include.

0 Kudos

Make sure number range ZMANUSITE is defined and specify it in upper case in the FM call. Try:


CALL FUNCTION 'NUMBER_GET_NEXT'
  EXPORTING
    nr_range_nr              = '02'
    object                   = 'ZMANUSITE'
    quantity                 = '1'
  IMPORTING
    number                   = number
   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.

Rob

0 Kudos

Hi Rob,

Finally i did it . i got my problem solved. Thanks for the help and responses man,

former_member194669
Active Contributor
0 Kudos

Hi,

se11--> enter table name > change> utilites> table maintenance generator> enviornment > modification>events

aRs