ℹ️ Select 'Choose Exercise', or randomize 'Next Random Exercise' in selected language.

Choose Exercise:
Timer 00:00
WPM --
Score --
Acc --
Correct chars --

ABAP Batch Input Session Creation

ABAP

Goal -- WPM

Ready
Exercise Algorithm Area
1REPORT z_batch_input_creation.
2
3TYPES:
4BEGIN OF ts_customer_data,
5customer_id TYPE kunnr,
6name TYPE name1,
7city TYPE ort01_l,
8country TYPE land1,
9END OF ts_customer_data.
10
11TYPES:
12tt_customer_data TYPE STANDARD TABLE OF ts_customer_data WITH EMPTY KEY.
13
14DATA:
15gt_customer_data TYPE tt_customer_data.
16gt_bdc_data TYPE STANDARD TABLE OF bdcdata.
17gs_bdc_data TYPE bdcdata.
18gv_session_name TYPE bdsession.
19gv_program_name TYPE progname.
20gv_subrc TYPE sy-subrc.
21
22START-OF-SELECTION.
23PERFORM get_customer_data.
24PERFORM create_batch_input_session.
25PERFORM schedule_batch_input_job.
26
27*&---------------------------------------------------------------------*
28*& Form GET_CUSTOMER_DATA
29*&---------------------------------------------------------------------*
30FORM get_customer_data.
31" Simulate fetching customer data that needs to be created.
32" In a real scenario, this would involve reading from a file or another table.
33gt_customer_data = VALUE #( (
34customer_id = 'CUST001' name = 'Alpha Corp' city = 'New York' country = 'US'
35) (
36customer_id = 'CUST002' name = 'Beta Ltd' city = 'London' country = 'GB'
37) (
38customer_id = 'CUST003' name = 'Gamma Inc' city = 'Paris' country = 'FR'
39) ).
40
41" Handle edge case: No data to process.
42IF gt_customer_data IS INITIAL.
43MESSAGE 'No customer data found for batch input processing.' TYPE 'I'.
44LEAVE LIST-SCREEN.
45ENDIF.
46ENDFORM.
47
48*&---------------------------------------------------------------------*
49*& Form CREATE_BATCH_INPUT_SESSION
50*&---------------------------------------------------------------------*
51FORM create_batch_input_session.
52" Define the transaction to be used for batch input.
53" For customer creation, typically 'XD01' or 'Create Customer' transaction.
54gv_program_name = 'SAPMF02D'. " Program for Customer Master (XD01)
55
56" Generate a unique session name.
57CONCATENATE sy-repid '_' sy-datum sy-uzeit INTO gv_session_name.
58REPLACE ALL OCCURRENCES OF '.' IN gv_session_name WITH ''.
59REPLACE ALL OCCURRENCES OF ':' IN gv_session_name WITH ''.
60
61LOOP AT gt_customer_data ASSIGNING FIELD-SYMBOL(<fs_customer>).
62" Add BDC data for creating a customer.
63" This involves simulating screen fields and their values.
64
65" Transaction screen: SAPMF02D 0100 (Customer: Initial Screen)
66CLEAR gs_bdc_data.
67gs_bdc_data-program = gv_program_name.
68gs_bdc_data-dynbegin = 'X'. " Start of a new screen
69APPEND gs_bdc_data TO gt_bdc_data.
70
71CLEAR gs_bdc_data.
72gs_bdc_data-program = gv_program_name.
73gs_bdc_data-dynscreen = '0100'.
74gs_bdc_data-fnam = 'R02D-KUNNR'. " Customer Number field
75gs_bdc_data-fval = <fs_customer>-customer_id.
76APPEND gs_bdc_data TO gt_bdc_data.
77
78CLEAR gs_bdc_data.
79gs_bdc_data-program = gv_program_name.
80gs_bdc_data-dynscreen = '0100'.
81gs_bdc_data-fnam = 'R02D-LAND1'. " Country field
82gs_bdc_data-fval = <fs_customer>-country.
83APPEND gs_bdc_data TO gt_bdc_data.
84
85CLEAR gs_bdc_data.
86gs_bdc_data-program = gv_program_name.
87gs_bdc_data-dynscreen = '0100'.
88gs_bdc_data-fnam = 'R02D-ORT01'. " City field
89gs_bdc_data-fval = <fs_customer>-city.
90APPEND gs_bdc_data TO gt_bdc_data.
91
92CLEAR gs_bdc_data.
93gs_bdc_data-program = gv_program_name.
94gs_bdc_data-dynscreen = '0100'.
95gs_bdc_data-fnam = 'R02D-NAME1'. " Name field
96gs_bdc_data-fval = <fs_customer>-name.
97APPEND gs_bdc_data TO gt_bdc_data.
98
99" Simulate pressing Enter or F8 to proceed to the next screen.
100CLEAR gs_bdc_data.
101gs_bdc_data-program = gv_program_name.
102gs_bdc_data-dynscreen = '0100'.
103gs_bdc_data-dyndyn = 'P+'. " Simulate Enter key
104APPEND gs_bdc_data TO gt_bdc_data.
105
106" Add more screens and fields as needed for the transaction.
107" For XD01, there are many subsequent screens for company code, sales area, etc.
108" This example simplifies by only showing the initial screen data.
109
110ENDLOOP.
111
112" Call the BDC session creation function module.
113CALL FUNCTION 'BD_OPEN_GROUP'
114EXPORTING
115group_name = gv_session_name
116EXCEPTIONS
117invalid_group_name = 1
118queue_not_empty = 2
119OTHERS = 3.
120
121IF sy-subrc <> 0.
122MESSAGE 'Error opening batch input session group.' TYPE 'E'.
123RETURN.
124ENDIF.
125
126CALL FUNCTION 'BDC_INSERT'
127EXPORTING
128tcode = 'XD01'
129program = gv_program_name
130TABLES
131dynprotab = gt_bdc_data
132EXCEPTIONS
133program_error = 1
134tcode_invalid = 2
135screen_does_not_exist = 3
136program_does_not_exist = 4
137OTHERS = 5.
138
139IF sy-subrc <> 0.
140MESSAGE 'Error inserting BDC data into session.' TYPE 'E'.
141CALL FUNCTION 'BD_CLOSE_GROUP'. " Close session on error
142RETURN.
143ENDIF.
144
145CALL FUNCTION 'BD_CLOSE_GROUP'.
146
147" Handle edge case: No BDC data generated.
148IF gt_bdc_data IS INITIAL.
149MESSAGE 'No BDC data was generated for the session.' TYPE 'I'.
150LEAVE LIST-SCREEN.
151ENDIF.
152
153ENDFORM.
154
155*&---------------------------------------------------------------------*
156*& Form SCHEDULE_BATCH_INPUT_JOB
157*&---------------------------------------------------------------------*
158FORM schedule_batch_input_job.
159" Schedule the batch input session to run as a background job.
160" This is optional; the session can also be run manually via SM35.
161
162" For simplicity, we'll just inform the user where to find the session.
163MESSAGE |Batch input session '{ gv_session_name }' created.| TYPE 'S'.
164MESSAGE 'Run transaction SM35 to process the session.' TYPE 'I'.
165
166" To schedule as a job, you would typically use:
167" CALL FUNCTION 'JOB_OPEN'...
168" CALL FUNCTION 'SET_JOB_SCHEDULING'...
169" CALL FUNCTION 'SUBMIT_JOB_RUN'...
170" CALL FUNCTION 'JOB_CLOSE'...
171" This requires more complex parameter handling for job definition.
172
173ENDFORM.
Algorithm description viewbox

ABAP Batch Input Session Creation

Algorithm description:

This ABAP program demonstrates the creation of a batch input session for processing customer master data. It first retrieves sample customer data. Then, for each customer, it constructs BDC (Batch Data Communication) data, simulating the input fields and values for the `XD01` transaction. The `BDC_INSERT` function module is used to add this data to a batch input session. Finally, it informs the user where to find and process the created session (transaction `SM35`). This is useful for mass data uploads or updates.

Algorithm explanation:

Batch input is a powerful tool for automating repetitive transactions in SAP. This program uses the `BDC_INSERT` function module, which requires a table of `BDCDATA` structures. Each `BDCDATA` entry maps a program, screen number, field name, and its value. The `DYNBEGIN` flag indicates the start of a new screen, and `DYNNR` specifies the screen number. `DYNPRO` is the program name. `FNAM` is the field name, and `FVAL` is the value to be entered. The program simulates the initial screen of `XD01` and populates the customer number, country, city, and name fields. The time complexity for generating BDC data is O(N*S), where N is the number of records and S is the number of screens per transaction. Session creation and insertion are generally efficient. Space complexity is O(N*F) where F is the number of fields per screen. Edge cases include no data to process and errors during session creation or data insertion.

Pseudocode:

1. Define a structure `ts_customer_data` for customer information.
2. Define a standard internal table type `tt_customer_data`.
3. Declare an internal table `gt_customer_data` of this type.
4. Declare a table `gt_bdc_data` of type `BDCDATA` and a work area `gs_bdc_data`.
5. Declare variables for session name and program name.
6. In `GET_CUSTOMER_DATA` form:
   a. Populate `gt_customer_data` with sample customer records.
   b. If `gt_customer_data` is empty, display a message and exit.
7. In `CREATE_BATCH_INPUT_SESSION` form:
   a. Set the target transaction program name (e.g., 'SAPMF02D' for XD01).
   b. Generate a unique batch input session name.
   c. Loop through `gt_customer_data`:
      i. For each customer, add `BDCDATA` entries for the transaction screens:
         - Set `DYNBEGIN` to 'X' for the first screen.
         - Specify `PROGRAM`, `DYNSCREEN`, `FNAM`, and `FVAL` for each field.
         - Simulate pressing Enter/PF-key using `DYNDYN`.
      ii. Append each `BDCDATA` entry to `gt_bdc_data`.
   d. Call `BD_OPEN_GROUP` to open the session.
   e. Call `BDC_INSERT` to add the generated `gt_bdc_data` to the session.
   f. Call `BD_CLOSE_GROUP` to close the session.
   g. Handle errors during session creation and data insertion.
   h. If no BDC data was generated, display a message and exit.
8. In `SCHEDULE_BATCH_INPUT_JOB` form:
   a. Inform the user about the created session name and how to run it (SM35).
   b. (Optional) Outline steps for scheduling as a background job using function modules like `JOB_OPEN`.