WHAT IS BAPI?
& STEP BY STEP PROCEDURE
A BAPI is a standardized programming interface (function module) provided by SAP
to allow external systems or internal programs to access and interact with SAP
business objects.
Enable data integration between SAP and other systems (like Java, .NET,
Python, etc.).
Allow mass data operations (create, change, display, delete).
Support modular and reusable ABAP code.
Used in interfaces, enhancements, and automation.
Released by SAP for public use (stable and safe).
RFC-enabled: Can be called remotely (e.g., from non-SAP systems).
Work with business object types (like Material, Customer, Vendor).
Let’s suppose we want to upload below fields data:
matnr = Material Number
mbrsh = Industry
mtart = Material Type
maktx = Description
meins = Base Unit of Measure
matkl = Material Group
Step 1: Prepare excel for above fields data
Note: Field sequence should be same as given
Step 2: Create text file for above data
Step 3: Create below executable program in SE38 or SE80
Step 4: Save, Activate and Execute
You will get below message that means your material has
been uploaded
Also you can check in MARA table
Now Let's break down and explain the code section by section.
🔹 1. Report Definition
REPORT zmat_bapi.
Defines the name of the custom ABAP report (ZTEST_FILE ).
🔹 2. Type Declaration
TYPES: BEGIN OF t_mara,
matnr TYPE mara-matnr, "Material Number
mbrsh TYPE mara-mbrsh, "Industry
mtart TYPE mara-mtart, "Material Type
maktx TYPE makt-maktx, "Description
meins TYPE mara-meins, "Base Unit of Measure
matkl TYPE mara-matkl, "Material Group
END OF t_mara.
Defines a structure t_mara to hold the input data from the file:
🔹 3. Data Declarations
DATA: it_data TYPE TABLE OF ty_data,
wa_data TYPE ty_data.
it_data: Internal table to hold uploaded data.
wa_data: Work area for looping through it_data.
Other variables:
DATA: file_01 TYPE string.
DATA: wa_mathead TYPE bapimathead,
wa_client TYPE bapi_mara,
wa_clientx TYPE bapi_marax,
it_des TYPE TABLE OF bapi_makt,
wa_des TYPE bapi_makt,
it_ret TYPE TABLE OF bapiret2,
wa_ret TYPE bapiret2.
Variables for passing data to BAPI_MATERIAL_SAVEDATA.
bapimathead, bapi_mara, bapi_marax, etc. are BAPI structures used for material master data.
it_return: Holds the return messages from the BAPI call.
4. Parameter for File Selection
PARAMETERS: file TYPE localfile.
Allows user to provide file path on selection screen.
AT SELECTION-SCREEN ON VALUE-REQUEST FOR file.
CALL FUNCTION 'F4_FILENAME'..
Adds F4 help to the file field (opens file explorer dialog).
5. Upload File Using GUI_UPLOAD
CALL FUNCTION 'GUI_UPLOAD'
EXPORTING
filename = file_01
filetype = 'ASC'
has_field_separator = 'X'
* HEADER_LENGTH =0
Uploads the text file (comma/tab-separated).
Loads the contents into internal table it_data.
6. Loop to Process Each Material
LOOP AT it_DATA INTO wa_DATA.
For each record:
Header data:
wa_mathead-material = wa_data-matnr.
wa_mathead-ind_sector = wa_data-mbrsh.
wa_mathead-matl_type = wa_data-mtart.
wa_mathead-basic_view = 'X'.
Client-level data (general data):
wa_client-matl_group = wa_data-matkl.
wa_client-base_uom = wa_data-meins.
wa_clientx-matl_group = 'X'.
wa_clientx-base_uom = 'X'.
Material description:
wa_des-matl_desc = wa_data-maktx.
wa_des-langu = sy-langu.
7. Call BAPI to Create Material
CALL FUNCTION 'BAPI_MATERIAL_SAVEDATA'
EXPORTING
headdata = wa_mathead
clientdata = wa_client
clientdatax = wa_clientx
* PLANTDATA =
Sends the data to BAPI to create the material.
The result/return messages are appended to it_return.
Then:
CLEAR: wa_des, wa_mathead…
Clear work areas for next iteration.
8. Display Output Using SALV
TRY.
CALL METHOD cl_salv_table=>factory
EXPORTING
list_display = if_salv_c_bool_sap=>false
* r_container =
* container_name =
IMPORTING
r_salv_table = DATA(lo_alv)
CHANGING
t_table = it_return.
CATCH cx_salv_msg.
ENDTRY.
lo_alv->display( ).
Uses SALV (Simple ALV) to show return messages (it_return) on the screen.
Helpful for debugging and seeing which records succeeded or failed.
Thank you!
Sandeep