BC BAPI创建用户

*&---------------------------------------------------------------------*
*& Report ZRPT_BC_CREATER_USER
*&---------------------------------------------------------------------*
*&vx:gavin_gxh
*&---------------------------------------------------------------------*
REPORT zrpt_bc_creater_user.
*&---------------------------------------------------------------------*
*& 数据定义
*&---------------------------------------------------------------------*
TABLES: sscrfields.
DATA: wa_username  LIKE bapibname-bapibname,
      wa_address   LIKE bapiaddr3,
      wa_logondata LIKE bapilogond,
      wa_password  LIKE bapipwd,
      wa_defaults  LIKE bapidefaul.
DATA: it_bapiret2  LIKE bapiret2  OCCURS 0 WITH HEADER LINE.

DATA:L_FILENAME TYPE STRING,L_MUBAN TYPE STRING.
DATA:GV_FULLPATH TYPE STRING,GV_PATH TYPE STRING,GV_NAME TYPE STRING.
DATA: BEGIN OF itab_load OCCURS 0,
        username       LIKE bapibname-bapibname,  "用户名
*        firstname      LIKE bapiaddr3-firstname,    "名
        lastname       LIKE bapiaddr3-lastname,   "姓
        tel1_numbr     LIKE bapiaddr3-tel1_numbr,
        e_mail(50),
        company(60),
        sort1          TYPE ad_sort1,
        department(40),
        function(40),
*        class(12) ,   "用户组
        gltgv(8),
        gltgb(8),
        passw(40),
      END OF itab_load.

DATA: return(01),
      message(100).

DATA: BEGIN OF itab_result OCCURS 0,
        username     LIKE bapibname-bapibname,  "用户名
        message(100),
      END OF itab_result.
*&---------------------------------------------------------------------*
*& 选择屏幕
*&---------------------------------------------------------------------*
SELECTION-SCREEN BEGIN OF BLOCK fu WITH FRAME TITLE TEXT-001.
  PARAMETERS:p_file LIKE rlgrap-filename MEMORY ID zy1 OBLIGATORY.
SELECTION-SCREEN END OF BLOCK fu.

SELECTION-SCREEN BEGIN OF LINE.
SELECTION-SCREEN PUSHBUTTON 5(20) but1 USER-COMMAND bt1.
*SELECTION-SCREEN PUSHBUTTON 30(40) but2 USER-COMMAND bt2.
SELECTION-SCREEN END OF LINE.

INITIALIZATION.
  DATA:
        lv_repid TYPE sy-repid.

  CALL FUNCTION 'ICON_CREATE'
    EXPORTING
      name                  = icon_bw_ref_structure_sap
      text                  = '模板下载'
      info                  = '模板下载'
    IMPORTING
      result                = but1
    EXCEPTIONS
      icon_not_found        = 1
      outputfield_too_short = 2
      OTHERS                = 3.

  lv_repid = sy-repid.
  sscrfields-functxt_01 = icon_export && '模版下载'.

*&---------------------------------------------------------------------*
*& At Selection-Screen
*&---------------------------------------------------------------------*
AT SELECTION-SCREEN.
  l_filename = 'ZSU01.xlsx'.
  l_muban = 'ZSU01'.
  CASE sscrfields-ucomm.
    WHEN 'BT1'.
      PERFORM download_data(zabap_common_program) USING l_filename l_muban gv_fullpath gv_path gv_name.
  ENDCASE.

*&---------------------------------------------------------------------*
*& AT SELECTION-SCREEN ON VALUE-REQUEST
*&---------------------------------------------------------------------*
AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_file.
  CALL FUNCTION 'F4_FILENAME'
    EXPORTING
      program_name  = syst-cprog
      dynpro_number = syst-dynnr
    IMPORTING
      file_name     = p_file.
*&---------------------------------------------------------------------*
*& START-OF-SELECTION
*&---------------------------------------------------------------------*
START-OF-SELECTION.
  PERFORM frm_get_data.
  PERFORM frm_create_user.
  PERFORM frm_display_log.
*&---------------------------------------------------------------------*
*&      Form  frm_get_data
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM frm_get_data .
  DATA:lt_raw TYPE truxs_t_text_data.
* 读取Excel文件数据到内表
  CALL FUNCTION 'TEXT_CONVERT_XLS_TO_SAP'
    EXPORTING
      i_tab_raw_data       = lt_raw
      i_filename           = p_file
    TABLES
      i_tab_converted_data = itab_load
    EXCEPTIONS
      conversion_failed    = 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.
  ENDIF.

  DELETE itab_load INDEX 1.

ENDFORM.                    " frm_get_data
*&---------------------------------------------------------------------*
*&      Form  frm_create_user
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM frm_create_user .
  DATA: lv_company TYPE bapiuscomp,
        lv_USALIAS TYPE usalias,
        ls_snc     TYPE bapisncu.
  LOOP AT itab_load.
    CLEAR wa_username.
    MOVE itab_load-username TO wa_username.

    CLEAR wa_logondata.
*    MOVE itab_load-class TO wa_logondata-class.
    wa_logondata-gltgv = itab_load-gltgv.
    wa_logondata-gltgb = itab_load-gltgb.
    wa_logondata-tzone = 'UTC+8'.
    wa_logondata-ustyp = 'A'.


    CLEAR wa_password.
    IF itab_load-passw IS INITIAL.
      itab_load-passw = 'Cimc11123$'.
    ENDIF.
    wa_password-bapipwd = itab_load-passw.  

    CLEAR wa_defaults.
    wa_defaults-dcpfm = 'X'.
    wa_defaults-datfm = '4'.
    wa_defaults-spld = 'LP01'.
    wa_defaults-spdb = 'G'.
    wa_defaults-spda = 'D'.


    CLEAR wa_address.
    wa_address-lastname = itab_load-lastname.
    lv_USALIAS = itab_load-lastname.
    wa_address-tel1_numbr = itab_load-tel1_numbr.
    wa_address-e_mail = itab_load-e_mail.
    wa_address-department = itab_load-department.
    wa_address-function = itab_load-function.
    wa_address-sort1_p = itab_load-sort1.

    IF itab_load-sort1 IS NOT INITIAL.
      SELECT SINGLE butxt INTO @lv_company FROM t001
        WHERE bukrs = @itab_load-sort1.
        IF sy-subrc NE 0.
          SELECT SINGLE name1 FROM t880
            WHERE substring( rcomp, 2, 4 ) =  @itab_load-sort1
            INTO @lv_company.
          ENDIF.
        ENDIF.
        REFRESH it_bapiret2. CLEAR it_bapiret2.

        CALL FUNCTION 'BAPI_USER_CREATE'
          EXPORTING
            username  = wa_username
            logondata = wa_logondata
            password  = wa_password
            defaults  = wa_defaults
            address   = wa_address
            company   = lv_company
            snc       = ls_snc
*           REF_USER  =
            alias     = lv_USALIAS
*           EX_ADDRESS                    =
*           UCLASS    =
*           FORCE_SYSTEM_ASSIGNMENT       =
*           SELF_REGISTER                 = ' '
          TABLES
*           PARAMETER =
            return    = it_bapiret2
*           ADDTEL    =
*           ADDFAX    =
*           ADDTTX    =
*           ADDTLX    =
*           ADDSMTP   =
*           ADDRML    =
*           ADDX400   =
*           ADDRFC    =
*           ADDPRT    =
*           ADDSSF    =
*           ADDURI    =
*           ADDPAG    =
*           ADDCOMREM =
*           GROUPS    =
*           PARAMETER1                    =
*           EXTIDHEAD =
*           EXTIDPART =
          .


        CLEAR return. CLEAR message.
        LOOP AT it_bapiret2 WHERE type = 'E' OR type = 'A'.
          return = 'X'.
          message = it_bapiret2-message.
          EXIT.
        ENDLOOP.

        IF return = 'X'.
          CALL FUNCTION 'BAPI_TRANSACTION_ROLLBACK'.

          MOVE itab_load-username TO itab_result-username.
          itab_result-message = message.

          APPEND itab_result.

        ELSE.
          CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'
            EXPORTING
              wait = '3'.

          MOVE itab_load-username TO itab_result-username.
          itab_result-message = '成功创建'.

          APPEND itab_result.

        ENDIF.

      ENDLOOP.

ENDFORM.                    " frm_create_user
*&---------------------------------------------------------------------*
*&      Form  frm_display_log
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM frm_display_log .
  LOOP AT itab_result.
    WRITE: / itab_result-username,
             itab_result-message.

  ENDLOOP.
ENDFORM.                    " frm_display_log

你可能感兴趣的:(BASIS,ABAP,经验分享,其他)