Showing posts with label IDOC. Show all posts
Showing posts with label IDOC. Show all posts

Jul 2, 2026

IDOC Extension Step by Step in SAP




Ref: https://www.youtube.com/watch?v=aLv6uF2JM1M&t

Jun 17, 2026

Upload Internal Table to AL11 in Sap Abap

To upload internal table data to the SAP Application Server (Transaction AL11), you must use standard ABAP Dataset statements: OPEN DATASET, TRANSFER, and CLOSE DATASET. 

Below is a direct answer and complete guide on how to implement this programmatically. 
1. Simple Text Mode Upload
Use TEXT MODE if you want to upload data as a standard text file or CSV. 
abap
DATA: lv_filepath TYPE string VALUE '/usr/sap/interfaces/test_file.txt',
      ls_data     TYPE ty_your_structure. " Replace with your structure type

" Step 1: Open the application server file path
OPEN DATASET lv_filepath FOR OUTPUT IN TEXT MODE ENCODING DEFAULT.

IF sy-subrc = 0.
  " Step 2: Loop through the internal table and transfer records
  LOOP AT lt_internal_table INTO ls_data.
    TRANSFER ls_data TO lv_filepath.
  --ENDLOOP.

  " Step 3: Close the dataset (Mandatory)
  CLOSE DATASET lv_filepath.
  MESSAGE 'File uploaded successfully to AL11' TYPE 'S'.
ELSE.
  MESSAGE 'Error opening file path on AL11' TYPE 'E'.
ENDIF.
2. Comma-Separated Values (CSV) Upload 
If you want to view the fields properly structured in a spreadsheet layout later, convert the table to a string/CSV format using SAP_CONVERT_TO_CSV_FORMAT before sending it to the server.
abap
DATA: lv_filepath   TYPE string VALUE '/tmp/sales_data.csv',
      lt_csv_format TYPE truxs_t_text_data, " Standard table type for text
      lv_csv_line   TYPE string.

" Step 1: Convert internal table to CSV structure
CALL FUNCTION 'SAP_CONVERT_TO_CSV_FORMAT'
  EXPORTING
    i_field_seperator    = ','
  TABLES
    i_tab_sap_data       = lt_internal_table
  CHANGING
    i_tab_converted_data = lt_csv_format
  EXCEPTIONS
    conversion_failed    = 1
    OTHERS               = 2.

IF sy-subrc = 0.
  " Step 2: Write to Application Server
  OPEN DATASET lv_filepath FOR OUTPUT IN TEXT MODE ENCODING DEFAULT.
  
  IF sy-subrc = 0.
    LOOP AT lt_csv_format INTO lv_csv_line.
      TRANSFER lv_csv_line TO lv_filepath.
    ENDLOOP.
    CLOSE DATASET lv_filepath.
  ENDIF.
ENDIF.
3. Binary Mode Upload (For Excel / Zip / Images)
Use BINARY MODE if your data is an XSTRING or raw binary data stream (e.g., generating an actual .xlsx file using cl_salv_bs_lex). 
abap
DATA: lv_filepath TYPE string VALUE '/usr/sap/interfaces/report.xlsx',
      lv_xstring  TYPE xstring. " Contains your binary file data

OPEN DATASET lv_filepath FOR OUTPUT IN BINARY MODE.
IF sy-subrc = 0.
  TRANSFER lv_xstring TO lv_filepath.
  CLOSE DATASET lv_filepath.
ENDIF.
Key Syntax Reminders
  • FOR OUTPUT: Opens a file for writing. If the file already exists, this completely overwrites its contents.
  • FOR APPENDING: Opens a file and places the cursor at the end of the existing text. Use this if you want to append new rows without losing old logs.
  • ENCODING DEFAULT: Mandatory constraint for text mode file handling in modern Unicode SAP environments. 
If you would like, tell me:
  • What file type do you want to create in AL11 (TXT, CSV, XLSX)?
  • Are you handling large datasets that require specific block sizing?
I can provide the exact target layout adjustments or helper class transformations you need!

Jun 1, 2026

Sample Program of Inbound IDoc MRESCR/MRESCR01

To process the MRESCR01 inbound IDoc (used to create material reservations), SAP typically uses the standard function module IDOC_INPUT_MRESCR or a BAPI-wrapper via process code MRES or BAPI in WE42.

Below is an ABAP program sample that reads a flat file from the application server and manually forces IDoc creation using IDOC_INBOUND_ASYNCHRONOUS
abap
*&---------------------------------------------------------------------*
*& Report Z_INBOUND_IDOC_MRESCR01
*&---------------------------------------------------------------------*
*& Sample program to generate and process MRESCR01 inbound IDoc
*&---------------------------------------------------------------------*
REPORT z_inbound_idoc_mrescr01.

* DATA DECLARATIONS
DATA: lv_file     TYPE string VALUE '/usr/sap/tmp/reservation_data.txt',
      lv_lines    TYPE i,
      ls_edidc    TYPE edidc,
      lt_edidd    TYPE TABLE OF edidd,
      ls_edidd    TYPE edidd,
      lt_control  TYPE TABLE OF edidc,
      lv_msg_text TYPE string.

* SELECTION SCREEN
PARAMETERS: p_file LIKE lgpa-filepath DEFAULT '/usr/sap/tmp/reservation_data.txt'.

START-OF-SELECTION.

  " 1. Read flat file from application server
  PERFORM read_flat_file CHANGING lt_edidd.

  " 2. Build Control Record
  PERFORM build_control_record CHANGING ls_edidc.

  " 3. Call Standard Inbound Function
  CALL FUNCTION 'IDOC_INBOUND_ASYNCHRONOUS'
    EXPORTING
      pi_edidc      = ls_edidc
    TABLES
      pit_edidd     = lt_edidd
    EXCEPTIONS
      error_message = 1
      OTHERS        = 2.

  IF sy-subrc = 0.
    COMMIT WORK.
    WRITE: / 'IDoc successfully created and triggered for processing.'.
  ELSE.
    lv_msg_text = sy-msgli.
    WRITE: / 'Error creating IDoc. Message:', lv_msg_text.
  ENDIF.

*&---------------------------------------------------------------------*
*& Form BUILD_CONTROL_RECORD
*&---------------------------------------------------------------------*
FORM build_control_record CHANGING cs_edidc TYPE edidc.

  cs_edidc-mestyp = 'MRESCR'.         " Message Type
  cs_edidc-idoctp = 'MRESCR01'.       " Basic IDoc Type
  cs_edidc-direct = '2'.              " Direction: 2 = Inbound
  cs_edidc-rcvprt = 'LS'.             " Partner Type: Logical System
  cs_edidc-rcvprn = 'SAPD01'.         " Receiver Partner Number (Your SAP system)
  cs_edidc-sndprt = 'LS'.             " Sender Partner Type
  cs_edidc-sndprn = 'LEGACY_SYS'.     " Sender Partner Number (Source System)

ENDFORM.

*&---------------------------------------------------------------------*
*& Form READ_FLAT_FILE
*&---------------------------------------------------------------------*
FORM read_flat_file CHANGING ct_edidd TYPE STANDARD TABLE of edidd.

  DATA: lv_string TYPE string,
        ls_header TYPE e1bpresbc,   " Standard BAPI segment for MRESCR01 header
        ls_item   TYPE e1bpresbc.   " Standard BAPI segment for item

  OPEN DATASET p_file FOR INPUT IN TEXT MODE ENCODING DEFAULT.
  IF sy-subrc NE 0.
    WRITE: / 'Cannot open file:', p_file.
    EXIT.
  ENDIF.

  DO.
    READ DATASET p_file INTO lv_string.
    IF sy-subrc NE 0. EXIT. ENDIF.

    " Add file parsing logic here depending on your file structure
    " Example: Split CSV by comma and populate IDoc segments

    " Example for Header segment
    CLEAR ls_edidd.
    ls_edidd-segnam = 'E1BPRESBC'. " Segment Name
    ls_header-mov_type = '201'.
    ls_header-res_date = sy-datum.
    ls_edidd-sdata = ls_header.
    APPEND ls_edidd TO ct_edidd.

    " Example for Item segment
    CLEAR ls_edidd.
    ls_edidd-segnam = 'E1BPRESBC'.
    " Populate ls_item fields here ...
    ls_edidd-sdata = ls_item.
    APPEND ls_edidd TO ct_edidd.

  ENDDO.

  CLOSE DATASET p_file.

ENDFORM.

Next Steps for Implementation
  1. Define Message/IDoc Types: Ensure MRESCR (Message) and MRESCR01 (IDoc) are active in your system using transaction WE81 and WE30.
  2. Assign Inbound Function: Register your process code in WE42 (usually linked to IDOC_INPUT_MRESCR or BAPI_IDOC_INPUT1) so that the IDoc posts application data.
  3. Background Processing: If you set your Inbound Partner Profile (WE20) to collect IDocs instead of processing them immediately, you will need to schedule standard report RBDAPP01 to actually post the application data (status 64 to 53).