Understanding BAPI RETURN Messages
Learn how to read BAPIRET2 messages and decide whether to commit or rollback.
Explanation
Most BAPIs return messages in BAPIRET2 or a similar RETURN structure/table. The RETURN table is more important than sy-subrc because the BAPI may technically execute but still return business errors. Message type E or A usually means error or abort. W means warning, I means information and S means success. In upload programs and interfaces, every row should collect BAPI messages so users can correct data. A common production issue happens when developers commit even though the BAPI returned an error message.
Code example
* Purpose:* Check BAPI RETURN table correctly.* sy-subrc only tells whether function module call technically happened.* Business success must be checked from RETURN messages. DATA lv_has_error TYPE abap_bool. LOOP AT lt_return INTO DATA(ls_return). * Type E = Error, A = Abort. * These should normally stop commit. IF ls_return-type = 'E' OR ls_return-type = 'A'. lv_has_error = abap_true. ENDIF. * Log every message for user/support analysis APPEND VALUE #( type = ls_return-type id = ls_return-id number = ls_return-number message = ls_return-message ) TO lt_log. ENDLOOP. IF lv_has_error = abap_true. CALL FUNCTION 'BAPI_TRANSACTION_ROLLBACK'.ELSE. CALL FUNCTION 'BAPI_TRANSACTION_COMMIT' EXPORTING wait = abap_true.ENDIF.Real project scenario
A material creation upload showed technical success because sy-subrc was 0, but RETURN contained errors for invalid material groups. Proper RETURN logging revealed the real issue.
Common mistakes
- Checking only sy-subrc. - Ignoring warnings and information messages. - Not logging message ID and number. - Committing even after error messages.
Best practices
- Check RETURN type E and A before commit. - Log all RETURN messages. - Use rollback on error. - Show row-wise messages in upload/interface reports.
Interview angle
Interviewers often ask why BAPI returned sy-subrc 0 but document was not created. The answer is usually RETURN table handling.