Transfer Data Between SAP & FTP Server

Untuk mengakses data pada FTP server, berikut adalah function module yang bisa digunakan.

Open Connection

**Scramble the password provided in a format recognized by SAP.
  CALL FUNCTION 'SCRAMBLE_STRING'
    EXPORTING
      SOURCE = fi_password
      key    = 26101957
    IMPORTING
      target = ld_password.

**Connect to the Server using FTP
  CALL FUNCTION 'FTP_CONNECT'
    EXPORTING
      user            = fi_username
      password        = ld_password
      host            = fi_host
      rfc_destination = fi_rfcdest
    IMPORTING
      handle          = ld_handle
    EXCEPTIONS
      not_connected   = 1
      OTHERS          = 2.

Close Connection

**Disconnect the FTP
  CALL FUNCTION 'FTP_DISCONNECT'
    EXPORTING
      handle = ld_handle.

  CALL FUNCTION 'RFC_CONNECTION_CLOSE'
    EXPORTING
      destination = fi_rfcdest
    EXCEPTIONS
      OTHERS      = 1.

Change folder
FM perlu digunakan terlebih dahulu sebelum mengakses file pada folder FTP.

**Change FTP Folder
**ld_command = 'cd /foldername'

  CALL FUNCTION 'FTP_COMMAND'
    EXPORTING
      handle        = ld_handle
      command       = ld_command
      compress      = 'N'
    TABLES
      data          = lt_result
    EXCEPTIONS
      tcpip_error   = 1
      command_error = 2
      data_error    = 3
      OTHERS        = 4.

Get List File

**Get list file in FTP Folder
  CALL FUNCTION 'FTP_COMMAND'
    EXPORTING
      handle        = ld_handle
      command       = 'nlist'
    TABLES
      data          = lt_files
    EXCEPTIONS
      tcpip_error   = 1
      command_error = 2
      data_error    = 3
      OTHERS        = 4.

Reading file

****Reading file
    CALL FUNCTION 'FTP_SERVER_TO_R3'
      EXPORTING
        handle         = ld_handle
        fname          = lt_files-line
        character_mode = 'X'
      IMPORTING
        blob_length    = ld_bloblen
      TABLES
        blob           = lt_blob
        text           = lt_text
      EXCEPTIONS
        tcpip_error    = 1
        command_error  = 2
        data_error     = 3
        OTHERS         = 4.

Write File

****Write to FTP Server
    CALL FUNCTION 'FTP_R3_TO_SERVER'
      EXPORTING
        handle         = ld_handle
        fname          = ld_file_out "filename
        blob_length    = ld_bloblen
        character_mode = 'X'
      TABLES
        blob           = lt_blob
        text           = lt_text
      EXCEPTIONS
        tcpip_error    = 1
        command_error  = 2
        data_error     = 3
        OTHERS         = 4.

Delete File

****Delete file after processing
****ld_command = 'del filename'

    CALL FUNCTION 'FTP_COMMAND'
      EXPORTING
        handle        = ld_handle
        command       = ld_command
      TABLES
        data          = lt_result
      EXCEPTIONS
        tcpip_error   = 1
        command_error = 2
        data_error    = 3
        OTHERS        = 4.

Full code-nya bisa dilihat di SINI.

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.