Wednesday, January 22, 2020

Downloading a file data from FTP Server to SAP ECC internal table

Downloading a file data from FTP Server to SAP  internal table

SAP provides example reports for connecting to FTP using the function module above. Following report will give you comprehensive example:
  • RSFTP002: Execute FTP Command
  • RSFTP007: Example use of FTP_R3_TO_SERVER and FTP_SERVER_TO_R3


Code : Simply copy and paste the code and run ,if you want to display the record then use alv to display the result in alv re_use_alv display

 

*&---------------------------------------------------------------------*
*& Report  ZHR_ATTENDANCE_UPLOAD_FTP
*&
*&---------------------------------------------------------------------*
*& date : 22/1/2020   technical : Sourav Rai  functional --------
*& Employee attendance upload from FTP for entire group of Companies Emp
*&---------------------------------------------------------------------*
REPORT zhr_attendance_upload_ftp_sr.

************************************************************************
*T Y P E S
************************************************************************
TYPESBEGIN OF stru_ftpdata,
         data1 TYPE char200,
       END OF stru_ftpdata.
TYPES BEGIN OF ty_final,
          emp_code  TYPE char40,
          emp_code1 TYPE char40,
          code      TYPE char100,
          location  TYPE char100,
          timestamp TYPE char40,
          date      TYPE char20,
          time      TYPE char20,
          inout     TYPE char40,
        END OF ty_final.
DATA it_final TYPE TABLE OF ty_final.
DATA wa_final TYPE  ty_final.
DATAit_ftpdata TYPE TABLE OF stru_ftpdata.
DATAw_handle TYPE i.

DATAfilename TYPE char80.

TYPES:
  BEGIN OF x_cmdout,
    line(100TYPE c,
  END OF x_cmdout.

TYPE-POOLS slis.

************************************************************************
*    D A T A
************************************************************************
*data specifications of FTP server

*Handler and Key
DATAw_cmd(40TYPE c,
      w_hdl     TYPE i,
      w_key     TYPE VALUE 26101957,
      w_slen    TYPE i,
      wa_cmdout TYPE x_cmdout,
      it_cmdout TYPE STANDARD TABLE OF x_cmdout.

*Constant declarations
CONSTANTSc_dest     TYPE rfcdes-rfcdest VALUE 'SAPFTP',
           c_host(15TYPE VALUE '199.111.111.11',
           c_ftp(6)   TYPE VALUE 'Mumbai',
           c_sap(16)  TYPE VALUE '/sap/inbound/SAPDIRECTORY'.

************************************************************************
*    P A R A M E T E R S
************************************************************************

SELECTION-SCREEN BEGIN OF BLOCK b1 WITH FRAME TITLE text-001.
PARAMETERSp_user(30TYPE LOWER CASE,
            p_pwd(30)  TYPE LOWER CASE,
            p_host(64TYPE LOWER CASE DEFAULT c_host,
            p_ftp      TYPE e_dexcommfilepath LOWER CASE DEFAULT c_ftp,
            p_sap      TYPE esefilepath LOWER CASE DEFAULT c_sap.
SELECTION-SCREEN END OF BLOCK b1.

************************************************************************
*AT SELECTION SCREEN Events
************************************************************************

AT SELECTION-SCREEN OUTPUT.

  LOOP AT SCREEN.
    CASE screen-name.
      WHEN 'P_PWD'."Set the password field as invisible
        screen-invisible '1'.
        MODIFY SCREEN.
    ENDCASE.
  ENDLOOP.

************************************************************************
*    S T A R T - O F - S E L E C T I O N
************************************************************************

START-OF-SELECTION.

*Connect to the FTP server.
  PERFORM ftp_connect.

*Find all files in the directory and store inside the Log table
  PERFORM log_files.

*Change the local save directory and download to SAP application server.
  PERFORM move_files.

*Close the connection
  PERFORM close_ftp.

************************************************************************
*END-OF-SELECTION
************************************************************************
END-OF-SELECTION.

*Display report.
  PERFORM disp_res.

*&---------------------------------------------------------------------
*& Form FTP_CONNECT
*&---------------------------------------------------------------------
*Make a connection to the FTP server
*----------------------------------------------------------------------
FORM ftp_connect .

  SET EXTENDED CHECK OFF.
  w_slen strlenp_pwd ).

  CALL FUNCTION 'HTTP_SCRAMBLE'
    EXPORTING
      source      p_pwd
      sourcelen   w_slen
      key         w_key
    IMPORTING
      destination p_pwd.

  CALL FUNCTION 'FTP_CONNECT'
    EXPORTING
      user            p_user
      password        p_pwd
      host            p_host
      rfc_destination c_dest
    IMPORTING
      handle          w_hdl
    EXCEPTIONS
      not_connected   1
      OTHERS          2.
  IF sy-subrc <> 0.
*Message will arise in case of any issues in connecting to the FTP server.
    MESSAGE text-e01 TYPE 'I'.
    LEAVE LIST-PROCESSING.
  ENDIF.

ENDFORM" FTP_CONNECT
*&---------------------------------------------------------------------
*& Form LOG_FILES
*&---------------------------------------------------------------------
*find all the files in the home directory
*----------------------------------------------------------------------
FORM log_files .

*Change directory to the FTP directory for LUPIN
  CONCATENATE 'cd' p_ftp INTO w_cmd SEPARATED BY space.

  CALL FUNCTION 'FTP_COMMAND'
    EXPORTING
      handle        w_hdl
      command       w_cmd
      compress      'N'
    TABLES
      data          it_cmdout
    EXCEPTIONS
      tcpip_error   1
      command_error 2
      data_error    3
      OTHERS        4.
  IF sy-subrc <> 0.
    MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
    WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
  ENDIF.
  REFRESH it_cmdout.
  CLEAR w_cmd.
  w_cmd 'ls'.

  CALL FUNCTION 'FTP_COMMAND'
    EXPORTING
      handle        w_hdl
      command       w_cmd
      compress      'N'
    TABLES
      data          it_cmdout
    EXCEPTIONS
      tcpip_error   1
      command_error 2
      data_error    3
      OTHERS        4.
  IF sy-subrc <> 0.
    MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
    WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
  ENDIF.

  CALL FUNCTION 'FTP_SERVER_TO_R3'
    EXPORTING
      handle         w_hdl
      fname          filename  " i hardcorded the file name as for testing
      character_mode 'X'
* IMPORTING
*     BLOB_LENGTH    =
    TABLES
*     BLOB           =
      text           it_ftpdata
    EXCEPTIONS
      tcpip_error    1
      command_error  2
      data_error     3
      OTHERS         4.
  IF sy-subrc <> 0.
* Implement suitable error handling here
  ENDIF.


  FIELD-SYMBOLS <fs_ftpdata> LIKE LINE OF it_ftpdata .

 
  LOOP AT it_ftpdata ASSIGNING <fs_ftpdata>.

    SPLIT <fs_ftpdata>-data1  AT ',' INTO wa_final-emp_code
                                          wa_final-emp_code1
                                          wa_final-code
                                          wa_final-location
                                          wa_final-timestamp
                                          wa_final-inout.

    Split wa_final-timestamp at ' ' INTO wa_final-date
                                         wa_final-time.

    APPEND wa_final TO it_final.
    CLEAR wa_final.
  ENDLOOP.



  REFRESH it_cmdout.

ENDFORM" LOG_FILES
*&---------------------------------------------------------------------
*& Form MOVE_FILES
*&---------------------------------------------------------------------
FORM move_files .

ENDFORM" MOVE_FILES
*&---------------------------------------------------------------------
*& Form CLOSE_FTP
*&---------------------------------------------------------------------
FORM close_ftp .

  CALL FUNCTION 'FTP_DISCONNECT'
    EXPORTING
      handle w_hdl.

  CALL FUNCTION 'RFC_CONNECTION_CLOSE'
    EXPORTING
      destination c_dest
    EXCEPTIONS
      OTHERS      1.
  IF sy-subrc <> 0.
    MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
    WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
  ENDIF.

ENDFORM" CLOSE_FTP
*&---------------------------------------------------------------------
*& Form DISP_RES
*&---------------------------------------------------------------------
*Display the output
*----------------------------------------------------------------------
FORM disp_res .

*  DATA : l_layout TYPE slis_layout_alv.
*
*  l_layout-colwidth_optimize = 'X'.
*
**Display table contents of updated files
*  CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
*    EXPORTING
*      i_callback_program = sy-repid
*      i_structure_name   = 'ZTBL_IPL_FTP'
*      is_layout          = l_layout
*    TABLES
*      t_outtab           = it_iplftp
*    EXCEPTIONS
*      program_error      = 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.

ENDFORM" DISP_RES