^_^函数创建用户并分配权限

时间:2022-08-21 08:48:38
"李渊 测试 创建用户 函数BAPI_USER_CREATE1

DATA:LS_DATA   TYPE BAPILOGOND,
     LS_ADDR   TYPE BAPIADDR3,
     LS_DEFU   TYPE BAPIDEFAUL,
     LS_PWD    TYPE BAPIPWD,
     GT_RETURN TYPE TABLE OF BAPIRET2,
     GW_RETURN LIKE LINE OF GT_RETURN,
     GV_DEST   TYPE RFCDEST,
     GT_FILE   TYPE TABLE OF BAPIPROF,
     GW_FILE   LIKE LINE OF GT_FILE.

CALL FUNCTION 'CRM_OFI_GET_RFCDEST'
  IMPORTING
    EV_RFCDEST       = GV_DEST
  EXCEPTIONS
    NO_RFCDEST       = 1
    MULTIPLE_RFCDEST = 2
    OTHERS           = 3.
IF SY-SUBRC <> 0.
ENDIF.

"LODIN DATA
LS_DATA-GLTGV = SY-DATUM - 1.
LS_DATA-GLTGB = SY-DATUM + 1.
LS_DATA-USTYP = 'A'.
"set efault data
LS_DEFU-LANGU = 'E'.
LS_DEFU-SPLD = 'LP01'.
LS_DEFU-DATFM = '4'.
LS_DEFU-DCPFM = 'X'.
LS_DEFU-TIMEFM = '0'.
"set password
LS_PWD-BAPIPWD = '123456'.
"set name
LS_ADDR-LASTNAME = 'Lee'.
"call func create user
CALL FUNCTION 'BAPI_USER_CREATE1' DESTINATION GV_DEST
  EXPORTING
    USERNAME  = 'ANDY01'
*   NAME_IN   =
    LOGONDATA = LS_DATA
    PASSWORD  = LS_PWD
    DEFAULTS  = LS_DEFU
    ADDRESS   = LS_ADDR
  TABLES
    RETURN    = GT_RETURN.

READ TABLE GT_RETURN INTO GW_RETURN WITH KEY TYPE = 'E'.
IF SY-SUBRC = 0.
  MESSAGE ID GW_RETURN-ID TYPE GW_RETURN-TYPE NUMBER GW_RETURN-NUMBER.
ELSE.
  REFRESH:GT_RETURN.
  GW_FILE-BAPIPROF = 'SAP_ALL'.
  GW_FILE-BAPITYPE = 'C'.
  GW_FILE-BAPIAKTPS = 'X'.
  APPEND GW_FILE TO GT_FILE.
  GW_FILE-BAPIPROF = 'SAP_NEW'.
  GW_FILE-BAPITYPE = 'C'.
  GW_FILE-BAPIAKTPS = 'X'.
  APPEND GW_FILE TO GT_FILE.
  "assign sap_all to user
  CALL FUNCTION 'BAPI_USER_PROFILES_ASSIGN' DESTINATION GV_DEST
    EXPORTING
      USERNAME = 'ANDY01'
    TABLES
      PROFILES = GT_FILE
      RETURN   = GT_RETURN.
  READ TABLE GT_RETURN INTO GW_RETURN WITH KEY TYPE = 'E'.
  IF SY-SUBRC = 0.
    WRITE 'Assign Profile to User error'.
  ELSE.
  ENDIF.
ENDIF.