Writing File on Application Server

 

  • The most important transaction code for File Handling on Application Server is AL11.
  • AL11 is a transaction code for SAP Directories.


  • Below are few statements which are important to work with File Handling on Application Server.
    1. OPEN DATASET :- Opens the specified file
    2. CLOSE DATASET :- Closes the specified table.
    3. DELETE DATASET :- Deletes the specified file.
    4. READ DATASET :- Read a record from the file.
    5. TRANSFER :- Write a record into a file.

Requirement :-

  • let’s take a requirement to understand it more clearly.
  • Suppose, User gave a Sales Document number as input on the select screen.
  • I will store the details of Sales Document number in a internal table.
  • Then I will store it a directory of AL11 transaction code.

Solution :-

  • Step 1 :- Create a executable program in ABAP Editor.



  • Step 2 :- Create a type structure and a corresponding internal table for the type structure.



  • Step 3 :- Create a select option through which user will give input on the selection screen.



  • Step 4 :- In Start Of Selection, Write the select query to fetch the data from VBAK table.



  • Step 5 :- Declare a directory in which you will save your file.

  • In the directory give a file name where your data will be saved.



  • Step 6 :- Use OPEN_DATASET to open a file.

    • Loop into the internal table and concatenate the record into one string and then use transfer syntax to transfer into the directory.


  • Step 7 :- Use close data set to close the file.




Code :-

*&---------------------------------------------------------------------*
*& Report ZAR_OPEN_FILE
*&---------------------------------------------------------------------*
*&
*&---------------------------------------------------------------------*
REPORT ZAR_OPEN_FILE.

types : begin of ty_vbak,
   vbeln type vbeln_va,
   erdat type erdat,
   erzet type erzet,
  ernam type ernam,
  vbtyp type vbtypl,
  end of ty_vbak.

DATA : lt_vbak type table of ty_vbak.

DATA : lv_vbeln type vbeln_va.
SELECT-OPTIONS : s_vbeln for lv_vbeln.

START-OF-SELECTION.
Select vbeln erdat erzet ernam vbtyp
  from vbak
  into table lt_vbak
  where vbeln in s_vbeln.

  If lt_vbak is NOT INITIAL.
    DATA : lo_direct(20) type c value '/usr/sap/tmp/VBAK.txt'.

    OPEN DATASET lo_direct for OUTPUT in TEXT MODE ENCODING DEFAULT.
    if sy-subrc eq 0.
      Loop at lt_vbak into data(ls_vbak).
        concatenate ls_vbak-vbeln ls_vbak-erdat
                    ls_vbak-erzet ls_vbak-ernam ls_vbak-vbtyp
                    into data(lv_string) SEPARATED BY SPACE.

        TRANSFER lv_string to lo_direct.

      ENDLOOP.
      endif.

      CLOSE DATASET lo_direct.
    endif.

Execute the Code :-



  • Press F8 to execute.
  • Now go to AL11.
  • Open the directory .


  • VBAK.txt has been created.
  • Double click on it.


Comments

Popular posts from this blog

Understanding Different Types of SAP Function Modules: Normal, RFC, and Update

Mastering ABAP: A Step-by-Step Guide to Function Modules and Groups for Modularized Programming

Introduction to SAP