Application Development Blog Posts
Learn and share on deeper, cross technology development topics such as integration and connectivity, automation, cloud extensibility, developing at scale, and security.
cancel
Showing results for 
Search instead for 
Did you mean: 
jyoti_s_sahu
Explorer
12,922

Introduction:


This blog covers how to access a local file in background job in SAP. Basis knowledge of ABAP is required to understand the concept.

Its applicable for SAP Netweaver 7.4 onwards.

Ideally when the background job is running, it can access the file from application server. It can't access the file from presentation server. But let's say there is a requirement where i am getting the file in email. Then I am placing the file in my local PC. After that my need is that a program which is running in background on application server, should process the file.

How to achieve that ?

Please read further....

Main Content:


For demonstration purpose, I have explained below one scenario where one column is getting updated by background job in the excel file.

Scenario:


I have a file which is received in email. that I stored to my local computer. it contains the fields: Doc, Item, Cost and SalesAmount.

The file is filled with first 3 fields information. SalesAmount should be calculated using the background job. it will be cost + 10% of mark-up.

File format looks like below. Save the file with .csv extension. [test.csv]


Solution:


Configure FTP synchronization:


To achieve the above requirement, there is a need for FTP software. There are many open source FTP softwares are available. I have used WinSCP.

Create a folder in local system with the same name which is present in application server. Save the file to this folder.

in this case its saved to D:\FTP\in_main



Open WinSCP and configure the FTP server of SAP system.



Provide the server details and userid/pwd to configure the FTP for SAP server. you can get the details about SAP FTP server and login credentials from your basis team.



Create the session and login. After that you can visualize the folders in local system on left side and folders in application server FTP in right side. shown below.



Now I have to set-up the synchronization between local system folder and FTP server folder. The purpose is the read all the changes and write back to target and vice versa. So if there is any change to the file in local folder, it will be written to server. Same way if there is any change to the file in server, it will update to local folder.

follow the path mentioned below to sync local folder from server.



I have set the frequency 5 seconds. So each 5 seconds, system will read the file from server, if any change is there, it will update the local file.



There will be a window opened. it will keep showing the progress each 5 seconds. You can minimize the window.



Same way I have to configure the synchronization from local system to server. That is immediate. if there is any changes to local file, it will update the server file.





Click on Start. now you can see the file which was stored in the local folder, has been copied to the server folder. Then minimize the window.

Now there is nothing more required from the WinSCP.


Program Creation:


Create a program to read the file content from FTP server. Read the the cost amount, then calculate the salesamount by increasing 10%.

finally put back the file in FTP server. Program code is given below.
REPORT zjss.
CONSTANTS : lc_per TYPE p DECIMALS 2 VALUE '1.1'.
TYPES: BEGIN OF lty_file,
doc type char10,
item TYPE char6,
cost type p DECIMALS 2,
SalesAmount TYPE p DECIMALS 2,
END OF lty_file.
TYPES: BEGIN OF lty_string,
f1(10) TYPE c,
f2(10) TYPE c,
f3(10) TYPE c,
f4(10) TYPE c,
END OF lty_string.
DATA: lt_File TYPE STANDARD TABLE OF lty_File,
ls_File TYPE lty_File,
lt_string TYPE STANDARD TABLE OF lty_string,
lt_output TYPE STANDARD TABLE OF string,
ls_output TYPE string,
ls_string TYPE lty_string.
DATA: lv_app_server_file TYPE string.

START-OF-SELECTION.
PERFORM upload_file.
PERFORM download_File.

*&---------------------------------------------------------------------*
* Form upload_file
*&---------------------------------------------------------------------*
* To upload file data into SAP
*&---------------------------------------------------------------------*
FORM upload_file.
PERFORM upload_app_server.
PERFORM data_convert.
ENDFORM. "upload_file

*&---------------------------------------------------------------------*
* Form upload_app_server
*&---------------------------------------------------------------------*
* To read file from application server
*&---------------------------------------------------------------------*
FORM upload_app_server.

lv_app_server_file = '/sapdownload/BPC/in_main/test.csv'.
OPEN DATASET lv_app_server_file FOR INPUT IN TEXT MODE ENCODING DEFAULT.
DO.
READ DATASET lv_app_server_file INTO ls_string.
IF sy-subrc = 0.
APPEND ls_string TO lt_string.
ELSE.
EXIT.
ENDIF.
ENDDO.
CLOSE DATASET lv_app_server_file.
ENDFORM. "upload_app_server

*&---------------------------------------------------------------------*
* Form data_convert
*&---------------------------------------------------------------------*
* To convert file to specified format
*&---------------------------------------------------------------------*
FORM data_convert.
DATA: lv_file_separator TYPE c.
lv_file_separator = ','.
READ TABLE lt_string INTO ls_output INDEX 1.
APPEND ls_output to lt_output.
LOOP AT lt_string INTO ls_string FROM 2.
SPLIT LS_string at ',' INTO ls_string-f1 ls_string-f2 ls_string-f3 ls_string-f4.
ls_file-doc = ls_string-f1.
ls_file-item = ls_string-f2.
ls_file-cost = ls_string-f3.
ls_file-costprofit = ls_file-cost * lc_per.
ls_string-f4 = ls_file-costprofit.
CONDENSE ls_string-f4.
CONCATENATE ls_string-f1 ls_string-f2 ls_string-f3 ls_string-f4 INTO ls_output SEPARATED BY ',' .
APPEND ls_output to lt_output.
ENDLOOP.

ENDFORM. "data_convert

*&---------------------------------------------------------------------*
* Form download_file
*&---------------------------------------------------------------------*
* To assign field value into character format
*&---------------------------------------------------------------------*
FORM download_File.
OPEN DATASET lv_app_server_file FOR UPDATE IN TEXT MODE ENCODING DEFAULT.
LOOP AT lt_output INTO ls_output.
TRANSFER ls_output to lv_app_server_File.
ENDLOOP.
CLOSE DATASET lv_app_server_file.

ENDFORM. "data_assign

Execution:


Schedule the program in background. once the program execution is successful, verify the content of the file in local folder.





the SalesAmount has been updated.

Conclusion:


In the above article I have shown how to read a file available in local system and update it using background job in SAP.

I hope you will find this blog useful. Looking forward for your comments and feedback.
17 Comments
Labels in this area