BAPI Part 7: Test Run, Simulation and Safe BAPI Execution
Validate before posting: BAPIs with TESTRUN parameters (BAPI GOODSMVT CREATE, BAPI PO CREATE1, BAPI SALESORDER CREATEFROMDAT2), check BAPIs like BAPI ACC DOCUMENT CHECK, simulate-then-post patterns in upload programs, and why a test run must always end with a rollback.
Explanation
📌 KEY POINTS
✅ Many BAPIs offer a TESTRUN (or SIMULATION) parameter: they run all checks and return messages, but must not save.
✅ Some areas provide separate check BAPIs, e.g. BAPI_ACC_DOCUMENT_CHECK before BAPI_ACC_DOCUMENT_POST.
✅ After a test run, call BAPI_TRANSACTION_ROLLBACK to discard anything registered and release locks.
⚠️ A successful test run is not a guarantee: data can change between simulation and posting (stock, credit, locks).
━━━━━━━━━━━━━━━━━━━━
▶ WHERE WE ARE
Parts 2–6 posted documents directly. For uploads and critical interfaces, users often want to see errors first.
━━━━━━━━━━━━━━━━━━━━
▶ 1. WHICH BAPIS SUPPORT IT
- BAPI_GOODSMVT_CREATE — parameter TESTRUN.
- BAPI_PO_CREATE1 — TESTRUN.
- BAPI_SALESORDER_CREATEFROMDAT2 — TESTRUN (checks, no document).
- BAPI_ACC_DOCUMENT_CHECK — separate check BAPI for FI postings.
Check the documentation: parameter names and behaviour differ.
━━━━━━━━━━━━━━━━━━━━
▶ 2. SIMULATE-THEN-POST PATTERN
- Read the file, map data (Part 6).
- Run each document with TESTRUN = 'X'; collect RETURN; ROLLBACK after each.
- Show the results (ALV with errors per row); user corrects the file or confirms.
- Real run: call without TESTRUN, commit per document, log results.
━━━━━━━━━━━━━━━━━━━━
▶ 3. WHY ROLLBACK AFTER TEST RUN
Some BAPIs register data or set locks even in test mode. A rollback ensures a clean state for the next call.
━━━━━━━━━━━━━━━━━━━━
▶ 4. LIMITS OF SIMULATION
- Number assignment, stock and credit checks may differ at posting time.
- Output, workflow and follow-on processes don't run.
- Some checks happen only in the update task.
So the real run still needs full RETURN evaluation.
━━━━━━━━━━━━━━━━━━━━
▶ 5. SAFE EXECUTION CHECKLIST
- Test mode as default in upload programs (radio button: Test / Update).
- Authorization check before the update run.
- Idempotency (Part 9) to prevent duplicates on rerun.
- Logging of both runs.
━━━━━━━━━━━━━━━━━━━━
▶ STEP BY STEP
- Run the code example in test mode with valid and invalid rows.
- Check MKPF: no documents after the test run.
- Switch to update mode and compare results.
✅ CHECKPOINT: you can build upload programs with a safe test run and a controlled update run.
Code example
REPORT zbapi_gm_upload. PARAMETERS: p_test RADIOBUTTON GROUP r DEFAULT 'X', " test run is the default p_upd RADIOBUTTON GROUP r. DATA: ls_header TYPE bapi2017_gm_head_01, ls_code TYPE bapi2017_gm_code, lt_items TYPE STANDARD TABLE OF bapi2017_gm_item_create, lt_return TYPE STANDARD TABLE OF bapiret2, ls_return TYPE bapiret2, lv_testrun TYPE bapi2017_gm_gen-testrun, lv_mblnr TYPE mblnr, lv_error TYPE abap_bool. START-OF-SELECTION. PERFORM fill_document. " header + items from the file (not shown) IF p_test = 'X'. lv_testrun = 'X'. ELSE. AUTHORITY-CHECK OBJECT 'M_MSEG_BWA' ID 'ACTVT' FIELD '01' ID 'BWART' FIELD '501'. IF sy-subrc <> 0. MESSAGE 'No authorization for posting' TYPE 'E'. ENDIF. ENDIF. CALL FUNCTION 'BAPI_GOODSMVT_CREATE' EXPORTING goodsmvt_header = ls_header goodsmvt_code = ls_code testrun = lv_testrun " 'X' = checks only IMPORTING materialdocument = lv_mblnr TABLES goodsmvt_item = lt_items return = lt_return. LOOP AT lt_return INTO ls_return WHERE type CA 'EA'. lv_error = abap_true. WRITE: / ls_return-message. ENDLOOP. IF p_test = 'X' OR lv_error = abap_true. CALL FUNCTION 'BAPI_TRANSACTION_ROLLBACK'. " always after a test run IF lv_error = abap_false. WRITE: / 'Test run OK - no document created'. ENDIF. ELSE. CALL FUNCTION 'BAPI_TRANSACTION_COMMIT' EXPORTING wait = 'X'. WRITE: / 'Posted material document', lv_mblnr. ENDIF. FORM fill_document. ls_header-pstng_date = sy-datum. ls_code-gm_code = '05'.ENDFORM. * ===== FI: check before post =====* CALL FUNCTION 'BAPI_ACC_DOCUMENT_CHECK' EXPORTING documentheader = ls_head* TABLES accountgl = lt_gl currencyamount = lt_amt return = lt_return.* -> only if no E/A: BAPI_ACC_DOCUMENT_POST + BAPI_TRANSACTION_COMMITReal project scenario
A plant uploaded monthly inventory adjustments from Excel. The first version posted directly; a file with a wrong storage location created 400 wrong material documents that had to be reversed.
The new program ran a test run by default and showed an ALV with results per row. Users corrected files before posting, and reversals dropped to almost zero. The update run also logged results for audit.
Common mistakes
- No rollback after a test run.
- Treating a successful test run as guaranteed success.
- Upload programs that post by default.
- Different code paths for test and update (results differ).
- Missing authorization check before the update run.
Best practices
- Test run as default with clear result display.
- Same mapping and call for test and update, only the flag differs.
- Rollback after every test call.
- Full RETURN evaluation in the real run too.
- Log both runs with user and file reference.
Interview angle
Questions: "How do you simulate a BAPI posting?", "Why roll back after TESTRUN?", "Is a successful test run a guarantee?"
Strong answer: TESTRUN parameter in BAPIs like BAPI_GOODSMVT_CREATE/BAPI_PO_CREATE1/CREATEFROMDAT2 or check BAPIs like BAPI_ACC_DOCUMENT_CHECK; rollback afterwards to discard registrations/locks; simulate-then-post pattern with result ALV; not a guarantee because data changes and some checks happen at update; evaluate RETURN again in the real run.
Practice this lesson
Quick check
1. What must follow a BAPI call made with TESTRUN = 'X'?
2. Which BAPI checks an FI document without posting it?
3. Why is a successful test run not a guarantee for the real posting?
Interview questions
1. An upload program claimed to offer a simulation mode by calling the real BAPI and simply not committing. Is this safe? What would you do instead?
Answer it in your own words first, then compare.
2. Difference between BAPI and RFC.
Answer it in your own words first, then compare.
Reference-verified: the steps are checked against official SAP documentation and tutorials, and object and field names in the sample code are checked for consistency. The code has not been compiled or run on an SAP system by ERPClimb. Syntax marked release-dependent varies by SAP release, so check it in your own system.
More lessons in this topic
- BAPI Part 1: What a BAPI Is and How to Find One
- BAPI Part 10: "Success but No Document" — Debugging BAPI Problems
- BAPI Part 2: Calling a BAPI — Inputs, X Structures, RETURN and Commit
- BAPI Part 3: BAPI vs RFC vs OData
- BAPI Part 4: Understanding BAPI RETURN Messages
- BAPI Part 5: Commit, WAIT, Rollback and the SAP LUW
- BAPI Part 6: Mapping Header, Item, Partner and Schedule Data
- BAPI Part 8: EXTENSIONIN — Saving Custom Fields with BAPIs