Recently I needed a program that would display a screen for maintaining Characteristics and Classifications following...
an SAP COMMIT WORK statement when maintaining a few master data objects, i.e., after the "save" processing when the object is changed or created.
Anyway, I came across a not very well-known technique which helped me solve this real-life programming problem rather neatly. I won't go into the details of the application. What I want to focus on is how to have an action triggered following a successful COMMIT WORK in an SAP transaction.
More information on SAP application development
Get tips and advice on SAP ABAP from Matthew Billingham.
Learn about how to find a piece of SAP ABAP code without debugging.
Read about how to use subclasses to develop applications running on different versions of SAP.
For my master data object, there is an SAP BADI which runs immediately following a save, but before the COMMIT WORK. There I called a Static Factory Method, in an SAP ABAP class I had developed (ZCL_MYCLASS), which follows the Singleton pattern.
The master data fields I needed for my application, were passed in the parameters of this method, and set in the attributes of the Singleton instance. (You could use global variables in a Function Group with a few Function Modules to achieve the same results.) The last line of ABAP within the BADI is:
set handler zcl_myclass=>tx_finished.
In my ABAP class, I define the Static Handler method tx_finished. This method, when triggered, calls additional processing for the maintenance of Characteristics.
class zcl_myclass definition. public section. Class-methods: tx_finished for event transaction_finished of cl_system_transaction_state importing kind, ... " further methods for handling the Singleton and the application endclass. " z_clmyclass DEFINITION class zcl_myclass implementation. method tx_finished. " Switch off the handler set handler post_commit_handler=>trans_finished activation false. " check value of importing parameter "kind" if kind ne cl_system_transaction_state=>commit_work. " successful commit exit. endif. " do the additional application processing using the singleton zcl_myclass=>r_singleton->maintain_chars( ). endmethod. " tx_finished " implementation of remaining methods endclass. " z_clmyclass IMPLEMENTATION
Following all the processing involved within COMMIT -- or ROLLBACK -- the system event CL_SYSTEM_TRANSACTION_STATE=>TRANSACTION_FINISHED is raised, which triggers any handler methods for that event. All of this happens within one session, so there is no danger of interference from other processes on the system.
To summarize: In the BADI, I pass relevant data to my class, and set a handler. Following the COMMIT WORK, the handler method runs in the same dialog task and the same memory space, launching my "post save" functionality, which can involve further screens and interaction with the user.
About the creator of this tip: Matthew Billingham is SearchSAP.com's application development expert. He has worked for companies such as Eurotunnel and Vodafone, served as development manager for Coats PLc, and as head of application development with Novartis Pharma in Switzerland, managing the development side of a global SAP rollout. He now runs his own SAP consultancy, providing guidance and designing solutions (particularly for intractable problems) for third parties. His expertise is in program design, troubleshooting and knowing just how SAP works; his clients describe him as "Consultant for everything." He is co-founder of SecureBI, delivering SAP security solutions.