Create Flat File Outbound Interface

Scenario: You are developing outbound interfaces for your project and want a consistent Look and Feel (Selection-screen) for all these interfaces making it easy to understand for users and maintain for developers. You are creating an outbound Flat file and your report should have all the functionality like (1) ability to upload file to Application server, which can be checked using TCode AL11, (2) ability to download file to Desktop (local machine), (3) the F4 helps for Application as well as Presentation server assisting the user in selecting File path.

Selection-criteria: A screenshot of the selection-screen which can handle such scenario is shown below. When the Application Server radio-button is selected, field for Presentation server is greyed out (and vice versa) as you can see below :
Figure: File Upload/ Download Report Selection
Sample Code: You can find the complete and functional code below. You can copy and paste after opening transaction code SE38 or SE80. F4 help is also designed for file paths for Application as well as Presentation server. I have used the flight data model to demonstrate it and you can replace this functionality with your business logic. As an example records are fetched from table SCARR (from Flight data model) into an internal table which can then be either uploaded or downloaded and the appropriate messages follow. If the table SCARR is empty, nothing follows, you can create your own messages for this situation.

For F4 help for file path in Application Server, Function module '/SAPDMC/LSM_F4_SERVER_FILE' is used and for creating file on application server, OPEN DATASET statement is used.

For F4 help for file path for Local machine (Desktop), downloading file, methods of standard SAP class CL_GUI_FRONTEND_SERVICES are used. This class offers several functions for front-end. I have discussed some other functions like downloading to a temporary file, printing and deleting (upon completion of printing) from the temporary directory in my blog: Printing DMS Documents.


*&---------------------------------------------------------------------*

*& Report  Z_UPLOAD_DOWNLOAD
*&
*&---------------------------------------------------------------------*
*&
*&
*&---------------------------------------------------------------------*

REPORT  z_upload_download.

* Data declaration for Application server File Dialog/Upload
DATA: v_directory TYPE rfpdo-rfbifile.

* Data declaration for Presentation File Dialog/Download
DATA : it_file  TYPE filetable,  "Local internal table
       wa_file LIKE LINE OF it_file, "Local structure
       v_rc     TYPE i. "Return variable

SELECTION-SCREEN BEGIN OF BLOCK blk WITH FRAME TITLE text-001.
* Presentation Server
PARAMETERS : rb_prs RADIOBUTTON GROUP rb1 USER-COMMAND fcode.  "Radio-button for Presentation server
PARAMETERS : p_lfile TYPE string MODIF ID r2.   "Local File
* Application Server
PARAMETERS : rb_app RADIOBUTTON GROUP rb1. "Radio-button for Application server
PARAMETERS : p_file(25) TYPE c MODIF ID r1.    "File Name
PARAMETERS : p_path TYPE rfpdo-rfbifile MODIF ID r1. "Server Path
SELECTION-SCREEN END OF BLOCK blk.

INITIALIZATION.
  rb_app = 'X'.

AT SELECTION-SCREEN OUTPUT.
* Logic for Greying out the fields for App Server/Presentation Server
  LOOP AT SCREEN.
*   If Application server radio button is selected
    IF rb_app = 'X' AND screen-group1 = 'R2'.
      IF  screen-name = 'P_LFILE'.
        screen-input = '0'.
        screen-output = '0'.
      ENDIF.
    ENDIF.
*   If Presentation server radio button is selected
    IF rb_prs = 'X' AND screen-group1 = 'R1'.
      IF screen-name = 'P_FILE' OR screen-name = 'P_PATH'.
        screen-input = '0'.
        screen-output = '0'.
      ENDIF.
    ENDIF.
    MODIFY SCREEN.
  ENDLOOP.

AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_path.
* This FM is used for F4 of file path in Application Server
  CALL FUNCTION '/SAPDMC/LSM_F4_SERVER_FILE'
    EXPORTING
      directory        = v_directory
    IMPORTING
      serverfile       = v_directory
    EXCEPTIONS
      canceled_by_user = 1
      OTHERS           = 2.
* If data found
  IF sy-subrc EQ 0.
    p_path = v_directory.
  ENDIF.

AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_lfile.
* This is used for F4 of file path in Presentation Server
  CALL METHOD cl_gui_frontend_services=>file_open_dialog
    EXPORTING
      default_extension       = 'TXT'
      initial_directory       = 'C:\'
    CHANGING
      file_table              = it_file
      rc                      = v_rc
    EXCEPTIONS
      file_open_dialog_failed = 1
      cntl_error              = 2
      error_no_gui            = 3
      not_supported_by_gui    = 4
      OTHERS                  = 5.

  IF sy-subrc EQ 0.
    READ TABLE it_file INTO wa_file INDEX 1.
    IF sy-subrc = 0.
      p_lfile = wa_file-filename.
    ENDIF.
  ENDIF.


START-OF-SELECTION.
  DATA : it_scarr TYPE TABLE OF scarr INITIAL SIZE 0,
         wa_scarr TYPE scarr.
  DATA : dset TYPE rfpdo-rfbifile,
         v_temp TYPE string,
         v_msg TYPE string,
         wa_data TYPE string,
         it_data TYPE TABLE OF string.

  wa_scarr-carrid = 'IDG'.
  wa_scarr-carrname = 'Indigo'.
  wa_scarr-currcode = 'INR'.
  wa_scarr-url = 'www.goindigo.in'.
  INSERT INTO scarr VALUES wa_scarr.

*Fetch data from table 'Flight' of Flight data model
  SELECT * FROM scarr INTO TABLE it_scarr.
  IF sy-subrc = 0.
    CLEAR : v_temp, wa_scarr.
    IF rb_app = 'X'.  "If Applicaton server is selected
      CONCATENATE p_path p_file INTO dset SEPARATED BY '/'.
* Open the file
      OPEN DATASET dset FOR OUTPUT IN TEXT MODE ENCODING DEFAULT MESSAGE v_msg.
      IF sy-subrc = 0.
        LOOP AT it_scarr INTO wa_scarr.
          CONCATENATE wa_scarr-carrid wa_scarr-carrname wa_scarr-currcode
                      wa_scarr-url INTO v_temp SEPARATED BY ','.
          TRANSFER v_temp TO dset.
        ENDLOOP.
        CLOSE DATASET dset.
        MESSAGE 'File uploaded successfully !' TYPE 'S'.
      ENDIF.
    ENDIF.
    IF rb_prs = 'X'.  "If Presentation server is selected
      LOOP AT it_scarr INTO wa_scarr.
        CONCATENATE wa_scarr-carrid wa_scarr-carrname wa_scarr-currcode
                    wa_scarr-url INTO wa_data SEPARATED BY ','.
        APPEND wa_data TO it_data.
      ENDLOOP.

* Download file to presentation server
      CALL METHOD cl_gui_frontend_services=>gui_download
        EXPORTING
          filename = p_lfile
        CHANGING
          data_tab = it_data
        EXCEPTIONS
          OTHERS   = 1.
      IF sy-subrc EQ 0.
        MESSAGE 'File downloaded successfully !' TYPE 'S'.
      ENDIF.
    ENDIF.
  ENDIF.

Featured Post

Top 5 Reasons you should be on Google+: [1] Immersive Look-and-Feel

Updated on August 5, 2017  due to remarkable changes in the landscape of Google+ making the post up-to-date and current. Enjoy the updated...