Blog 2: Turn Dead Code into a Live Uploader
ZTEST-001. Today, we are going to make it smart. We will teach it to read a file and upload multiple records at once using the Session Method.
We need to change three things in our code: read a file, loop through the data, and save it to a batch input session instead of executing it immediately.
Step 1: Prepare Your Data File
We need a file for the program to read. To keep it simple, we will use a text file where fields are separated by a pipe character (|).
Click below to download a simple text file with 2 rows of data.
⬇ Download Data File
(Creator Note: Replace the # with your actual text file link).
C:\Users\YourName\Desktop\bdc_data.txt). You will need this path in the next step.
Step 2: Update Your ABAP Code
Open the program you generated in Blog 1 (Z_MY_FIRST_BDC_PROG) using transaction SE38. We need to replace the hardcoded logic with dynamic logic.
Below is the complete code. Copy and paste it into your program, but you must change one line!
*&---------------------------------------------------------------------*
*& Report Z_MY_FIRST_BDC_PROG
*&---------------------------------------------------------------------*
REPORT z_my_first_bdc_prog.
* 1. Define the structure to hold our data
TYPES: BEGIN OF ty_data,
matnr TYPE char18,
maktx TYPE char40,
END OF ty_data.
DATA: lt_data TYPE TABLE OF ty_data,
ls_data TYPE ty_data.
DATA: gt_bdcdata TYPE TABLE OF bdcdata,
gs_bdcdata TYPE bdcdata.
* Variables for file upload
DATA: lv_file_path TYPE string.
DATA: lt_raw_data TYPE TABLE OF string,
ls_raw_data TYPE string.
DATA: lt_split TYPE TABLE OF string.
* Variables for the Session
DATA: lv_session_name TYPE apqi-groupid VALUE 'Z_MY_SESSION_01'.
START-OF-SELECTION.
" *** CHANGE THIS PATH TO WHERE YOU SAVED THE FILE ON YOUR DESKTOP ***
lv_file_path = 'C:\Users\YourName\Desktop\bdc_data.txt'.
" Read the file
CALL METHOD cl_gui_frontend_services=>gui_upload
EXPORTING
filename = lv_file_path
filetype = 'ASC'
CHANGING
data_tab = lt_raw_data
EXCEPTIONS
file_open_error = 1
OTHERS = 2.
IF sy-subrc <> 0.
WRITE: / 'Error reading file. Check the path!'.
EXIT.
ENDIF.
" Parse the data
LOOP AT lt_raw_data INTO ls_raw_data.
SPLIT ls_raw_data AT '|' INTO TABLE lt_split.
IF lines( lt_split ) >= 2.
ls_data-matnr = lt_split[ 1 ].
ls_data-maktx = lt_split[ 2 ].
APPEND ls_data TO lt_data.
ENDIF.
CLEAR lt_split.
ENDLOOP.
" Open the Batch Input Session
CALL FUNCTION 'BDC_OPEN_GROUP'
EXPORTING
client = sy-mandt
group = lv_session_name
keep = 'X'
EXCEPTIONS
group_exists = 1
OTHERS = 2.
" Loop through the parsed data and insert into session
LOOP AT lt_data INTO ls_data.
CLEAR gt_bdcdata.
" Screen 1 (Initial Screen)
PERFORM bdc_dynpro USING 'SAPLMGMM' '0060'.
PERFORM bdc_field USING 'RMMG1-MATNR' ls_data-matnr.
PERFORM bdc_field USING 'RMMG1-MBRSH' 'M'.
PERFORM bdc_field USING 'RMMG1-MTART' 'FERT'.
PERFORM bdc_field USING 'BDC_OKCODE' '/00'.
" You will need to add the other screens (Views, Basic Data 1) here
" based on your recording from Blog 1!
" Example:
* PERFORM bdc_dynpro USING 'SAPLMGMM' '0070'.
* PERFORM bdc_field USING 'BDC_OKCODE' '=ENTR'.
" Insert into session
CALL FUNCTION 'BDC_INSERT'
EXPORTING
tcode = 'MM01'
TABLES
dynprotab = gt_bdcdata.
ENDLOOP.
" Close the Session
CALL FUNCTION 'BDC_CLOSE_GROUP'.
WRITE: / 'Session created successfully! Go to SM35 to run it.'.
' ' around your file path. Fix the error and activate again.
Step 3: Run It and Check SM35
Go back to SE38, type your program name, and click Execute (F8).
If everything worked, you will see a message on the screen: “Session created successfully! Go to SM35 to run it.”
[INSERT YOUR SM35 SCREENSHOT HERE]
Show the session with 2 entries.
Do not click “Process” yet! We are saving that for Blog 3, where we will break it on purpose.
Revision: Let’s Understand the Magic
Click the questions below to check your understanding.
Why did we use BDC_OPEN_GROUP and BDC_CLOSE_GROUP?
BDC_OPEN_GROUP creates the empty folder. BDC_INSERT puts paper (your data) into the folder. BDC_CLOSE_GROUP closes the folder and puts it on the shelf (SM35) so you can process it later. This is called the Session Method.
What is GUI_UPLOAD doing?
GUI_UPLOAD grabs the file from your PC and pulls it into SAP’s memory (an internal table).
What’s Next?
Right now, your Session is waiting in SM35. You haven’t actually created the materials yet! In Blog 3, we are going to deliberately put a mistake in our text file, run the session, watch it fail, and then learn how to read the error logs to fix it.
Homework: Do NOT run your session yet. Leave it sitting in SM35!
WhatsApp us