4

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

 1 year ago
source link: https://blogs.sap.com/2022/10/29/delete-the-user-if-the-user-was-not-logged-in-more-than-360-days-using-bapi_user_delete/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client
October 29, 2022 2 minute read

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

userdelete1.png
userdelete1-1.png

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

userdelete2.png

3.Structure as per your output.

userdelete3.png

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.


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK