Skip to Content

Archived discussions are read-only. Learn more about SAP Q&A

USERS_GEN

Hello i need to generate users for our new system

The Users already exists.

When i do it using the USERS_GEN everything works fine. But I have to do for many users.

So I developed a BDC. However the BDC dosent work nor give any error message.

I am not sure any more methods.

the data provided in the text Sheet, is

ORGUNIT;COUNTRY;USER

50000185;US;SRM0011

50000185;US;SRM0012

Please help.

Ster

Former Member
Former Member replied

I can understand what you mean. This becomes a pain when there is huge list to org assign. Please find a code sample below which will solve your problem (This needs some cleanup, I wrote that in a hurry , hope you can manage. But the code works fine)

You can use a text file with user ids alone.

for example: user list1.txt

TEST_001
TEST_002
TEST_003

Here is the code

*&---------------------------------------------------------------------*
*& Report  ZKB_ASSIGN_USERS
*&---------------------------------------------------------------------*

REPORT  zkb_assign_users.

* Selection Screen
PARAMETERS: p_orgid TYPE pd_objid_r OBLIGATORY.
PARAMETERS: p_file  TYPE string     OBLIGATORY.


* File Open Dialog
AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_file.

  DATA: lt_filetab TYPE filetable,
        ls_filetab TYPE file_table.

  DATA: lv_rc TYPE i.

  CALL METHOD cl_gui_frontend_services=>file_open_dialog
    EXPORTING
      default_extension       = '*.TXT'
    CHANGING
      file_table              = lt_filetab
      rc                      = lv_rc
    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_filetab
         INTO ls_filetab INDEX 1.
    IF sy-subrc EQ 0.
      p_file = ls_filetab-filename.
    ENDIF.
  ENDIF.


* Start Of Selection
START-OF-SELECTION.

  TYPES: BEGIN OF lty_users_list,
          userid TYPE xubname,
         END OF lty_users_list.
  DATA: lt_users_list TYPE TABLE OF lty_users_list.
  DATA: ls_users_list TYPE lty_users_list.

  DATA: lv_def_country TYPE hri1028-land1.
  DATA: lv_dum_country TYPE bapibus1006_address-country.

  CALL FUNCTION 'BBPU_GET_DEFAULT_COUNTRY'
    IMPORTING
      default_country            = lv_dum_country
    EXCEPTIONS
      no_default_country_defined = 1
      OTHERS                     = 2.
  IF sy-subrc = 0.
    lv_def_country = lv_dum_country.
  ENDIF.

  CALL FUNCTION 'BBPU_CHECK_ORGUNIT'
    EXPORTING
      objid                     = p_orgid
      show_error_message        = 'X'
    EXCEPTIONS
      no_active_plvar           = 1
      orgunit_not_found         = 2
      orgunit_has_no_address    = 3
      bupa_of_orgunit_not_found = 4
      bupa_has_no_address       = 5
      OTHERS                    = 6.
  IF sy-subrc <> 0.
    MESSAGE 'Invalid Org Id'(001) TYPE 'I'.
    EXIT.
  ENDIF.

  CALL FUNCTION 'BBPU_CHECK_COUNTRY'
    EXPORTING
      country                = lv_def_country
      show_error_message     = 'X'
    EXCEPTIONS
      country_does_not_exist = 1
      OTHERS                 = 2.
  IF sy-subrc <> 0.
    MESSAGE 'Invalid/No Default Country'(002) TYPE 'I'.
    EXIT.
  ENDIF.

  CALL METHOD cl_gui_frontend_services=>gui_upload
    EXPORTING
      filename                = p_file
      filetype                = 'ASC'
*      has_field_separator     = ' '
    CHANGING
      data_tab                = lt_users_list
    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.
    EXIT.
  ENDIF.

  LOOP AT lt_users_list INTO ls_users_list.

    CALL FUNCTION 'BP_CENTRALPERSON_GET'
      EXPORTING
        iv_username         = ls_users_list-userid
      EXCEPTIONS
        no_central_person   = 1
        no_business_partner = 2
        no_id               = 3
        OTHERS              = 4.
    IF sy-subrc = 0.
      WRITE:/ 'User Id', ls_users_list-userid, 'is already assigned'.
      CONTINUE.
    ENDIF.

    PERFORM assign_user USING ls_users_list-userid
                              space
                              p_orgid
                              lv_def_country
                              'X'.

  ENDLOOP.
*&---------------------------------------------------------------------*
*&      Form  ASSIGN_USER
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*      -->P_LS_USERS_LIST_USERID  text
*      -->P_SPACE  text
*      -->P_P_ORGID  text
*      -->P_LV_DEF_COUNTRY  text
*      -->P_0348   text
*----------------------------------------------------------------------*
FORM assign_user  USING p_bname
                        p_rfc
                        p_orgunit
                        p_country
                        p_no_su01_user_creation LIKE bbpsapnew-customized.

  TABLES: tsad2, tsad3t.

  DATA: email_dummy LIKE bapiadsmtp.
  DATA: actgroups2 LIKE bapiagr OCCURS 0 WITH HEADER LINE.
  DATA: cp_inhouse_address LIKE bapibus1006002_address.
  DATA: cpadtel LIKE bapiadtel.
  DATA: cpadfax LIKE bapiadfax.
  DATA: cpadsmtp LIKE bapiadsmtp.
  DATA: password(8) TYPE c.
  DATA  ls_employee_data TYPE  bbps_om_usr_employee_data.
  DATA  ls_workplace_address TYPE  bbps_om_usr_addr3.
  DATA  lv_flag TYPE xflag.
  DATA  lv_usr_password TYPE  bapipwd.
  DATA: length_diff TYPE i.
  DATA: lead_initials_length TYPE i.
  DATA: start_number_length TYPE i.
  DATA: start_number_str TYPE string.
  DATA: fill TYPE string.
  DATA: correct_file_data LIKE bbp_ustru OCCURS 0 WITH HEADER LINE.
  DATA: l_bbp_cust LIKE bbp_cust.
  DATA: logondata LIKE  bapilogond,
        defaults LIKE  bapidefaul,
        address LIKE  bapiaddr3,
        company LIKE  bapiuscomp,
        snc LIKE  bapisncu.
  DATA: return LIKE bapiret2 OCCURS 0 WITH HEADER LINE.
  DATA: parameter LIKE bapiparam OCCURS 0 WITH HEADER LINE,
        profiles LIKE  bapiprof OCCURS 0 WITH HEADER LINE ,
        activitygroups LIKE  bapiagr OCCURS 0 WITH HEADER LINE,
        addtel LIKE  bapiadtel OCCURS 0 WITH HEADER LINE ,
        addfax LIKE  bapiadfax OCCURS 0 WITH HEADER LINE ,
        addttx LIKE  bapiadttx OCCURS 0 WITH HEADER LINE ,
        addtlx LIKE  bapiadtlx OCCURS 0 WITH HEADER LINE ,
        addsmtp LIKE  bapiadsmtp OCCURS 0 WITH HEADER LINE ,
        addrml LIKE  bapiadrml OCCURS 0 WITH HEADER LINE ,
        addx400 LIKE  bapiadx400 OCCURS 0 WITH HEADER LINE ,
        addrfc LIKE bapiadrfc OCCURS 0 WITH HEADER LINE ,
        addprt LIKE bapiadprt OCCURS 0 WITH HEADER LINE ,
        addssf LIKE bapiadssf OCCURS 0 WITH HEADER LINE ,
        adduri LIKE bapiaduri OCCURS 0 WITH HEADER LINE ,
        addpag LIKE bapiadpag OCCURS 0 WITH HEADER LINE ,
        addcomrem LIKE bapicomrem OCCURS 0 WITH HEADER LINE ,
        parameter1 LIKE bapiparam1 OCCURS 0 WITH HEADER LINE ,
        groups LIKE bapigroups OCCURS 0 WITH HEADER LINE.
  DATA: generated_password(8) TYPE c.
  DATA: debug_mode LIKE bbpsapnew-customized.
  DATA: use_pw_mode LIKE bbpsapnew-customized.
  DATA  iv_position_stext TYPE stext.

  CLEAR generated_password.

  SELECT SINGLE * FROM bbp_cust INTO l_bbp_cust
                        WHERE id = 'RFC_READ_USE_PW'.
  IF sy-subrc = 0 AND l_bbp_cust-path = 'YES'.
    use_pw_mode = 'X'.
  ENDIF.
  SELECT SINGLE * FROM bbp_cust INTO l_bbp_cust
                        WHERE id = 'DEBUG_UPLOAD_GEN'.
  IF sy-subrc = 0 AND l_bbp_cust-path = 'YES'.
    debug_mode = 'X'.
  ENDIF.

  REFRESH return. CLEAR return.
  CALL FUNCTION 'BAPI_USER_GET_DETAIL' DESTINATION p_rfc
    EXPORTING
      username       = p_bname
    IMPORTING
      logondata      = logondata
      defaults       = defaults
      address        = address
      company        = company
      snc            = snc
    TABLES
      parameter      = parameter
      profiles       = profiles
      activitygroups = activitygroups
      return         = return
      addtel         = addtel
      addfax         = addfax
      addttx         = addttx
      addtlx         = addtlx
      addsmtp        = addsmtp
      addrml         = addrml
      addx400        = addx400
      addrfc         = addrfc
      addprt         = addprt
      addssf         = addssf
      adduri         = adduri
      addpag         = addpag
      addcomrem      = addcomrem
      parameter1     = parameter1
      groups         = groups
    EXCEPTIONS
      OTHERS         = 1.
  IF sy-subrc <> 0.
    return-type   = 'E'.
    return-id     = 'BBPU_MESSAGES'.
    return-number = '035'.
    APPEND return.
  ENDIF.
  LOOP AT return WHERE type = 'E' OR type = 'A'.
  ENDLOOP.
  IF sy-subrc = 0.
    LOOP AT return WHERE type = 'E' OR type = 'A'.
      WRITE:/ return-message.
    ENDLOOP.
    EXIT.
  ENDIF.
  CLEAR: defaults-stcod,
         defaults-spld,
         defaults-splg,
         defaults-spdb,
         defaults-spda,
         defaults-cattkennz,
         defaults-kostl,
         defaults-start_menu.

  IF NOT address-title_aca1 IS INITIAL.
    SELECT SINGLE * FROM tsad2 WHERE title_text = address-title_aca1.
    IF sy-subrc <> 0.
      CLEAR tsad2.
    ENDIF.
  ELSE.
    CLEAR tsad2.
  ENDIF.
  IF NOT address-title_p IS INITIAL.
    SELECT SINGLE * FROM tsad3t WHERE title_medi = address-title_p.
    IF sy-subrc <> 0.
      CLEAR tsad3t.
    ENDIF.
  ELSE.
    CLEAR tsad3t.
  ENDIF.
  IF address-lastname IS INITIAL.
    address-lastname = p_bname.
  ENDIF.
  IF ( address-e_mail IS INITIAL )
     OR ( address-e_mail EQ '@' ).
    MOVE 'email@dummy' TO cpadsmtp-e_mail.
  ELSE.
    MOVE address-e_mail TO cpadsmtp-e_mail.
    CALL FUNCTION 'RSEC_GENERATE_PASSWORD'
      EXPORTING
        alphabet      = 'ABCDEFGHIJKLMNOPQUVWXYZ1234567890'
        output_length = 8
      IMPORTING
        output        = generated_password
      EXCEPTIONS
        some_error    = 1
        OTHERS        = 2.
    IF sy-subrc <> 0.
      CLEAR generated_password.
    ENDIF.
  ENDIF.
  IF generated_password IS INITIAL.
    password = 'abcd1234'.
  ELSE.
    password = generated_password.
  ENDIF.
  IF use_pw_mode = 'X'.
    password = 'abcd1234'.
  ENDIF.

  ls_employee_data-firstname   = address-firstname.
  ls_employee_data-lastname    = address-lastname.
  ls_employee_data-title_key   = tsad3t-title.
  ls_employee_data-title_aca1  = tsad2-title_key.
  ls_employee_data-searchterm1 = address-firstname.
  ls_employee_data-searchterm2 = address-lastname.

  ls_workplace_address-standardaddress = 'X'.
  ls_workplace_address-building_p      = address-building_p.
  ls_workplace_address-room_no         = address-room_no_p.
  ls_workplace_address-inhouse_ml      = address-inhouse_ml.
  ls_workplace_address-comm_country    = p_country.
  ls_workplace_address-e_mail_address  = cpadsmtp-e_mail.
  ls_workplace_address-tel_number      = address-tel1_numbr.
  ls_workplace_address-tel_extension   = address-tel1_ext.
  ls_workplace_address-fax_number      = address-fax_number.
  ls_workplace_address-fax_extension   = address-fax_extens.
  MOVE correct_file_data-manager TO lv_flag.
  MOVE password TO lv_usr_password.

  CALL FUNCTION 'BBPU_CHECK_FLOOR_FIELD_ACTIVE'
    EXCEPTIONS
      field_not_active = 1.
  IF sy-subrc = 0.
    MOVE address-floor_p TO ls_workplace_address-floor.
  ENDIF.

  TRY.
      CLEAR iv_position_stext.
      CONCATENATE ls_employee_data-firstname
                  ls_employee_data-lastname
                  INTO iv_position_stext SEPARATED BY space.
      CALL FUNCTION 'BBP_OM_USR_CREATE_EMPLOYEE_EXT'
        EXPORTING
          is_employee_data     = ls_employee_data
          is_workplace_address = ls_workplace_address
          iv_orgunit_id        = p_orgunit
          iv_position_short    = p_bname
          iv_position_stext    = iv_position_stext
          iv_is_mgr_position   = lv_flag
          iv_username          = p_bname
          iv_usr_password      = lv_usr_password
          iv_usr_email         = cpadsmtp-e_mail
          is_usr_defaults      = defaults
          iv_user_exists       = p_no_su01_user_creation.
    CATCH cx_bbp_om_internal_error.
      MESSAGE e001(bbpu_messages).
    CATCH cx_bbp_om_not_found.
      MESSAGE e001(bbpu_messages).
    CATCH cx_bbp_om_conflict.
      MESSAGE e001(bbpu_messages).
    CATCH cx_bbp_om_no_authorization.
      MESSAGE e003(bbpu_messages).
  ENDTRY.
  CALL FUNCTION 'BBPU_USER_ACTGROUPS_ASSIGN_ZBV'
    EXPORTING
      username          = p_bname
      send_notification = space
    TABLES
      activitygroups    = activitygroups
      return            = return.

  WRITE:/ 'User Id', p_bname, 'is assigned successfully'.

ENDFORM.                    " ASSIGN_USER

0 View this answer in context

Helpful Answer

by
Not what you were looking for? View more on this topic or Ask a question