FTP file transfer in Background

FTP file transfer in Background:
 
1)      Creating RFC Destination for FTP

Run program: RSFTP005.
The RFC destinations SAPFTP and SAPFTPA will be created automatically.

a)      SAPFTP (for local system)
b)      SAPFTPA (for running in Application server)(This is used for file transfer in background.
 
2)      Code sample for developing FTP programs can be got from report program
 
a)     RSFTP002
b)     RSFTP007
c)      RSFTP008
 
3)      Sample code for getting customer details as tab delimited .TXT file in background.
 
*&———————————————————————*
*& Report ZCUSTOMER
*&
*&———————————————————————*
*&-Customer -All new/changed customer records created during the period.
*&———————————————————————*
*LedgerKey————-Char(50)
*AccountName———–Char(80)
*AccountReference —–Char(50)
*Address1 ————-Char(80)
*Address2 ————-Char(80)
*Town —————–Char(50)
*County / State ——-Char(50)
*Post Code / ZIP ——Char(20)
*Country—————Char(2)
*AccountCurrencyCode –Char(3)
*ContactTelephone —–Char(20)

REPORT  zcustomer.

TABLES: kna1,knb1.

TYPES: BEGIN OF st_customer,
ledgerkey TYPE char50,
name1 TYPE kna1-name1,
kunnr TYPE kna1-kunnr,
name2 TYPE kna1-name2,
stras TYPE kna1-stras,
ort01 TYPE kna1-ort01,
regio TYPE kna1-regio,
pstlz TYPE kna1-pstlz,
land1 TYPE kna1-land1,
waers TYPE knvv-waers,
telf1 TYPE kna1-telf1,
END OF st_customer.

TYPES : BEGIN OF st_cus,
ledgerkey(50)  TYPE c, “LedgerKey
wrk_delim1      TYPE x ,
name1(80)   TYPE c,  “AccountName
wrk_delim2      TYPE x ,
kunnr(50)   TYPE c,  “AccountReference
wrk_delim3      TYPE x ,
name2(80)   TYPE c,      “Address1
wrk_delim4      TYPE x ,
stras(80)   TYPE c,    ” Address2
wrk_delim5      TYPE x ,
ort01(50)   TYPE c,   ” Town
wrk_delim6      TYPE x ,
regio(50)   TYPE c,   ” County / State
wrk_delim7      TYPE x ,
pstlz(10)   TYPE c,   “Post Code / ZIP
wrk_delim8      TYPE x ,
land1(2)    TYPE c,    “Country
wrk_delim9      TYPE x ,
waers(3)    TYPE c,    “AccountCurrencyCode
wrk_delim10     TYPE x ,
telf1(20)   TYPE c,    “ContactTelephone
END OF st_cus .

DATA: wrk_file TYPE char200.
DATA: it_customer TYPE STANDARD TABLE OF st_customer,
wa_customer LIKE LINE OF it_customer.

DATA: it_dat TYPE STANDARD TABLE OF st_cus,
wa_dat LIKE LINE OF it_dat.

SELECTION-SCREEN BEGIN OF BLOCK b2 WITH FRAME TITLE text-001.

SELECT-OPTIONS:cust_no FOR  kna1-kunnr,”customer no
com_code FOR knb1-bukrs,”company code
country FOR kna1-land1, “country
creat_on FOR kna1-erdat default sy-datum. “created or changed on
SELECTION-SCREEN END OF BLOCK b2.

START-OF-SELECTION.

IF creat_on-high IS INITIAL.
creat_on-high = creat_on-low.
ENDIF.

CONCATENATE sy-mandt ‘1‘  creat_on-low ‘_’ creat_on-high ‘_Cust.txt’ INTO
wrk_file.

PERFORM customer_data_select.    “Customer Data File
PERFORM build_template_data.
PERFORM ftp_file_customer.

*&———————————————————————*
*&      Form  CUSTOMER_DATA_SELECT
*&———————————————————————*
*       text
*———————————————————————-*
*  –>  p1        text
*  <–  p2        text
*———————————————————————-*
FORM customer_data_select .

SELECT a~name1 a~name2 a~kunnr a~stras a~ort01 a~regio a~pstlz a~land1 a~telf1    “Details of the Customer based on KNA1.
FROM kna1 AS a
INNER JOIN knb1 AS b ON a~kunnr = b~kunnr
INTO CORRESPONDING FIELDS OF TABLE it_customer
WHERE a~kunnr IN cust_no
AND ( ( a~erdat IN creat_on OR a~updat IN creat_on ) OR
( b~erdat IN creat_on OR b~updat IN creat_on ) )
AND a~land1 IN country
AND b~bukrs IN com_code.

IF sy-subrc = 0.
LOOP AT it_customer INTO wa_customer.

SELECT SINGLE waers FROM knvv INTO wa_customer-waers
WHERE kunnr = wa_customer-kunnr..

wa_customer-ledgerkey = ‘12345’.

MODIFY it_customer FROM wa_customer.

ENDLOOP.

ENDIF.

ENDFORM.                    ” CUSTOMER_DATA_SELECT
*&———————————————————————*
*&      Form  BUILD_TEMPLATE_DATA
*&———————————————————————*
*       text
*———————————————————————-*
*  –>  p1        text
*  <–  p2        text
*———————————————————————-*
FORM build_template_data .

LOOP AT it_customer INTO wa_customer.
MOVE-CORRESPONDING wa_customer TO wa_dat.

wa_dat-wrk_delim1  = ’09’.  “for adding tab after each field
wa_dat-wrk_delim2  = ’09’.
wa_dat-wrk_delim3  = ’09’.
wa_dat-wrk_delim4  = ’09’.
wa_dat-wrk_delim5  = ’09’.
wa_dat-wrk_delim6  = ’09’.
wa_dat-wrk_delim7  = ’09’.
wa_dat-wrk_delim8  = ’09’.
wa_dat-wrk_delim9  = ’09’.
wa_dat-wrk_delim10 = ’09’.
APPEND wa_dat TO it_dat.
ENDLOOP.

ENDFORM.                    ” BUILD_TEMPLATE_DATA
*&———————————————————————*
*&      Form  FTP_FILE_CUSTOMER
*&———————————————————————*
*       text
*———————————————————————-*
*  –>  p1        text
*  <–  p2        text
*———————————————————————-*
FORM ftp_file_customer .

DATA:  l_user(30) TYPE c VALUE ‘ZTEST’, “user name of ftp server
l_pwd(30) TYPE c VALUE ‘1234’, “password of ftp server
l_host(64) TYPE c VALUE ‘111.2.1.198’, “ip address of FTP server
l_dest LIKE rfcdes-rfcdest VALUE ‘SAPFTPA’.”Background RFC destination

DATA: w_hdl TYPE i,
c_key TYPE i VALUE 26101957,
l_slen TYPE i.

*HTTP_SCRAMBLE: used to scramble the password provided into a format, which is been recognized by SAP.
SET EXTENDED CHECK OFF.
l_slen = STRLEN( l_pwd ).

CALL FUNCTION ‘HTTP_SCRAMBLE’
EXPORTING
SOURCE      = l_pwd
sourcelen   = l_slen
key         = c_key
IMPORTING
destination = l_pwd.

* To Connect to the Server using FTP
CALL FUNCTION ‘FTP_CONNECT’
EXPORTING
user            = l_user
password        = l_pwd
host            = l_host
rfc_destination = l_dest
IMPORTING
handle          = w_hdl
EXCEPTIONS
OTHERS          = 1.

*FTP_R3_TO_SERVER:used to transfer the internal table data as a file to other system in the character mode.

CALL FUNCTION ‘FTP_R3_TO_SERVER’
EXPORTING
handle         = w_hdl
fname          = wrk_file          “file path of destination system
character_mode = ‘X’
TABLES
text           = it_dat
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
RAISING invalid_output_file.
ENDIF.

*FTP_DISCONNECT: This is used to disconnect the connection between SAP and other system.

* To disconnect the FTP
CALL FUNCTION ‘FTP_DISCONNECT’
EXPORTING
handle = w_hdl.

*RFC_CONNECTION_CLOSE:This is used to disconnect the RFC connection between SAP and other system.

CALL FUNCTION ‘RFC_CONNECTION_CLOSE’
EXPORTING
destination = l_dest
EXCEPTIONS
OTHERS      = 1.

ENDFORM.                    ” FTP_FILE_CUSTOMER

On running this report in background a txt file with customer details will be created in ftp server location.

Was this article helpful?

Related Articles

Leave A Comment?

You must be logged in to post a comment.