ABAP SAP UserID Creation
ABAP SAP UserID Creation
ABAP SAP UserID Creation
security Administrators. This is done through transaction code SU01. While creating SAP users there are only 2 mandatory fields User name on the Address tab and Initial Password field on the Logon Data tab page, all other entries on this screen are optional. But if proper roles are not assigned to the user, then the user wont be having any authorization to open any transaction code. If User is provided with SUPER in User Group, then he has all the SAP authorizations. But in real time the authorizations vary from User to User. So each user can be assigned to different roles. But the user IDs are to be created one by one. To make the process simple, the Transaction SU01 can be recorded and later using a BDC program all User IDs can be created in one go. Now the Question arises from where the Data for the User ID comes? Ans: 1. The Data can come in simple flat files (in Presentation Server or from Application Server) Or 2. The data can be accessed from HR infotypes, as for a new user all the personal data entered into HR infotypes through transaction PA30. As a result personal, Address, Communication information gets stored in PA002, PA006, and PA0105 and from there the User information can be collected. The following Report considers the data from Flat files only. Steps: 1. Go to transaction SHDB and record SU01 transaction.
3. Enter the mandatory fields (Which are normally User Last Name on Address tab and Password on Logon data tab)
Here one can provide the User group name in Logon data for different authorizations and also can provide different user based roles in Roles tab. 4. Upon saving the above the below screen appears.
5. Enter back button, it will ask to save. Save it. Now choose the recording and click on Program to transfer the recoding to a program.
Logic:
When the Display errors check boxed is marked then only the error file block gets enabled for accepting Error file path. When local file radio button is clicked then Server path gets disabled. Call transaction method is used for BDC. Users data from file is read to an internal table and then the same internal table is loop against the generated screen recording code to pass the values to internal table of type BDCDATA and messages into internal table of type BDCMSGCOLL. At the end the messages are formatted using standard function module to get a complete message. Source Code: *&---------------------------------------------------------------------* *& Report ZTEST_USER_CREATION *& *&---------------------------------------------------------------------* *& Created By : Debesh *& Date : January 31st,2013 *&---------------------------------------------------------------------* *&---------------------------------------------------------------------* *& Report ZTEST_USER_CREATION *& *&---------------------------------------------------------------------* *& Created By : Debesh *& Date : January 31st,2013 *&---------------------------------------------------------------------* report ztest_user_creation. ** Data Declration ** ** Types Declaration for USER Details ** types : begin of ty_usr, usrid type usr02-bname, lname type addr3_data-name_last, fname type addr3_data-name_first, pwd1(20) type c, pwd2(20) type c, end of ty_usr. types : begin of ty_msg , msg(60) type c, end of ty_msg. ** Internal tables declaration ** data : lt_usr type standard table of ty_usr, lt_bdc type standard table of bdcdata,
lt_error type standard table of bdcmsgcoll, lt_msgg type standard table of ty_msg. ** Work area Declaration ** data : lt_ftab type filetable, ls_usr type ty_usr, ls_bdc type bdcdata, ls_msg type bdcmsgcoll, ls_msgg type ty_msg. ** Variable declaration ** data : lv_rc type i, p_path type rlgrap-filename, lv_success type n value 0, lv_failure type n value 0.
** Select Check Box for Test Run ** selection-screen begin of block b1 with frame title text-001 . parameters : p_chk type c as checkbox user-command uc. selection-screen end of block b1. ** For Downloading Data File ** selection-screen begin of block b2 with frame title text-002. parameters : r_rad1 type c radiobutton group rg1 default 'X' user-command uc1, r_rad2 type c radiobutton group rg1. parameters : p_path1 type rlgrap-filename, p_path2 type rlgrap-filename. selection-screen end of block b2. ** For Uploading Error File ** selection-screen begin of block b3 with frame title text-003. parameters : r_rad3 type c radiobutton group rg2 default 'X' user-command uc2 modif id m2, r_rad4 type c radiobutton group rg2 modif id m2. parameters : p_path3 type rlgrap-filename modif id m2, p_path4 type rlgrap-filename modif id m2. selection-screen end of block b3. ** Selection Screen Modifications at selection-screen output. perform sub_validate_screen. ** Get File path at selection-screen on value-request for p_path1.
perform sub_loc_file_path changing p_path . p_path1 = p_path. at selection-screen on value-request for p_path2. perform sub_app_file_path changing p_path . p_path2 = p_path. at selection-screen on value-request for p_path3. perform sub_loc_file_path changing p_path . p_path3 = p_path. at selection-screen on value-request for p_path4. perform sub_app_file_path changing p_path . p_path4 = p_path. ********* Start Of Selection ******** start-of-selection. if not r_rad1 is initial. check lt_ftab is not initial. ** Read Local File into Internal table p_path = p_path1. perform sub_read_locfile using p_path. else. ** Read Server File into Internal table check p_path2 is not initial. p_path = p_path2. perform sub_read_serverfile using p_path. endif. check lt_usr is not initial. ** Call BDC code for SU01 to create User perform sub_create_user. check lt_msgg is not initial. ** Display System generated message perform sub_display_message. if p_chk is not initial. check lt_msgg is not initial. if not r_rad3 is initial. p_path = p_path3. perform sub_download_locfile using p_path. else.
p_path = p_path4. perform sub_download_serverfile using p_path. endif. endif. *&---------------------------------------------------------------------* *& Form sub_validate_screen *&---------------------------------------------------------------------* form sub_validate_screen . loop at screen. ** For test run no need to download error file if not p_chk = 'X'. if screen-group1 = 'M2'. * SCREEN-INVISIBLE = 1. screen-input = 0. modify screen. endif. endif. ** Disable Server Data File Path when Local Data file is choosen ** if screen-name cp 'P_PATH1'. if not r_rad1 is initial. screen-active = 1. * screen-required = 1. else. screen-input = 0. endif. modify screen. endif. ** Disable Local Data File Path when Server Data file is choosen ** if screen-name cp 'P_PATH2'. if not r_rad2 is initial. screen-active = 1. * screen-required = 1. else. screen-input = 0. endif. modify screen. endif. ** Disable Server Error File Path when Local Error file is choosen ** if screen-name cp 'P_PATH3'.
if not r_rad3 is initial. screen-active = 1. * screen-required = 1. else. screen-input = 0. endif. modify screen. endif. ** Disable Local Error File Path when Server Error file is choosen ** if screen-name cp 'P_PATH4'. if not r_rad4 is initial. screen-active = 1. * screen-required = 1. else. screen-input = 0. endif. modify screen. endif. endloop. endform. " sub_validate_screen *&---------------------------------------------------------------------* *& Form sub_loc_file_path *&---------------------------------------------------------------------* * WS_FILENAME_GET * F4_FILENAME * cl_gui_frontend_services=>directory_browse * can also be used * F4 Help for Local file *----------------------------------------------------------------------*
form sub_loc_file_path changing p_p_path . clear p_path. data: ls_path type file_table. call method cl_gui_frontend_services=>file_open_dialog * EXPORTING * WINDOW_TITLE = * DEFAULT_EXTENSION = * DEFAULT_FILENAME = * FILE_FILTER = * WITH_ENCODING =
* INITIAL_DIRECTORY = * MULTISELECTION = changing file_table = lt_ftab rc = lv_rc * USER_ACTION = * FILE_ENCODING = exceptions file_open_dialog_failed = 1 cntl_error =2 error_no_gui =3 not_supported_by_gui = 4 others =5 . if sy-subrc <> 0. message id sy-msgid type sy-msgty number sy-msgno with sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4. else. read table lt_ftab into ls_path index 1. if sy-subrc is initial. p_p_path = ls_path-filename. endif. endif. endform. " sub_loc_file_path
*&---------------------------------------------------------------------* *& Form sub_app_file_path *&---------------------------------------------------------------------* * F4 help for Application Server File *----------------------------------------------------------------------*
form sub_app_file_path changing p_p_path . clear p_path. data : lv_path type dxfields-longpath. * lv_path = p_p_path. call function 'F4_DXFILENAME_TOPRECURSION' exporting i_location_flag = 'A' * I_SERVER = '?' * i_path = lv_path
filemask = '*.*' fileoperation = 'R' importing * O_LOCATION_FLAG = * O_SERVER = o_path = lv_path * ABEND_FLAG = exceptions rfc_error =1 error_with_gui =2 others =3 . if sy-subrc <> 0. message id sy-msgid type sy-msgty number sy-msgno with sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4. else. p_p_path = lv_path. endif. endform. " sub_app_file_path *&---------------------------------------------------------------------* *& Form SUB_READ_LOCFILE *&---------------------------------------------------------------------* * Read File from presentation server into an internal table *----------------------------------------------------------------------* form sub_read_locfile using p_p_path . data : lv_file type string. lv_file = p_p_path. call method cl_gui_frontend_services=>gui_upload exporting filename = lv_file filetype = 'ASC' has_field_separator = 'X' * header_length =0 * read_by_line = 'X' * dat_mode = SPACE * codepage = SPACE * ignore_cerr = ABAP_TRUE * replacement = '#' * virus_scan_profile =
* IMPORTING * filelength = * header = changing data_tab = lt_usr 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 not_supported_by_gui = 17 error_no_gui = 18 others = 19 . 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. " SUB_READ_LOCFILE *&---------------------------------------------------------------------* *& Form SUB_READ_SERVERFILE *&---------------------------------------------------------------------* * Read file from Application server into an internal table *----------------------------------------------------------------------* form sub_read_serverfile using p_p_path . data : lv_loc type string.
data : ls_usr type ty_usr. open dataset p_p_path for input in text mode encoding default. if sy-subrc is initial. do. read dataset p_p_path into lv_loc. split lv_loc at cl_abap_char_utilities=>horizontal_tab into ls_usr-usrid ls_usr-lname ls_usr-fname ls_usr-pwd1 ls_usr-pwd2 . append ls_usr to lt_usr. clear ls_usr. enddo. close dataset p_p_path. endif. endform. " SUB_READ_SERVERFILE *&---------------------------------------------------------------------* *& Form SUB_CREATE_USER *&---------------------------------------------------------------------* * Write the generated code from transaction SHDB here *----------------------------------------------------------------------* form sub_create_user . loop at lt_usr into ls_usr. perform bdc_dynpro perform bdc_field perform bdc_field perform bdc_field perform bdc_dynpro perform bdc_field perform bdc_field perform bdc_field perform bdc_field perform bdc_field perform bdc_field perform bdc_dynpro perform bdc_field perform bdc_field perform bdc_field perform bdc_field perform bdc_field using 'SAPLSUU5' '0050'. using 'BDC_CURSOR' 'USR02-BNAME'. using 'BDC_OKCODE' '=CREA'. using 'USR02-BNAME' ls_usr-usrid. using 'SAPLSUU5' '0100'. using 'BDC_OKCODE' '=LOGO'. using 'BDC_CURSOR' 'ADDR3_DATA-NAME_FIRST'. using 'ADDR3_DATA-NAME_LAST' ls_usr-lname. using 'ADDR3_DATA-NAME_FIRST' ls_usr-fname.. using 'ADDR3_DATA-LANGU_P' 'EN'. using 'ADDR3_DATA-DEFLT_COMM' 'RML'. using 'SAPLSUU5' '0100'. using 'BDC_OKCODE' '=UPD'. using 'BDC_CURSOR' 'G_PASSWORD2'. using 'USLOGOND-USTYP' 'A'. using 'G_PASSWORD1' ls_usr-pwd1. using 'G_PASSWORD2' ls_usr-pwd2.
call transaction 'SU01' using lt_bdc mode 'N' messages into lt_error. if sy-subrc is initial. lv_success = lv_success + 1. else. lv_failure = lv_failure + 1. endif. perform sub_format. clear ls_usr. refresh lt_bdc. endloop. endform. " SUB_CREATE_USER *&---------------------------------------------------------------------* *& Form BDC_DYNPRO *&---------------------------------------------------------------------* * Send BDC screen details to BDC Data *----------------------------------------------------------------------* * -->P_0668 program name * -->P_0669 screen no *----------------------------------------------------------------------* form bdc_dynpro using value(p_0668) value(p_0669). clear ls_bdc. ls_bdc-program = p_0668. ls_bdc-dynpro = p_0669. ls_bdc-dynbegin = 'X'. append ls_bdc to lt_bdc. ls_bdc-fnam = ''. endform. " BDC_DYNPRO *&---------------------------------------------------------------------* *& Form BDC_FIELD *&---------------------------------------------------------------------* * Send Screen field values to BDC DATA *----------------------------------------------------------------------* * -->P_0673 field name * -->P_0674 field value *----------------------------------------------------------------------* form bdc_field using value(p_0673) value(p_0674). clear ls_bdc.
ls_bdc-fnam = p_0673. ls_bdc-fval = p_0674. append ls_bdc to lt_bdc. endform. " BDC_FIELD *&---------------------------------------------------------------------* *& Form SUB_DISPLAY_MESSAGE *&---------------------------------------------------------------------* * Display Messages *----------------------------------------------------------------------* form sub_display_message . data : lv_msg type string. concatenate 'No of users successfully created = ' lv_success into lv_msg. write : 5 lv_msg. new-line. concatenate 'No of user creation failed = ' lv_failure into lv_msg. write : 5 lv_msg. clear lv_msg. loop at lt_msgg into ls_msgg. new-line. write : 15 ls_msgg-msg. endloop. endform. " SUB_DISPLAY_MESSAGE *&---------------------------------------------------------------------* *& Form SUB_FORMAT *&---------------------------------------------------------------------* * Format message *----------------------------------------------------------------------* form sub_format . read table lt_error into ls_msg index 1. call function 'FORMAT_MESSAGE' exporting id = ls_msg-msgid lang = 'EN' no = ls_msg-msgnr v1 = ls_msg-msgv1 v2 = ls_msg-msgv2
v3 = ls_msg-msgv3 v4 = ls_msg-msgv4 importing msg = ls_msgg-msg exceptions not_found = 1 others = 2. if sy-subrc <> 0. message id sy-msgid type sy-msgty number sy-msgno with sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4. else. append ls_msgg to lt_msgg. endif. refresh lt_error. endform. " SUB_FORMAT *&---------------------------------------------------------------------* *& Form SUB_DOWNLOAD_LOCFILE *&---------------------------------------------------------------------* * Download messages into local file *----------------------------------------------------------------------* * -->P_P_PATH text *----------------------------------------------------------------------* form sub_download_locfile using p_p_path. data : lv_fpath type string . lv_fpath = p_p_path. call method cl_gui_frontend_services=>gui_download exporting * bin_filesize = filename = lv_fpath * filetype = 'ASC' * append = SPACE * write_field_separator = SPACE * header = '00' * trunc_trailing_blanks = SPACE * write_lf = 'X' * col_select = SPACE * col_select_mask = SPACE * dat_mode = SPACE * confirm_overwrite = SPACE * no_auth_check = SPACE * codepage = SPACE
* * * * * * * * * *
ignore_cerr = ABAP_TRUE replacement = '#' write_bom = SPACE trunc_trailing_blanks_eol = 'X' wk1_n_format = SPACE wk1_n_size = SPACE wk1_t_format = SPACE wk1_t_size = SPACE IMPORTING filelength = changing data_tab = lt_msgg exceptions file_write_error =1 no_batch =2 gui_refuse_filetransfer = 3 invalid_type =4 no_authority =5 unknown_error =6 header_not_allowed =7 separator_not_allowed = 8 filesize_not_allowed = 9 header_too_long = 10 dp_error_create = 11 dp_error_send = 12 dp_error_write = 13 unknown_dp_error = 14 access_denied = 15 dp_out_of_memory = 16 disk_full = 17 dp_timeout = 18 file_not_found = 19 dataprovider_exception = 20 control_flush_error = 21 not_supported_by_gui = 22 error_no_gui = 23 others = 24 . 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. " SUB_DOWNLOAD_LOCFILE *&---------------------------------------------------------------------* *& Form SUB_DOWNLOAD_SERVERFILE *&---------------------------------------------------------------------* * Download messages into server file *----------------------------------------------------------------------* * -->P_P_PATH text *----------------------------------------------------------------------* form sub_download_serverfile using p_p_path. open dataset p_p_path for output in text mode encoding default. if sy-subrc is initial. loop at lt_msgg into ls_msgg . transfer ls_msgg-msg to p_p_path. endloop. close dataset p_p_path. endif. endform. " SUB_DOWNLOAD_SERVERFILE
Output: Upon executing record using a tab separated file , the resulting screen looks like this and the error file gets downloaded to respective path.
Now the user can login to SAP using the User ID and Password from the file. The system will ask to change the password, now the User can change the password. The details get updated in the standard Logon Data table USR02.
Author Details: Debesh Bhabani Swain Contact: 8123032818 Email Id: [email protected] [email protected] Working in Wipro as an Associate SAP ABAP Consultant from last 2.5 Years.