Problem solve Get help with specific problems with your technologies, process and projects.

Send output of ABAP query as attachment

Sometimes you may need to send the output of ABAP Queries as mail attachments.

Sometimes you may need to send the output of ABAP Queries as mail attachments. Furthermore, there may be a need...

to send output of already existing report programs as mail attachments. Two options that could be used are (along with its disadvantages) given below: 1 Coding in the ABAP Query -- Easier said than done! 2 Use the Spool List Recipient while defining a back ground job -- Also will not work, as the mail content is not in the form of attachment and that there is a limit to the number of lines (1000, i think) that will only be sent. The other option that could be used is explained below: 1. Create a variant for the query which will be used to run 2. Make sure that the output type of the query is an 'ABAP List' 3. Have a Unix script (we use Chron job) that will accept two files one containing the mail ids of the recipient and the other the data file that will be sent. 4. Write an executable program that accepts the a) Name of the query (PARAMETERS: p_rep LIKE sy-repid OBLIGATORY,) b) Variant to be used (PARAMETERS: p_var LIKE rsvar-variant OBLIGATORY) c) Unix path wher the file will be dropped ( p_dir LIKE rfpdo-rfbifile) d) Mail recipients (SELECT-OPTIONS: s_smtp FOR sad1s-l_smtp_adr NO INTERVALS) 5. Call the query and export the list to memory SUBMIT (p_rep) USING SELECTION-SET p_var USING SELECTION-SETS OF PROGRAM p_rep EXPORTING LIST TO MEMORY AND RETURN. 6. Use the FM 'LIST_FROM_MEMORY' to read the list to internal table itab(DATA itab LIKE abaplist OCCURS 0 WITH HEADER LINE.) CALL FUNCTION 'LIST_FROM_MEMORY' TABLES listobject = itab EXCEPTIONS not_found = 1 OTHERS = 2. 7. Convert the content of the itab to ASCII using FM into internal table itab2 ( DATA: BEGIN OF itab2 OCCURS 10000, text(255) TYPE c, END OF itab2. ) CALL FUNCTION 'LIST_TO_ASCI' TABLES listasci = itab2 listobject = itab EXCEPTIONS empty_list = 1 list_index_invalid = 2 OTHERS = 3. 8. Move the content of itab2 to internal table outtab with end of line character( '0D' in hex) (DATA: BEGIN OF outtab OCCURS 0, text(255) TYPE c , eol TYPE x, END OF outtab.) LOOP AT itab2. MOVE: itab2-text TO outtab-text, '0D' TO outtab-eol. APPEND outtab. CLEAR outtab. ENDLOOP. 9. Drop the Mail ids of the recipients into unix directory CONCATENATE p_dir 'Mailid.DAT' INTO mail_file. OPEN DATASET mail_file FOR OUTPUT IN TEXT MODE. LOOP AT s_smtp WHERE sign EQ 'I' AND option EQ 'EQ'. CLEAR l_mail_id. l_mail_id = s_smtp-low. CONDENSE l_mail_id. TRANSFER l_mail_id TO mail_file. ENDLOOP. CLOSE DATASET mail_file. 10. Drop the data file (outtab) CONCATENATE p_dir 'Data.DAT' INTO data_file. OPEN DATASET data_file FOR OUTPUT IN TEXT MODE. LOOP AT outtab. TRANSFER outtab TO data_file. ENDLOOP. CLOSE DATASET data_file. 11. Once the mail id file and the data file are dropped, the script will pick up the data file and send it as mail attachments to the mail ids. Points to note: # Always check if variant for the called program/query exists using FM 'RS_VARIANT_EXISTS' # If the called program is a query, then ensure that the output type is 'ABAP List'. This can be done by using FM 'RS_VARIANT_VALUES_TECH_DATA' # Reports can also be used here instead of ABAP queries. # This program can be scheduled so that several programs/queries can be run with its variants after making sure that the file names are dynamically created so that they do not overwrite. # If mailing option (PARAMETERS: p_mail AS CHECKBOX) is not required, then the submit can used as follows : IF p_mail = 'X'. SUBMIT (p_rep) USING SELECTION-SET p_var USING SELECTION-SETS OF PROGRAM p_rep EXPORTING LIST TO MEMORY AND RETURN. else. SUBMIT (p_rep) VIA SELECTION-SCREEN USING SELECTION-SET p_var USING SELECTION-SETS OF PROGRAM p_rep AND RETURN. endif. This way, one gets the selection screen of the called program. # Creating this program is one time effort and Once it is is developed, as mentioned, it can be used to add mailing functionality to already existing reports instead of making changes to it.

This was last published in November 2002

Dig Deeper on SAP ABAP

Start the conversation

Send me notifications when other members comment.

Please create a username to comment.