Skip to Content

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.

To report this post you need to login first.

17 Comments

You must be Logged on to comment or reply to a post.

      1. Shai Sinai

        It is done in your file system (not SAP) and depends on your operating system.

        Googling your operating system and “shared folder” should give you some hints.

        (1) 
        1. Jyoti Sankar Sahu Post author

          I think you must have gone through the blog and its context/requirement. Most of SAP application servers work on Linux and local computers will be on Windows. Can you share some details, how you are going to configure a folder which will be shared among Linux OS of application server and Local folder of Windows(local computer).

          (0) 
  1. Matthew Billingham

    There are a number of points of bad programming practice.

    1. You are using the L prefix for variables that are global.
    2. In fact. nowhere do you have local variables.
    3. You are using global variables in contexts where they should be local. ls_output for example.

    Further, you are using FORMs which are now obsolete.

    (5) 
    1. Jyoti Sankar Sahu Post author

      I think you must have gone through the entire blog. This is to describe how to access a file from local system in background job. The code is just the sample code to execute for demonstration. Its not the core part. This blog is not about coding standards or naming.

      (1) 
      1. Matthew Billingham

        Yes I have gone through the entire blog – do you expect people to only half read it?! 🙂

        I think it is good for blog writers to endeavour to reach the highest possible standards. It doesn’t take not much effort to publish ABAP code that is well written and adheres to modern good practice. There’s no reason why sample code shouldn’t be high quality.

        Poor programming practice detracts from the overall quality of the blog, regardless of the overall utility of the offered solution.

        (2) 
      2. Sandra Rossi

        I agree, people can’t be experts in all topics, so we should focus more on the core of the post, and only post fair comments about the rest. I think that Jelena is more objective about the main issue of this post.

        (1) 
        1. Matthew Billingham

          I’d expect someone posting a blog with the ABAP Development tag to be reasonably expert in the basics of programming! I don’t expect perfection, but a certain level of quality would be nice.

          The rest of the blog is fairly well written, even if the content isn’t exactly groundbreaking. I feel it is let down by the ABAP.

          (1) 
  2. Jelena Perfiljeva

    Isn’t the title a little misleading? The program is not actually accessing the local file. You’re just using FTP to copy a local file to a server. The fact that FTP client offers synchronization doesn’t make it much better as this whole thing goes down the second you switch off the PC (or any machine where FTP client runs from).

    And why does it require Netweaver 7.4? This is a textbook OPEN DATASET example. The FTP part might as well be done by any FTP client. The code would run in any earlier release.

    Sorry, I don’t get it…

    ¯\_(ツ)_/¯

    (4) 
    1. Sandra Rossi

      100% agree. The only interest of this post is about WinSCP (and by searching a little bit, there’s probably another post somewhere). Or if the goal is to talk about accessing local files in background, then there should be all solutions. As this has been discussed in many other posts, it’s rather easy to list them, and add links to them. Post only what has not been posted yet.

      (1) 
      1. Jelena Perfiljeva

        I searched in Google for “shared folder Linux site:sap.com” based on the hint from Shai Sinai above and found several old questions that also mentioned WinSCP.

        It would’ve been beneficial if the author shared more information about the business scenario as well as potential alternatives and advantages/disadvantages (e.g. what about security? why not just write a program to read a local file?) compared to other solutions. Otherwise the blog just doesn’t offer a lot to the readers. It’s not even like no one is ever allowed to write more than once on the subject. But if we do then we need to make sure to add more value.

        (0) 
        1. Shai Sinai

          Actually, shared folders in Linux has no much to do with SAP application server itself since it’s a generic question in Linux area. Hence, limiting the results to sap.com might not return the best results.

          Anyway, as I’ve said before, the simplest (and most common) solution would be using SMB share, which is used in Windows system and was implemented also for Linux (e.g. Samba).

          Afterwards, we are talking about standard method of reading/writing files from application server, as described by Jyoti Sankar Sahu (And, as you’ve mentioned, doesn’t really handle the issue in the topic, but bypasses it instead).

          (1) 
    2. Matthew Billingham

      My guess is “Applicable from 7.4 onwards” means that that’s the level of system on which it was developed. So he knows it works there, but can’t guarantee that it will run on lower levels. I know that it will, but that’s only because I’ve been writing ABAP since 30D.

      If only he’d written it using 7.4 constructs, then his statement would have been true!

      (1) 
  3. Denis Konovalov

    On the FTP part – if you have this 2 way synchronization, aren’t you creating never ending loop ?
    immediately after the file is written to local dir it will be uploaded to server, where it will be processed be the abap program and written back, which will trigger transfer to local dir within 5 second and then an immediate transfer back to server (because to local system file will be new), there the program changes file again and circle continues.

    I might have missed the additional controls, if so – my apologies.

    (0) 

Leave a Reply