ERPClimb logoERPClimb
User Exits
ABAP Developmentadvanced 6 min read

User Exits Part 7: Safe Coding Rules Inside User Exits

Preview · a free account unlocks the full lesson

Key takeaway

Protect standard processes when coding in user exits: guard conditions, transaction type and processing mode checks, correct message types, no COMMIT/ROLLBACK/CALL TRANSACTION, no popups in background, careful handling of global data, namespaces for own variables and code organisation in Z includes.

In this lessonShow sections

Visual overview

User Exits Part 7: Safe Coding Rules Inside User Exits — learning sheetFlow: Review one existing exit against this checklist → Move its logic into a Z include or class with a header comment → Add a guard and test in dialog and via BAPI. Key ideas: GUARD FIRST; TRANSACTIONAL SAFETY; MESSAGES; GLOBAL DATA. Watch out: Exits run in dialog, background, BAPIs and IDocs — a popup or a type 'A' message can stop a whole interface run.ERPClimb.com / ABAP / LESSON 7/8 / LEARNING SHEETUser Exits Part 7: Safe Coding Rules Inside User Exits1 · HOW IT FLOWS1Review one existing exitagainst this checklist2Move its logic into a Zinclude or class with aheader comment3Add a guard and test indialog and via BAPI2 · KEY IDEAS EXPLAINEDGuard FirstCHECK vbak-auart = 'ZOR'. or IF … RETURN.CHECK t180-trtyp <> 'A'. (no logic indisplay)Transactional SafetySAP commits the document itself. Own DB writes →update function modules IN UPDATE TASK or PERFORM… ON COMMIT.No CALL TRANSACTION / BAPIs that commit / RFC withimplicit commit inside save exits.MessagesE in checking exits (e.g. SAVE_DOCUMENT_PREPARE) —stops save, BAPI returns it.W/I only when the process can handle it; avoid inbackground.Global DataChange only fields the exit is intended to change.Don't modify SAP internal tables used for control(except documented X tables with UPDKZ).3 · REMEMBER · WATCH OUTRememberEvery exit starts with guard conditions: document type, org unit,transaction type (T180-TRTYP), processing mode.Never COMMIT WORK, ROLLBACK WORK, CALL TRANSACTION, SUBMIT … ANDRETURN with commits, or leave the program inside an exit.Declare own global variables with a ZZ prefix in the customerdeclaration includes and reset them when a new document starts.!Watch outExits run in dialog, background,BAPIs and IDocs — a popup or a type'A' message can stop a wholeinterface run.4 · IN A REAL PROJECTAn EDI interface creating 5,000 orders per night started failing after a change: a new check in MV45AFZZ usedPOPUP_TO_CONFIRM to ask users about a price deviation. In the BAPI path there was no user, and the IDocprocessing ended with errors for every affected order. The check was changed to an error message with a clear…Original ERPClimb learning sheet • Verify against your SAP release and edition

Explanation

Parts 4 through 6 of this series showed what to do inside specific exits; this lesson is the general rule set that applies to any exit.

Every exit should start with guard conditions, for example CHECK vbak-auart = 'ZOR'. or an IF … RETURN, and CHECK t180-trtyp <> 'A'. to keep logic out of display mode. Check the call context when it matters, too: sy-tcode is unreliable for BAPIs, so use call_bapi/call_activity flags where the program provides them, or check sy-batch instead.

Transactional safety matters because SAP commits the document itself. Any of your own database writes should go through update function modules called IN UPDATE TASK, or through PERFORM … ON COMMIT — never through CALL TRANSACTION, a BAPI that commits, or an RFC with an implicit commit placed inside save exits.

On messages: an E message in a checking exit (such as SAVE_DOCUMENT_PREPARE) stops the save, and a BAPI returns it as an error; W or I messages should only be used when the process can actually handle them, and should be avoided in background processing; A and X messages should never be used in exits; and POPUP_TO_CONFIRM should never appear when sy-batch = 'X' or inside BAPI calls.

Free account

Create a free account to keep reading

You have read the opening of this lesson. A free ERPClimb account unlocks the rest of it, every other lesson and topic, and your learning history, so you can pick up exactly where you stopped. No card needed.

  • Full explanation
  • Code example
  • Real project scenario
  • Common mistakes
  • Best practices
  • Interview angle

One tap with Google works too. Every topic keeps its first lesson open to everyone.

Keep exploring

Referenced SAP objects

Reviewed reference pages for the objects this page mentions.

Check before use: lesson examples are educational, not a guarantee of correctness for your SAP edition or release. Sample code has not been executed on your SAP system. Confirm API availability, syntax, authorizations and business impact in an approved development or test environment.