Registration

Dear SAP Community Member,
In order to fully benefit from what the SAP Community has to offer, please register at:
http://scn.sap.com
Thank you,
The SAP Community team.
Skip to end of metadata
Go to start of metadata

REPORT  Z_TEMP_USER_DELETE.
TYPE-POOLS: SLIS.
TABLES: USR02.
PARAMETERS: CTUMODE LIKE CTU_PARAMS-DISMODE DEFAULT 'E' NO-DISPLAY.
CONSTANTS:
      C_TYPDIA TYPE USR02-USTYP VALUE 'A'. "Dialog
SELECT-OPTIONS:
      S_CLASS  FOR  USR02-CLASS NO-DISPLAY,
      S_TEMP FOR USR02-BNAME NO-DISPLAY.
TYPES: BEGIN OF TY_USR02,
       BNAME TYPE USR02-BNAME,
       END OF TY_USR02.
DATA: W_CLASS  LIKE LINE OF S_CLASS.
DATA: W_DATE   TYPE USR02-TRDAT.
DATA: WA_ADDRESS TYPE BAPIADSMTP,
      IT_ADDRESS TYPE TABLE OF BAPIADSMTP,
      WA_RETUNR TYPE BAPIRET2,
      IT_RETURN TYPE TABLE OF BAPIRET2 With header line,
      IT_RETURN1 TYPE TABLE OF BAPIRET2 With header line,
      IT_RET2 TYPE TABLE OF BAPIRET2 With header line.
TYPES: BEGIN OF TY_EMAIL,
      USERNAME TYPE USR02-BNAME,
      AD_SMTPADR TYPE BAPIADSMTP-E_MAIL,
      END OF TY_EMAIL.
DATA: WA_EMAIL TYPE TY_EMAIL,
      IT_EMAIL TYPE TABLE OF TY_EMAIL.
*for sending mail details.
DATA:IT_MAIL_CONTENT TYPE STANDARD TABLE OF SOLISTI1,
     WA_IT_MAIL_CONTENT TYPE SOLISTI1,
     WA_IT_DOC_DATA TYPE SODOCCHGI1,
     IT_RECEIVER  TYPE STANDARD TABLE OF SOMLRECI1,
     WA_IT_RECEIVER TYPE SOMLRECI1.
*types: begin of w_usr02_ty,
*          bname type usr02-bname,
*          class type usr02-class,
*          ustyp type usr02-ustyp,
*          uflag TYPE usr02-uflag,
*          aname type usr02-aname,
*          erdat type usr02-erdat,
*          gltgb type usr02-gltgb,
*          trdat type usr02-trdat,
*          ltime type usr02-ltime,
*          bcda1 type usr02-bcda1,
*       end of w_usr02_ty.

*data: t_usr02    type table of w_usr02_ty.
DATA: T_USR02    TYPE TABLE OF USR02 WITH HEADER LINE.
DATA: T_FCAT     TYPE          SLIS_T_FIELDCAT_ALV,
      W_FCAT_DS  TYPE          SLIS_FIELDCAT_ALV.
DATA: WA_USR02 LIKE LINE OF T_USR02.
*      IT_USR02 TYPE TABLE OF TY_USR02.
*       Batchinputdata of single transaction
DATA:   BDCDATA LIKE BDCDATA    OCCURS 0 WITH HEADER LINE.
*       Internal Table for Collecting Messages after Call transaction
DATA: BEGIN OF MESSTAB OCCURS 0.
        INCLUDE STRUCTURE BDCMSGCOLL.
DATA: END OF MESSTAB.
*-------------------------------------------------------------------*
START-OF-SELECTION.

* 3. User groups - SUPER, ADMIN, should be excluded.
  PERFORM EXCLUDE_GROUPS.
  W_DATE = SY-DATUM - 10.

  S_TEMP-LOW = 'T_*'.
  S_TEMP-SIGN = 'I'.
  S_TEMP-OPTION = 'CP'.
  APPEND S_TEMP.
  CLEAR S_TEMP.

  S_TEMP-LOW = 'S_*'.
  S_TEMP-SIGN = 'I'.
  S_TEMP-OPTION = 'CP'.
  APPEND S_TEMP.
  CLEAR S_TEMP.

  S_TEMP-LOW = 'ST_*'.
  S_TEMP-SIGN = 'I'.
  S_TEMP-OPTION = 'CP'.
  APPEND S_TEMP.
  CLEAR S_TEMP.

  S_TEMP-LOW = 'BU_*'.
  S_TEMP-SIGN = 'I'.
  S_TEMP-OPTION = 'CP'.
  APPEND S_TEMP.
  CLEAR S_TEMP.

  S_TEMP-LOW = '09*'.
  S_TEMP-SIGN = 'I'.
  S_TEMP-OPTION = 'CP'.
  APPEND S_TEMP.
  CLEAR S_TEMP.

  SELECT *
      FROM USR02
      INTO TABLE T_USR02
     WHERE  USTYP = C_TYPDIA " * 2. Only Dialog (User type A) should be considered.
       AND BNAME NOT IN S_TEMP
       AND GLTGB LE  W_DATE
       AND CLASS IN S_CLASS
       AND UFLAG EQ 64.
  IF SY-SUBRC NE 0.
    MESSAGE 'No users found' TYPE 'I'.
  ELSE.
  ENDIF.

  PERFORM BUILD_FIELDCAT.

* Display the selected users in pop-up window (in foreground)
  CALL FUNCTION 'REUSE_ALV_POPUP_TO_SELECT'
    EXPORTING
      I_TITLE               = 'Users'
      I_SELECTION           = ' '
      I_ZEBRA               = 'X'
      I_SCREEN_START_COLUMN = 10
      I_SCREEN_START_LINE   = 2
      I_SCREEN_END_COLUMN   = 130
      I_SCREEN_END_LINE     = 20
      I_TABNAME             = 'T_USR02'
      IT_FIELDCAT           = T_FCAT
    TABLES
      T_OUTTAB              = T_USR02
    EXCEPTIONS
      PROGRAM_ERROR         = 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.

*&--------------------------------------------------------------------*
*&      Form  exclude_groups
*&--------------------------------------------------------------------*
*       text
*---------------------------------------------------------------------*
FORM EXCLUDE_GROUPS.

  CLEAR W_CLASS.
  W_CLASS-SIGN    = 'E'.
  W_CLASS-OPTION  = 'EQ'.
  W_CLASS-LOW     = 'SUPER'.
  APPEND W_CLASS TO S_CLASS.

  CLEAR W_CLASS.
  W_CLASS-SIGN    = 'E'.
  W_CLASS-OPTION  = 'EQ'.
  W_CLASS-LOW     = 'ADMIN'.
  APPEND W_CLASS TO S_CLASS.

  CLEAR W_CLASS.
  W_CLASS-SIGN    = 'E'.
  W_CLASS-OPTION  = 'EQ'.
  W_CLASS-LOW     = 'SYSTEM'.
  APPEND W_CLASS TO S_CLASS.

  CLEAR W_CLASS.
  W_CLASS-SIGN    = 'E'.
  W_CLASS-OPTION  = 'EQ'.
  W_CLASS-LOW     = 'COMMUNICATIO'.
  APPEND W_CLASS TO S_CLASS.

ENDFORM.                    "exclude_groups

*&--------------------------------------------------------------------*
*&      Form  build_fieldcat
*&--------------------------------------------------------------------*
*       text
*---------------------------------------------------------------------*
FORM BUILD_FIELDCAT.

  DATA: W_COL_POS TYPE SYST-CUCOL.
  REFRESH T_FCAT.
  CLEAR W_FCAT_DS.

  W_FCAT_DS-FIELDNAME = 'BNAME'.
  W_FCAT_DS-TABNAME   = 'T_USR02'.
  ADD 1 TO W_COL_POS.
  W_FCAT_DS-COL_POS   = W_COL_POS.
  W_FCAT_DS-SELTEXT_M = 'User'.
  APPEND W_FCAT_DS TO T_FCAT.
  CLEAR W_FCAT_DS.

  W_FCAT_DS-FIELDNAME = 'CLASS'.
  W_FCAT_DS-TABNAME   = 'T_USR02'.
  ADD 1 TO W_COL_POS.
  W_FCAT_DS-COL_POS   = W_COL_POS.
  W_FCAT_DS-SELTEXT_M = 'Group'.
  APPEND W_FCAT_DS TO T_FCAT.
  CLEAR W_FCAT_DS.

  W_FCAT_DS-FIELDNAME = 'USTYP'.
  W_FCAT_DS-TABNAME   = 'T_USR02'.
  ADD 1 TO W_COL_POS.
  W_FCAT_DS-COL_POS   = W_COL_POS.
  W_FCAT_DS-SELTEXT_M = 'Type'.
  W_FCAT_DS-OUTPUTLEN = 1.
  APPEND W_FCAT_DS TO T_FCAT.
  CLEAR W_FCAT_DS.

  W_FCAT_DS-FIELDNAME = 'ANAME'.
  W_FCAT_DS-TABNAME   = 'T_USR02'.
  ADD 1 TO W_COL_POS.
  W_FCAT_DS-COL_POS   = W_COL_POS.
  W_FCAT_DS-SELTEXT_M = 'Created By'.
  APPEND W_FCAT_DS TO T_FCAT.
  CLEAR W_FCAT_DS.

  W_FCAT_DS-FIELDNAME = 'ERDAT'.
  W_FCAT_DS-TABNAME   = 'T_USR02'.
  ADD 1 TO W_COL_POS.
  W_FCAT_DS-COL_POS   = W_COL_POS.
  W_FCAT_DS-SELTEXT_M = 'Created On'.
  APPEND W_FCAT_DS TO T_FCAT.
  CLEAR W_FCAT_DS.

  W_FCAT_DS-FIELDNAME = 'GLTGB'.
  W_FCAT_DS-TABNAME   = 'T_USR02'.
  ADD 1 TO W_COL_POS.
  W_FCAT_DS-COL_POS   = W_COL_POS.
  W_FCAT_DS-SELTEXT_M = 'Valid'.
  APPEND W_FCAT_DS TO T_FCAT.
  CLEAR W_FCAT_DS.

  W_FCAT_DS-FIELDNAME = 'TRDAT'.
  W_FCAT_DS-TABNAME   = 'T_USR02'.
  ADD 1 TO W_COL_POS.
  W_FCAT_DS-COL_POS   = W_COL_POS.
  W_FCAT_DS-SELTEXT_M = 'Last logon Date'.
  APPEND W_FCAT_DS TO T_FCAT.
  CLEAR W_FCAT_DS.

  W_FCAT_DS-FIELDNAME = 'LTIME'.
  W_FCAT_DS-TABNAME   = 'T_USR02'.
  ADD 1 TO W_COL_POS.
  W_FCAT_DS-COL_POS   = W_COL_POS.
  W_FCAT_DS-SELTEXT_M = 'Last logon Time'.
  APPEND W_FCAT_DS TO T_FCAT.
  CLEAR W_FCAT_DS.

  W_FCAT_DS-FIELDNAME = 'BCDA1'.
  W_FCAT_DS-TABNAME   = 'T_USR02'.
  ADD 1 TO W_COL_POS.
  W_FCAT_DS-COL_POS   = W_COL_POS.
  W_FCAT_DS-SELTEXT_M = 'User locked'.
  APPEND W_FCAT_DS TO T_FCAT.
  CLEAR W_FCAT_DS.
  IF SY-SUBRC <> 0.
    EXIT.
  ELSE.
    LOOP AT T_USR02 INTO WA_USR02.
      REFRESH: IT_RETURN,IT_ADDRESS.
      CALL FUNCTION 'BAPI_USER_GET_DETAIL'
        EXPORTING
          USERNAME             = WA_USR02-BNAME
*     IMPORTING
*       LOGONDATA            =
*       DEFAULTS             =
*       ADDRESS              =
*       COMPANY              =
*       SNC                  =
*       REF_USER             =
*       ALIAS                =
*       UCLASS               =
*       LASTMODIFIED         =
*       ISLOCKED             =
        TABLES
*       PARAMETER            =
*       PROFILES             =
*       ACTIVITYGROUPS       =
          RETURN               = IT_RETURN
*       ADDTEL               =
*       ADDFAX               =
*       ADDTTX               =
*       ADDTLX               =
         ADDSMTP              = IT_ADDRESS
*       ADDRML               =
*       ADDX400              =
*       ADDRFC               =
*       ADDPRT               =
*       ADDSSF               =
*       ADDURI               =
*       ADDPAG               =
*       ADDCOMREM            =
*       PARAMETER1           =
*       GROUPS               =
*       UCLASSSYS            =
*       EXTIDHEAD            =
*       EXTIDPART            =
*       SYSTEMS              =
                .
      READ TABLE IT_ADDRESS INDEX 1 INTO WA_ADDRESS.
      WA_EMAIL-USERNAME = WA_USR02-BNAME.
      WA_EMAIL-AD_SMTPADR = WA_ADDRESS-E_MAIL.
      APPEND WA_EMAIL TO IT_EMAIL.
    ENDLOOP.
    REFRESH IT_MAIL_CONTENT.
*sending e-mail about id deletion to concerned users.
    LOOP AT IT_EMAIL INTO WA_EMAIL.
      READ TABLE T_USR02 WITH KEY BNAME = WA_EMAIL-USERNAME INTO WA_USR02.

      CONCATENATE 'Your SAP user'     WA_USR02-BNAME     'was deleted due to not logged in for more than 90 days and non-usage of SAP ID, please get in touch with  your Immediate Manager' INTO
  WA_IT_MAIL_CONTENT-LINE SEPARATED BY SPACE.
      APPEND WA_IT_MAIL_CONTENT TO IT_MAIL_CONTENT.
      CLEAR  IT_RECEIVER.
      MOVE: WA_EMAIL-AD_SMTPADR TO WA_IT_RECEIVER-RECEIVER,
              'X'    TO WA_IT_RECEIVER-EXPRESS,
              'U'    TO WA_IT_RECEIVER-REC_TYPE,
              'INT'  TO WA_IT_RECEIVER-COM_TYPE.
      APPEND WA_IT_RECEIVER TO IT_RECEIVER.
      MOVE 'Your SAP user ID was deleted' TO WA_IT_DOC_DATA-OBJ_DESCR.

      CALL FUNCTION 'SO_NEW_DOCUMENT_SEND_API1'
        EXPORTING
          DOCUMENT_DATA              = WA_IT_DOC_DATA
          PUT_IN_OUTBOX              = 'X'            "X
          COMMIT_WORK                = 'X'
        TABLES
          OBJECT_CONTENT             = IT_MAIL_CONTENT
          RECEIVERS                  = IT_RECEIVER
        EXCEPTIONS
          TOO_MANY_RECEIVERS         = 1
          DOCUMENT_NOT_SENT          = 2
          DOCUMENT_TYPE_NOT_EXIST    = 3
          OPERATION_NO_AUTHORIZATION = 4
          PARAMETER_ERROR            = 5
          X_ERROR                    = 6
          ENQUEUE_ERROR              = 7
          OTHERS                     = 8.
    ENDLOOP.

    PERFORM U_LOCK.
  ENDIF.

ENDFORM.                    "build_fieldcat
*&---------------------------------------------------------------------*
*&      Form  U_LOCK
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM U_LOCK .
*  DELETE T_USR02 WHERE TRDAT IS INITIAL.

  LOOP AT T_USR02.

*    T_USR02-UFLAG = 64.
*    T_USR02-GLTGB = SY-DATUM.
*
*    MODIFY T_USR02.

***    PERFORM BDC_DYNPRO      USING  'SAPLSUU5'               '0050'.
***    PERFORM BDC_FIELD       USING 'BDC_CURSOR'              'USR02-BNAME'.
***    PERFORM BDC_FIELD       USING 'BDC_OKCODE'              '=DELE'.
***    PERFORM BDC_FIELD       USING 'USR02-BNAME'             T_USR02-BNAME.
***
***    PERFORM BDC_DYNPRO      USING 'SAPLSPO1'                '0300'.
***    PERFORM BDC_FIELD       USING 'BDC_OKCODE'              '=YES'.
***
***
***    REFRESH MESSTAB.
***    CALL TRANSACTION 'SU01' USING BDCDATA
***                     MODE   CTUMODE
***                     UPDATE 'S'
***                     MESSAGES INTO MESSTAB.
***    CLEAR BDCDATA.
***    REFRESH BDCDATA.

CALL FUNCTION 'BAPI_USER_DELETE'
  EXPORTING
    USERNAME       = T_USR02-BNAME
  TABLES
    RETURN         = it_return1
          .
Read table it_return1 with key type = 'E'.
if sy-subrc ne 0.
clear:it_ret2[].
CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'
* EXPORTING
*   WAIT          =
 IMPORTING
   RETURN        = it_ret2
          .
endif.
ENDLOOP.

ENDFORM.                    " U_LOCK

************************************************************************
*        Start new screen                                              *
*----------------------------------------------------------------------*
***FORM BDC_DYNPRO USING PROGRAM DYNPRO.
***  CLEAR BDCDATA.
***  BDCDATA-PROGRAM  = PROGRAM.
***  BDCDATA-DYNPRO   = DYNPRO.
***  BDCDATA-DYNBEGIN = 'X'.
***  APPEND BDCDATA.
***ENDFORM.                    "bdc_dynpro

*----------------------------------------------------------------------*
*        Insert field                                                  *
*----------------------------------------------------------------------*
***FORM BDC_FIELD USING FNAM FVAL.
***  IF FVAL <> '0'.
***    CLEAR BDCDATA.
***    BDCDATA-FNAM = FNAM.
***    BDCDATA-FVAL = FVAL.
***    APPEND BDCDATA.
***  ENDIF.
***ENDFORM.                    "BDC_FIELD

  • No labels