Thursday, July 15, 2021

ABAP Program for Lock All Users

 

REPORT YUSRLOCK MESSAGE-ID Z1 .


TABLES: USR02.
PARAMETERS: LOCK AS CHECKBOX, LISTLOCK AS CHECKBOX.
DATA: UFLAGVAL TYPE I, LOCKSTRING(8) TYPE C.
*————– Authorization check ———————–*
AUTHORITY-CHECK OBJECT ‘ZPROG_RUN’ ID ‘PROGRAM’ FIELD SY-CPROG.
IF SY-SUBRC <> 0.
IF SY-SUBRC = 4.
MESSAGE E000 WITH SY-CPROG. “some message about authorization check failure
ELSE.
MESSAGE E005 WITH SY-SUBRC. “some message about authorization check failure
ENDIF.
ENDIF.
IF LISTLOCK = ‘X’.
WRITE:/ ‘List all locked users: ‘.
SELECT * FROM USR02 WHERE UFLAG = 64.
WRITE: / USR02-BNAME.
ENDSELECT.
EXIT.
ENDIF.
IF LOCK = ‘X’.
UFLAGVAL = 64. “lock all users
LOCKSTRING = ‘locked’.
ELSE.
UFLAGVAL = 0. “unlock all users
LOCKSTRING = ‘unlocked’.
ENDIF.
SELECT * FROM USR02 WHERE BNAME <> ‘SAP*’ AND BNAME <> SY-UNAME.
IF USR02-UFLAG <> 0 AND USR02-UFLAG <> 64.
WRITE: ‘User’, USR02-BNAME, ‘untouched; please handle manually.’.
CONTINUE.
ENDIF.
** check that user has authority to make these changes
AUTHORITY-CHECK OBJECT ‘S_USER_GRP’
ID ‘CLASS’ FIELD USR02-CLASS
ID ‘ACTVT’ FIELD ’05’.
IF SY-SUBRC <> 0.
IF SY-SUBRC = 4.
WRITE: /’You are not authorized to lock/unlock user ‘,
USR02-BNAME, USR02-CLASS.
ELSE.
WRITE: /’Authorization error checking user ‘,
USR02-BNAME, USR02-CLASS, ‘(return code’, SY-SUBRC, ‘).’.
ENDIF.
ELSE. “has authority
UPDATE USR02 SET UFLAG = UFLAGVAL WHERE BNAME = USR02-BNAME.
WRITE: / ‘User’, USR02-BNAME, LOCKSTRING, ‘.’.
ENDIF.

ABAP program to send a report to an external mail-id




REPORT ZREPORT_TO_EMAIL NO STANDARD PAGE HEADING LINE-SIZE 200.

DATA : BEGIN OF ITAB
OCCURS 0,
PERNR LIKE PA0001-PERNR,
ENAME LIKE PA0001-ENAME,
END OF ITAB.

DATA: message_content LIKE soli OCCURS 10 WITH HEADER LINE,
receiver_list LIKE soos1 OCCURS 5 WITH HEADER LINE,
packing_list LIKE soxpl OCCURS 2 WITH HEADER LINE,
listobject LIKE abaplist OCCURS 10,
compressed_attachment LIKE soli OCCURS 100 WITH HEADER LINE,
w_object_hd_change LIKE sood1,
compressed_size LIKE sy-index.

START-OF-SELECTION.

SELECT PERNR ENAME
INTO CORRESPONDING FIELDS OF TABLE ITAB
FROM PA0001
WHERE PERNR < 50.

LOOP AT ITAB.
WRITE :/02 SY-VLINE , ITAB-PERNR, 15 SY-VLINE , ITAB-ENAME, 50
SY-VLINE.
ENDLOOP.
 

* Receivers
receiver_list-recextnam = “
EXTERNAL-MAIL-ID@GMAIL.COM.
“–>
* EMAIL ADDRESS
RECEIVER_list-RECESC = “E”.
RECEIVER_list-SNDART = “INT”.
RECEIVER_list-SNDPRI = “1”.

APPEND receiver_list.

* General data
w_object_hd_change-objla = sy-langu.
w_object_hd_change-objnam = “Object name”.
w_object_hd_change-objsns = “P”.
* Mail subject
w_object_hd_change-objdes = “Message subject”.

* Mail body
APPEND ‘Message content’ TO message_content.

* Attachment
CALL FUNCTION ‘SAVE_LIST’
EXPORTING
list_index = ‘0’
TABLES
listobject = listobject.
CALL FUNCTION ‘TABLE_COMPRESS’
IMPORTING
compressed_size = compressed_size
TABLES
in = listobject
out
= compressed_attachment.
DESCRIBE TABLE compressed_attachment.
CLEAR packing_list.
packing_list-transf_bin = ‘X’.
packing_list-head_start = 0.
packing_list-head_num = 0.
packing_list-body_start = 1.
packing_list-body_num = sy-tfill.
packing_list-objtp = ‘ALI’.
packing_list-objnam = ‘Object name’.
packing_list-objdes = ‘Attachment description’.
packing_list-objlen = compressed_size.
APPEND packing_list.

CALL FUNCTION ‘SO_OBJECT_SEND’
EXPORTING
object_hd_change = w_object_hd_change
object_type = ‘RAW’
owner =
sy-uname
TABLES
objcont = message_content
receivers = receiver_list
packing_list = packing_list
att_cont = compressed_attachment.