10-27-2015 9:56 AM
Hello SCN Members,
We have a requirement to transfer an XML and few PDF's to an external FTP location.
The XML contains delivery information(let's say 8 deliveries) and PDF's are invoices for these 8 deliveries.
Everything works fine, except sometimes one or more invoice PDF's are not in the FTP.
If we rerun the process, the file is sent correctly.
Adding the code. Any ideas how to resolve this?
Should a wait be added to make sure there is enough time to write the file to ftp before sending another write request?
FORM file_to_ftp .
DATA : lv_dest TYPE rfcdes-rfcdest VALUE 'SAPFTPA',
lv_slen TYPE i,
lv_len TYPE i,
lv_command(120) TYPE c,
lv_outfile LIKE rlgrap-filename,
lv_key TYPE i VALUE 26101957,
lv_handle TYPE i,
lt_tab TYPE tsfixml,
lt_result TYPE TABLE OF text WITH HEADER LINE.
CLEAR gv_ftp_error .
IF lv_handle IS INITIAL.
lv_slen = STRLEN( gs_3pl_definition-password ).
CALL FUNCTION 'HTTP_SCRAMBLE'
EXPORTING
SOURCE = gs_3pl_definition-password
sourcelen = lv_slen
key = lv_key
IMPORTING
destination = gs_3pl_definition-password.
CALL FUNCTION 'FTP_CONNECT'
EXPORTING
user = gs_3pl_definition-userid
password = gs_3pl_definition-password
host = gs_3pl_definition-ftp
rfc_destination = lv_dest
IMPORTING
handle = lv_handle
EXCEPTIONS
not_connected = 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.
* HOME OR ROOT
CONCATENATE 'cd' '/..' INTO lv_command SEPARATED BY ' '.
REFRESH lt_result.
CALL FUNCTION 'FTP_COMMAND'
EXPORTING
handle = lv_handle
command = lv_command
verify = 'X'
TABLES
data = lt_result
EXCEPTIONS
tcpip_error = 1
command_error = 2
data_error = 3.
CONCATENATE 'cd' gs_3pl_definition-path INTO lv_command SEPARATED BY ' '.
REFRESH lt_result.
CALL FUNCTION 'FTP_COMMAND'
EXPORTING
handle = lv_handle
command = lv_command
verify = 'X'
TABLES
data = lt_result
EXCEPTIONS
tcpip_error = 1
command_error = 2
data_error = 3.
lv_command = 'set passive on'.
CALL FUNCTION 'FTP_COMMAND'
EXPORTING
handle = lv_handle
command = lv_command
verify = 'X'
TABLES
data = lt_result
EXCEPTIONS
tcpip_error = 1
command_error = 2
data_error = 3.
CONCATENATE gv_date 'Nuskin' gv_time '.xml' INTO lv_outfile SEPARATED BY '_'.
IF gv_reprint = 'X'.
CONCATENATE 'R-' lv_outfile INTO lv_outfile.
ENDIF.
CALL FUNCTION 'SCMS_XSTRING_TO_BINARY'
EXPORTING
buffer = ixml
IMPORTING
output_length = lv_len
TABLES
binary_tab = lt_tab.
CALL FUNCTION 'FTP_R3_TO_SERVER'
EXPORTING
handle = lv_handle
fname = lv_outfile
blob_length = lv_len
TABLES
blob = lt_tab
EXCEPTIONS
tcpip_error = 1
command_error = 2
data_error = 3
OTHERS = 4.
IF sy-subrc <> 0.
gv_ftp_error = 'X'.
ENDIF.
SORT gt_pdf_temp BY vbeln.
LOOP AT gt_pdf_temp INTO wa_pdf_temp.
READ TABLE gt_results WITH KEY vgbel = wa_pdf_temp-vbeln.
IF sy-subrc = 0.
REFRESH lt_tab.
lv_outfile = wa_pdf_temp-pdf_key.
lv_outfile(8) = gv_date.
lv_outfile+20(6) = gv_time.
IF gv_reprint = 'X'.
CONCATENATE 'R-' lv_outfile INTO lv_outfile.
ENDIF.
CALL FUNCTION 'SCMS_XSTRING_TO_BINARY'
EXPORTING
buffer = wa_pdf_temp-pdf_string
IMPORTING
output_length = lv_len
TABLES
binary_tab = lt_tab.
IF sy-subrc = 0.
CALL FUNCTION 'FTP_R3_TO_SERVER'
EXPORTING
handle = lv_handle
fname = lv_outfile
blob_length = lv_len
TABLES
blob = lt_tab
EXCEPTIONS
tcpip_error = 1
command_error = 2
data_error = 3
OTHERS = 4.
IF sy-subrc <> 0.
gv_ftp_error = 'X'.
ENDIF.
ENDIF.
DELETE FROM zpdf_temp WHERE pdf_key = wa_pdf_temp-pdf_key.
ENDIF.
CLEAR : wa_pdf_temp, lv_len.
ENDLOOP.
CALL FUNCTION 'FTP_DISCONNECT'
EXPORTING
handle = lv_handle.
CALL FUNCTION 'RFC_CONNECTION_CLOSE'
EXPORTING
destination = lv_dest
EXCEPTIONS
OTHERS = 1.
ENDIF.
ENDFORM.
10-27-2015 10:31 AM
In the FTP_COMMAND you should manage the returned code and the log returned in DATA to build your own log, in other FTP FM, analyze the return codes/error messages.
Regards,
Raymond
10-27-2015 10:37 AM
Hi Raymond,
Thank you for the inputs.
The problem is with FTP_R3_TO_SERVER FM(rest all commands works fine as we have some files written) and we have the return code check at this FM. Here, the field GV_FTP_ERROR is marked as X and later this variable is used to write the message.
The problem is it is never X.
Regards,
Richa
10-27-2015 10:55 AM
Hi Richa,
Can you clear the variable "lv_outfile " within the loop where you are trying to send PDF files.
Regards,
Kiran
10-27-2015 11:16 AM
Hi Kiran,
Thanks for your inputs. I can surely clear the variable, but this will not help. Reason being -
lv_outfile = wa_pdf_temp-pdf_key
The field is of type datestamp-delivery-timestamp.
The delivery is always unique even if the date and time stamp are the same.
This field is the key field of the table from where I extract data and send to FTP.
Regards,
Richa
10-27-2015 1:31 PM
Are you sure that the code is actually executed, there are some sy-subrc check that may prevent this and won't set the global error flag. (empty LOOP, READ TABLE, etc.)
Regards,
Raymond
10-27-2015 1:44 PM
Hi Raymond,
I will give u a real time example -
The xml is having 56 deliveries and 52 pdf's, 4 pdf's are missing.
The gt_pdf_temp table had 56 entries for 56 deliveries.
This is the reason why i say, the basic logic is working fine. Randomly some pdf's are not saved in ftp.
Regards,
Richa
10-28-2015 5:05 AM
Hi Richa,
As Raymond is rightly mentioning, you may need to patiently check if the logic is getting called for all the 56 PDF's in your case.
Just a hunch, it might be a failure of the function module call CALL FUNCTION 'SCMS_XSTRING_TO_BINARY' (the call within the LOOP...ENDLOOP) in all those four cases mentioned by you. Kindly handle ELSE part for IF SY-SUBRC = 0 check for this function module call and place a break point within the ELSE part to check if the call is failing for those 4 entries mentioned by you. Then you might understand the issue.
Regards,
Kiran
12-10-2015 6:02 AM
I dont know if this is still relevant to you but why not make your life easier by using 3rd-party-tools?
For simple File-transfer just use Filezilla but for scripts and such you could use SysaxAutomation (sysax.com) and create automated scripts.