Skip to Content
Technical Articles
Author's profile photo krishna sidda

Delete the user if the user was not logged in more than 360 days using BAPI_USER_DELETE

Delete the user if the user was not logged in more than 360 days using BAPI_USER_DELETE

1..Give the import parameters as number of days and test flag

2.Create the structure as per output pass into the export parameters ET_RETURN.

3.Structure as per your output.

Now click on Source code write the below code

FUNCTION zuser_login_check360.
*"----------------------------------------------------------------------
*"*"Local Interface:
*"  IMPORTING
*"     VALUE(DTRDAT) TYPE  JBNTAGE DEFAULT 360
*"  EXPORTING
*"     REFERENCE(EX_RETURN) TYPE  ZT_USER
*"----------------------------------------------------------------------
* Data declarations
  DATA:  lv_last_logon_date TYPE xuldate,
         lv_days            TYPE vtbbewe-atage,
         ls_date            TYPE bapilogond,
         ls_flag            TYPE bapilogonx,
         lt_return          TYPE bapiret2_t,
         lv_dat             TYPE dats.
*   **--- Getting the last 180 days dates
  lv_dat = sy-datum - 360.
* Based on the last login date fetch the user details
  SELECT bname,trdat FROM usr02
                     INTO TABLE @DATA(lt_user)
                     WHERE  trdat LE @lv_dat.

  IF lt_user IS NOT INITIAL.
*    **--Fetch User details
    SELECT bname, name_first, name_last FROM user_addr
                                        INTO TABLE @DATA(lt_addr)
                                        FOR ALL ENTRIES IN @lt_user
                                        WHERE bname = @lt_user-bname.
    LOOP AT lt_addr ASSIGNING FIELD-SYMBOL(<fs_user>).
**--Update user valid to date
          CALL FUNCTION 'BAPI_USER_DELETE'
            EXPORTING
              username = <fs_user>-bname
            tables
              return   = lt_return.
          IF sy-subrc EQ 0.
            APPEND INITIAL LINE TO ex_return ASSIGNING FIELD-SYMBOL(<ls_return>).
            <ls_return>-username = <fs_user>-bname.
            <ls_return>-firstname = <fs_user>-name_first.
            <ls_return>-lastname = <fs_user>-name_last.
            READ TABLE lt_return INTO DATA(ls_return) WITH KEY type = 'S'.
             <ls_return>-status = 'User' && | | && <fs_user>-bname && | | && 'Deleted' .
          ELSE.
            <ls_return>-username = <fs_user>-bname.
            <ls_return>-firstname = <fs_user>-name_first.
            <ls_return>-lastname = <fs_user>-name_last.
            READ TABLE lt_return INTO ls_return WITH KEY type = 'E'.
            IF sy-subrc = 0.
              <ls_return>-status = ls_return-message .
            ENDIF.
        ENDIF.
    ENDLOOP.
  ENDIF.

ENDFUNCTION.

 

Now execute the function module we will get the deleted users.

NOTE: While testing the code please comment the BAPI_USER_DELETE, Otherwise users would be deleted from the system.

Thank you

Siva sidda.

 

 

 

Assigned Tags

      1 Comment
      You must be Logged on to comment or reply to a post.
      Author's profile photo Michelle Crapo
      Michelle Crapo

      Well this one surprised me.   I thought there would be an SAP out-of-the-box program for deleting users.  I know I had written a delete program for users before but it's been awhile.

      SO - great job!   This will be a useful program.  Of course, my job wants each looked at individually to make sure they aren't running background jobs - that could be automated too.  I honestly don't think they will want to give up that control, but maybe at some point.

      It will be interesting to see any more comments.   There might be something out there; I did see a lot of options, but nothing that was automatic.