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: 

ABAP Program to Upload Users Throwing Errors - Help Optimize Please!

ravin_jugdav3
Explorer
0 Kudos
518

Hi, I am creating a program to upload users from a text file into SAP. The code is throwing up errors, and probably needs a little optimization. Your help appreciated. Here is the code, followed by the errors:

REPORT Z_USER_CREATE.

DATA: lv_filename TYPE string,

lv_file_content TYPE string,

lt_users TYPE TABLE OF sy-uname.

PARAMETERS: p_file TYPE rlgrap-filename.

START-OF-SELECTION.

CALL FUNCTION 'GUI_UPLOAD'

EXPORTING

filename = p_file

filetype = 'ASC'

CHANGING

data_tab = lt_users

EXCEPTIONS

file_open_error = 1

file_read_error = 2

no_batch = 3

gui_refuse_filetransfer = 4

invalid_type = 5

no_authority = 6

unknown_error = 7

bad_data_format = 8

header_not_allowed = 9

separator_not_allowed = 10

header_too_long = 11

unknown_dp_error = 12

access_denied = 13

dp_out_of_memory = 14

disk_full = 15

dp_timeout = 16

OTHERS = 17.

IF sy-subrc <> 0.

MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno

WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.

ENDIF.

LOOP AT lt_users INTO DATA(ls_user).

CREATE OBJECT ls_user TYPE bapiuser.

ls_user-username = ls_user.

" Set additional user data if required

APPEND ls_user TO lt_users.

ENDLOOP.

CALL FUNCTION 'BAPI_USER_CREATE1'

EXPORTING

user_tab = lt_users

EXCEPTIONS

user_name_exist = 1

user_lock = 2

password_generation_failed = 3

profile_creation_failed = 4

parameter_error = 5

OTHERS = 6.

IF sy-subrc <> 0.

MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno

WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.

ELSE.

COMMIT WORK.

WRITE: 'Users uploaded successfully.'.

ENDIF.

Errors:

5 REPLIES 5

Eduardo-CE
Active Participant
0 Kudos
324

Hi, why are you doing this?

xiswanto
Active Participant
324

not sure what version is your sap, but some weird thing you have done

1. BAPI_USER_CREATE1 does not have the importing user_tab parameter, are you using the right FM?
2. code below is really weird,
- you have created the ls_user with the same type as lt_users, but you wanted to change the workarea into another type, then you wanted to append workarea into itab with different structure, why?

LOOP AT lt_users INTO DATA(ls_user).
CREATE OBJECT ls_user TYPE bapiuser.
ls_user-username = ls_user.
APPEND ls_user TO lt_users.
ENDLOOP.

ravin_jugdav3
Explorer
0 Kudos
324

Hi Guys

Thanks for the answers.... and questions....I am trying to upload a list of users from a text file and create them en masse in SAP. Kindly if you could rewrite the above code to optimize it for me, I would highly appreciate.

Thanks very much.

raymond_giuseppi
Active Contributor
324

Some steps

  • Dig out your Abap course materials
  • Read the BAPI documentation to identify the minimal data required to create a user (Try with SE37)
  • Rewrite your code with a correct code of the BAPI (use the pattern option in SE38/SE80)
  • Third, after call of BAPI, look for error in RETURN parameter, if none call BAPI_TRANSACTION_COMMIT else BAPI_TRANSACTION_ROLLBACK
  • Write some log and/or display an execution report

ravin_jugdav3
Explorer
0 Kudos
324

Any other suggestions? Thanks.