Tuesday, May 6, 2008

Sample ABAP Program for Output file to application server then send mail with Download details

*&---------------------------------------------------------------------*
*& Form download_to_application
*& download to application server, attach to mail and send to user
*&---------------------------------------------------------------------*
FORM download_to_application.

data: l_title type SO_OBJ_DES.

l_title = sy-repid.

CALL FUNCTION 'ZSEND_REPORT_MAIL'
EXPORTING
i_title = l_title
tables
it_text_data = it_download
EXCEPTIONS
INVALID_USER = 1
MAIL_SEND_ERROR = 2
OPEN_FILE = 3
FILE_GET_NAME = 4
OTHERS = 5
.
IF sy-subrc <> 0.
MESSAGE e368(00) with 'ZSEND_REPORT_MAIL fail -'
sy-subrc.
ENDIF.

ENDFORM. " download_to_application
===============================================================================

FUNCTION Zsend_report_mail.
*"----------------------------------------------------------------------
*"*"Local interface:
*" IMPORTING
*" REFERENCE(I_UNAME) TYPE SYUNAME DEFAULT SY-UNAME
*" REFERENCE(I_TITLE) TYPE SO_OBJ_DES
*" TABLES
*" IT_TEXT_DATA
*" EXCEPTIONS
*" INVALID_USER
*" MAIL_SEND_ERROR
*" OPEN_FILE
*" FILE_GET_NAME
*"----------------------------------------------------------------------

* This function is used to store a file (report result) on the file
* system of the application server and to send an "active" SAP mail
* to the user.
* The "active" SAP mail calls function ZMAIL_DOWNLOAD to
* download the file to the presentation server.

DATA: ls_document_data LIKE sodocchgi1.
DATA: lt_object_para LIKE soparai1 OCCURS 5 WITH HEADER LINE.
DATA: lt_object_parb LIKE soparbi1 OCCURS 0 WITH HEADER LINE.
DATA: lt_object_cont LIKE solisti1 OCCURS 0 WITH HEADER LINE.
DATA: lt_reclist LIKE somlreci1 OCCURS 5 WITH HEADER LINE.

DATA: l_filename(200) TYPE c.

CHECK NOT it_text_data[] IS INITIAL.

* terminate if name not suitable.
IF i_uname IS INITIAL OR
i_uname = 'WF-BATCH' OR
i_uname = 'DDIC' OR
i_uname = 'SAP*'.
RAISE invalid_user.
ENDIF.

* get physical file from logical filename( optional)

CALL FUNCTION 'FILE_GET_NAME'
EXPORTING
logical_filename = 'MAILFILE'
parameter_1 = sy-uname
parameter_2 = sy-datum
parameter_3 = sy-uzeit
* USE_PRESENTATION_SERVER = ' '
* WITH_FILE_EXTENSION = ' '
* USE_BUFFER = ' '
* ELEMINATE_BLANKS = 'X'
IMPORTING
* EMERGENCY_FLAG =
* FILE_FORMAT =
file_name = l_filename
EXCEPTIONS
file_not_found = 1
OTHERS = 2.
IF sy-subrc NE 0.
RAISE file_get_name.
ENDIF.

* save file
OPEN DATASET l_filename FOR OUTPUT IN TEXT MODE.
IF sy-subrc NE 0.
RAISE open_file.
ENDIF.
LOOP AT it_text_data. " into l_text_data.
TRANSFER it_text_data TO l_filename.
ENDLOOP.
CLOSE DATASET l_filename.

* SAP mail header (execute function)
CLEAR: ls_document_data.
ls_document_data-obj_descr = i_title.
ls_document_data-proc_type = 'F'. " function call
ls_document_data-proc_name = 'ZMAIL_DOWNLOAD'.
ls_document_data-no_change = 'X'.
* SAP mail receiver
REFRESH lt_reclist.
CLEAR lt_reclist.
lt_reclist-receiver = i_uname.
lt_reclist-rec_type = 'B'.
* gt_reclist-express = 'X'.
APPEND lt_reclist.
* message text
REFRESH lt_object_cont.
CLEAR lt_object_cont.
lt_object_cont-line =
'The result of the following report has been saved.'.
APPEND lt_object_cont.
* report name
lt_object_cont-line = 'Report:'.
lt_object_cont-line+15 = sy-cprog.
CONDENSE lt_object_cont-line.
APPEND lt_object_cont.
* date & time
lt_object_cont-line = 'Date/Time:'.
WRITE sy-datum TO lt_object_cont-line+15.
WRITE sy-uzeit TO lt_object_cont-line+27.
CONDENSE lt_object_cont-line.
APPEND lt_object_cont.
*
lt_object_cont-line =
'Please execute (Ctrl-F6) this mail to download the result.'.
APPEND lt_object_cont.

* mail parameters
REFRESH lt_object_parb.
CLEAR lt_object_parb.
lt_object_parb-name = 'FUNCTION'. " mail identifier
lt_object_parb-value = 'FILE_DOWNLOAD'. " mail identifier
APPEND lt_object_parb.
lt_object_parb-name = 'FILENAME'.
lt_object_parb-value = l_filename.
APPEND lt_object_parb.

*call SAPOffice API
CALL FUNCTION 'SO_NEW_DOCUMENT_SEND_API1'
EXPORTING
document_data = ls_document_data
TABLES
object_content = lt_object_cont
object_para = lt_object_para
object_parb = lt_object_parb
receivers = lt_reclist
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.
IF sy-subrc NE 0.
RAISE mail_send_error.
ENDIF.

ENDFUNCTION.
===========================================================================
FUNCTION zmail_download.
*"----------------------------------------------------------------------
*"*"Local interface:
*" TABLES
*" MSGDIAL STRUCTURE SOPARBI1
*"----------------------------------------------------------------------
* This function is called in a SAP mail to download a file from the
* application server file system.
* Function ZSEND_REPORT_MAIL is used to save report result
* on application server file system and to send SAP mail to user.
* Based on UK COM solution by Damian Norton.

DATA: ls_msgdial TYPE soparbi1.
DATA: l_filename TYPE filep.
* DATA: l_filename_local TYPE filep.
DATA: l_operation(30) TYPE c.
DATA: BEGIN OF lt_text_data OCCURS 10,
line(2000),
END OF lt_text_data.

* read parameters
LOOP AT msgdial INTO ls_msgdial.
CASE ls_msgdial-name.
WHEN 'FUNCTION'.
l_operation = ls_msgdial-value.
WHEN 'FILENAME'.
l_filename = ls_msgdial-value.
WHEN OTHERS.
MESSAGE e368(00) WITH 'Invalid parameter' ls_msgdial-name.
ENDCASE. " ls_msgdial-name
ENDLOOP. " msgdial

IF l_operation = 'FILE_DOWNLOAD'.
* check, whether file exists on presentation server
REFRESH lt_text_data.
OPEN DATASET l_filename FOR INPUT IN TEXT MODE.
IF sy-subrc = 0.
DO.
READ DATASET l_filename INTO lt_text_data-line.
IF sy-subrc = 0.
APPEND lt_text_data.
ELSE.
EXIT.
ENDIF.
ENDDO.
CLOSE DATASET l_filename.
* request filename on presentation server - or GUI_DOWNLOAD??
CALL FUNCTION 'DOWNLOAD'
TABLES
data_tab = lt_text_data
EXCEPTIONS
invalid_filesize = 1
invalid_table_width = 2
invalid_type = 3
no_batch = 4
unknown_error = 5
gui_refuse_filetransfer = 6
customer_error = 7
OTHERS = 8.
IF sy-subrc NE 0.
MESSAGE e688(00) WITH 'File download error' sy-subrc.
ENDIF.
ELSE.
MESSAGE e398(00) WITH 'File open error' l_filename.
ENDIF. " sy-subrc = 0 (OPEN DATASET)
ENDIF. " l_operation = 'FILE_DOWNLOAD'

ENDFUNCTION.

No comments:

Blog Archive