Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
338 views

Automailer Program For Sending Output of SAP TCodes in Background

This program sends output from SAP transactions (TCodes) automatically via email on a scheduled basis. It takes a TCode and variant as input, runs the report to collect the data, exports it to an Excel file, zips the file, and emails it along with a message body to specified recipients. The user simply needs to schedule the program to run automatically so reports are emailed periodically without additional user intervention.

Uploaded by

Emil S
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
338 views

Automailer Program For Sending Output of SAP TCodes in Background

This program sends output from SAP transactions (TCodes) automatically via email on a scheduled basis. It takes a TCode and variant as input, runs the report to collect the data, exports it to an Excel file, zips the file, and emails it along with a message body to specified recipients. The user simply needs to schedule the program to run automatically so reports are emailed periodically without additional user intervention.

Uploaded by

Emil S
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

Automailer program for sending output of SAP

TCodes in Background
There has been requirement from users to get output of some report(s), mailed to them in excel
format(so that they can adjust/formulate the data as they want). And mostly such reports have fixed
periodicity, such as weekly, fortnight or monthly.

The following program performs similar functionality by taking following steps of exection:

1.  It takes Transaction code and its variant as input. Also the recipients can be added on the input
screen.

2. Collect the data, export it to excel format, zipped and send to the specified recipients.

The user just need to schedule this program in background, such that it can be scheduled for auto-
mailing.

Note: The following code provide the basic layout of the program, it need to be adjusted/modified as
per your need.

*&---------------------------------------------------------------------*
*& Report ZSD_AUTOMAIL
*&
*&---------------------------------------------------------------------*
*&
*& PURPOSE - TO SEND AUTOMAIL TO DEFINED RECEIPENTS
*&---------------------------------------------------------------------*

REPORT ZSD_AUTOMAIL.
TABLES: ADR6.

*** Data declaration

TYPE-POOLS : ABAP, SLIS.

TYPES: BEGIN OF X_TEXT,


LINE(2500) TYPE C,
END OF X_TEXT.
*
**Internal Tables
DATA : IT_LIST TYPE STANDARD TABLE OF ABAPLIST,
IT_TEXT TYPE STANDARD TABLE OF X_TEXT,
IT_TXT TYPE STANDARD TABLE OF X_TEXT,
WA_TEXT TYPE X_TEXT,
IT_OUTTAB TYPE REF TO DATA,
WA_OUTTAB TYPE REF TO DATA.

** Variables
DATA : GV_FIELDCNT TYPE I,
GV_STR TYPE STRING,
GV_STRMSG TYPE STRING,
GV_XSTRMSG TYPE XSTRING.

DATA: L_PRGNM TYPE BTCPROG.

** CONSTANTS
CONSTANTS:
CO_TAB TYPE C VALUE CL_ABAP_CHAR_UTILITIES=>HORIZONTAL_TAB,
CO_CRET TYPE C VALUE CL_ABAP_CHAR_UTILITIES=>CR_LF.

** Selection screen
SELECTION-SCREEN : BEGIN OF BLOCK B WITH FRAME TITLE TEXT-001.

****PARAMETERS : L_PRGNM TYPE BTCPROG OBLIGATORY


**** DEFAULT 'AQZZZSALESREG-1=SALESREG-1====', "Program name
PARAMETERS : P_TCODE TYPE TSTC-TCODE OBLIGATORY,
P_VARINT TYPE BTCVARIANT OBLIGATORY.
PARAMETERS: P_RECP TYPE AD_SMTPADR OBLIGATORY.

SELECT-OPTIONS: SO_RECP FOR ADR6-SMTP_ADDR NO INTERVALS.

SELECTION-SCREEN : END OF BLOCK B.

*** Initialization
INITIALIZATION.

*** start of selection block


START-OF-SELECTION.

SELECT SINGLE PGMNA INTO L_PRGNM FROM TSTC WHERE TCODE = P_TCODE.
IF SY-SUBRC <> ''.
MESSAGE 'Please provide correct Transaction Code.' TYPE 'E'.
ENDIF.

PERFORM GET_REPORT_OUTPUT.
PERFORM EXCEL_ZIP_SEND.

*----------------------------------------------------------------------*
***INCLUDE ZSD_SALES_REGISTER_ROUTINES .
*----------------------------------------------------------------------*
*&---------------------------------------------------------------------*
*& Form GET_REPORT_OUTPUT
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
FORM GET_REPORT_OUTPUT .

DATA : ITAB TYPE TABLE OF STRING,


LV_ITAV TYPE STRING.
CONSTANTS:
CO_SEPARATER TYPE C VALUE CL_ABAP_CHAR_UTILITIES=>HORIZONTAL_TAB,
CO_NEWLINE TYPE C VALUE CL_ABAP_CHAR_UTILITIES=>CR_LF.

REFRESH : IT_TEXT, IT_TXT.


CLEAR : WA_TEXT.

** 1. Execute the report and write ALV output to memory


SUBMIT (L_PRGNM) USING SELECTION-SET P_VARINT
EXPORTING LIST TO MEMORY AND RETURN.
IF SY-SUBRC <> 0.
MESSAGE TEXT-002 TYPE 'E'.
ENDIF.

** 2. Get ALV output from memory to internal table


CALL FUNCTION 'LIST_FROM_MEMORY'
TABLES
LISTOBJECT = IT_LIST
EXCEPTIONS
NOT_FOUND = 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.

*** 3. convert output to text


CALL FUNCTION 'LIST_TO_ASCI'
EXPORTING
LIST_INDEX = -1
TABLES
LISTASCI = IT_TEXT
LISTOBJECT = IT_LIST
EXCEPTIONS
EMPTY_LIST = 1
LIST_INDEX_INVALID = 2
OTHERS = 3.
IF SY-SUBRC <> 0.
* MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
* WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
ENDIF.

*** 4. all data to string variable


CLEAR GV_STRMSG.
LOOP AT IT_TEXT INTO WA_TEXT.

IF WA_TEXT-LINE NP '-*'.
REPLACE ALL OCCURRENCES OF '|' IN WA_TEXT WITH CO_SEPARATER.
CONCATENATE GV_STRMSG WA_TEXT CO_NEWLINE INTO GV_STRMSG.
ENDIF.
ENDLOOP.
REFRESH IT_TEXT.

" GV_STRMSG holds all data to be sent in excel.

ENDFORM. " GET_REPORT_OUTPUT


*&---------------------------------------------------------------------*
*& Form EXCEL_ZIP_SEND
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
FORM EXCEL_ZIP_SEND .
** data declaration
"Object
DATA : LV_FILE TYPE STRING,
LV_ZIPFILEHEX TYPE XSTRING,
LV_BDYMSG TYPE STRING,
LV_BDYLEN TYPE I,
LV_BDYLENG TYPE SO_OBJ_LEN,
GO_ZIPPER TYPE REF TO CL_ABAP_ZIP,
IT_BIN_CON TYPE SOLIX_TAB,
IT_BDYMSG TYPE BCSY_TEXT,
SEND_REQUEST TYPE REF TO CL_BCS,
DOCUMENT TYPE REF TO CL_DOCUMENT_BCS,
LV_SUBJECT TYPE SO_OBJ_DES,
SENT_TO_ALL TYPE OS_BOOLEAN,
RECIPIENT TYPE REF TO IF_RECIPIENT_BCS.

*** 1. convert data from string to xstring


CLEAR GV_XSTRMSG.
CALL FUNCTION 'SCMS_STRING_TO_XSTRING'
EXPORTING
TEXT = GV_STRMSG
IMPORTING
BUFFER = GV_XSTRMSG.
.

*** 2. Zip the file - itbin_con


DATA: L_DATUM TYPE SY-DATUM.
CALL FUNCTION 'CONVERT_DATE_FORMAT'
EXPORTING
I_DATE = SY-DATUM
IMPORTING
E_CALC_DATE = L_DATUM.

CONCATENATE P_VARINT '-' L_DATUM '-' SY-UNAME '.xls' INTO LV_FILE.


" add file to zip
CREATE OBJECT GO_ZIPPER.
GO_ZIPPER->ADD( NAME = LV_FILE
CONTENT = GV_XSTRMSG ).
"
LV_ZIPFILEHEX = GO_ZIPPER->SAVE( ).

REFRESH IT_BIN_CON.

"convert file to binary


CALL FUNCTION 'SCMS_XSTRING_TO_BINARY'
EXPORTING
BUFFER = LV_ZIPFILEHEX
TABLES
BINARY_TAB = IT_BIN_CON.
** 3. Body of the email - IT_BDYMSG

CLEAR : LV_BDYMSG .

DATA: L_CDATE(10) TYPE C,


L_PDATE(10) TYPE C,
L_BODY TYPE STRING.

DATA: L_VALUETAB TYPE RSPARAMS OCCURS 0 WITH HEADER LINE.

*** for Tcode ZDSPG - the Subject


IF P_TCODE = 'ZDSPG'.

CALL FUNCTION 'RS_VARIANT_CONTENTS'


EXPORTING
REPORT = 'ZDLYDSPHNEW'
VARIANT = P_VARINT
MOVE_OR_WRITE = 'W'
TABLES
VALUTAB = L_VALUETAB
EXCEPTIONS
VARIANT_NON_EXISTENT = 1
VARIANT_OBSOLETE = 2
OTHERS = 3.
IF SY-SUBRC <> 0.
MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
ENDIF.

READ TABLE L_VALUETAB WITH KEY SELNAME = 'S_FKDAT'.


IF SY-SUBRC = ''.
L_PDATE = L_VALUETAB-LOW.
IF L_VALUETAB-HIGH <> '00.00.0000'.
L_CDATE = L_VALUETAB-HIGH.
ELSE.
L_CDATE = L_VALUETAB-LOW.
ENDIF.
ENDIF.

CONCATENATE 'Billing Information From' L_PDATE 'To' L_CDATE INTO L_BODY SEPARATED
BY ' '.
LV_BDYMSG = L_BODY.

*****for Tcode ZMBPN - the Subject


ELSEIF P_TCODE = 'ZMBPN'.

CALL FUNCTION 'RS_VARIANT_CONTENTS'


EXPORTING
REPORT = 'ZMANBPAY_NEW'
VARIANT = P_VARINT
MOVE_OR_WRITE = 'W'
TABLES
VALUTAB = L_VALUETAB
EXCEPTIONS
VARIANT_NON_EXISTENT = 1
VARIANT_OBSOLETE = 2
OTHERS = 3.
IF SY-SUBRC <> 0.
MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
ENDIF.

READ TABLE L_VALUETAB WITH KEY SELNAME = 'BUDAT'.


IF SY-SUBRC = ''.
L_PDATE = L_VALUETAB-LOW.
IF L_VALUETAB-HIGH <> '00.00.0000'.
L_CDATE = L_VALUETAB-HIGH.
ELSE.
L_CDATE = L_VALUETAB-LOW.
ENDIF.
ENDIF.

CONCATENATE 'Cheque Details From' L_PDATE 'To' L_CDATE INTO L_BODY SEPARATED BY '
'.
LV_BDYMSG = L_BODY.

****for Tcode ZAGE - the Subject


ELSEIF P_TCODE = 'ZAGE'.

CALL FUNCTION 'RS_VARIANT_CONTENTS'


EXPORTING
REPORT = 'ZFAGANLS'
VARIANT = P_VARINT
MOVE_OR_WRITE = 'W'
TABLES
VALUTAB = L_VALUETAB
EXCEPTIONS
VARIANT_NON_EXISTENT = 1
VARIANT_OBSOLETE = 2
OTHERS = 3.
IF SY-SUBRC <> 0.
MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
ENDIF.

READ TABLE L_VALUETAB WITH KEY SELNAME = 'KEY_DATE'.


IF SY-SUBRC = ''.
L_PDATE = L_VALUETAB-LOW.
L_CDATE = L_VALUETAB-HIGH.
ENDIF.

CONCATENATE 'BR Details as on ' L_CDATE INTO L_BODY.


LV_BDYMSG = L_BODY.

ENDIF.

LV_BDYLEN = STRLEN( LV_BDYMSG ) .


LV_BDYLENG = LV_BDYLEN .
CALL FUNCTION 'SCMS_STRING_TO_FTEXT'
EXPORTING
TEXT = LV_BDYMSG
TABLES
FTEXT_TAB = IT_BDYMSG.

** 4. Subject of the email


** LV_SUBJECT = P_VARINT.
LV_SUBJECT = LV_FILE.

** 5. Prepare send document [email]

TRY.
"
CLEAR SEND_REQUEST .
SEND_REQUEST = CL_BCS=>CREATE_PERSISTENT( ).

" create document with email body


CLEAR DOCUMENT .
DOCUMENT = CL_DOCUMENT_BCS=>CREATE_DOCUMENT(
I_TYPE = 'HTM'
I_TEXT = IT_BDYMSG
I_LENGTH = LV_BDYLENG
I_SUBJECT = LV_SUBJECT ).

"add excel-zip file to email as attachment

CALL METHOD DOCUMENT->ADD_ATTACHMENT


EXPORTING
I_ATTACHMENT_TYPE = 'zip'
I_ATTACHMENT_SUBJECT = LV_SUBJECT "Attachment name
I_ATT_CONTENT_HEX = IT_BIN_CON.

"add document to send request


CALL METHOD SEND_REQUEST->SET_DOCUMENT( DOCUMENT ).

"convert receipient to internet format and then add


RECIPIENT = CL_CAM_ADDRESS_BCS=>CREATE_INTERNET_ADDRESS( P_RECP ).
CALL METHOD SEND_REQUEST->ADD_RECIPIENT
EXPORTING
I_RECIPIENT = RECIPIENT
I_EXPRESS = 'X'.

LOOP AT SO_RECP.
"convert receipient to internet format and then add
RECIPIENT = CL_CAM_ADDRESS_BCS=>CREATE_INTERNET_ADDRESS( SO_RECP-LOW ).
CALL METHOD SEND_REQUEST->ADD_RECIPIENT
EXPORTING
I_RECIPIENT = RECIPIENT
I_EXPRESS = 'X'.
ENDLOOP.

" Send mail


CALL METHOD SEND_REQUEST->SET_STATUS_ATTRIBUTES
EXPORTING
I_REQUESTED_STATUS = 'E'
I_STATUS_MAIL = 'E'.

CALL METHOD SEND_REQUEST->SET_SEND_IMMEDIATELY( 'X' ).

CALL METHOD SEND_REQUEST->SEND(


EXPORTING
I_WITH_ERROR_SCREEN = 'X'
RECEIVING
RESULT = SENT_TO_ALL ).
IF SENT_TO_ALL = 'X'.
** MESSAGE TEXT-003 TYPE 'S'.
MESSAGE 'The file has been sent successfully.' TYPE 'S'.
ELSE.
** MESSAGE TEXT-004 TYPE 'E'.
MESSAGE 'Error is generated.' TYPE 'E'.
ENDIF.
COMMIT WORK.
EXIT.
ENDTRY.

ENDFORM. " EXCEL_ZIP_SEND


************************************************************************

You might also like