Chapter 6: Automated Document Reversal
In the previous chapter, we witnessed the most exciting part of our process: our Financial Document Posting Engine took a temporary "noted item" and created a real, official accounting document. The job is done, right? Not quite.
The Problem: Forgetting to Void the Cheque
Imagine you have a cheque from a customer. You take it to the bank and cash it, and the money is now in your account. What do you do with the original paper cheque? You certainly don't put it back in your wallet, or you might accidentally try to cash it again next week! The bank voids the cheque so it can never be used again.
Our noted item is exactly like that cheque. Now that we've "cashed" it by creating a real financial document, we have a critical cleanup task: we must reverse the original noted item to ensure it's never accidentally processed again. This prevents duplicate postings and keeps our financial records clean.
The Solution: A High-Speed Robot User (reverse_doc)
How do we reverse a document in SAP? A user would typically go to a specific screen, called a "transaction" (in this case, transaction FB08), type in the document number, and click "Save."
Our program needs to do this automatically. For this, we use a classic and powerful technique called Batch Data Communication (BDC).
Think of BDC as creating a high-speed robot that perfectly mimics a human user. We can record a user's actions once (like a macro) and then tell our program to "play back" those actions thousands of times, filling in different data each time.
The reverse_doc method is our implementation of this robot. Its sole purpose is to take the details of a noted item and use BDC to automatically run transaction FB08 to reverse it.
Step 1: Receiving the Reversal Order
After the PDC Processing Orchestrator gets a success confirmation from the posting engine, it immediately calls our reversal robot, reverse_doc, and hands it the noted item that needs to be voided.
* Inside the Orchestrator's PROCESS method
" If the posting was successful...
IF is_cust1-belnr IS NOT INITIAL.
" ...then immediately tell the robot to reverse the original noted item.
reverse_doc( is_cust1 ).
ENDIF.
Step 2: Giving the Robot Its Instructions
The core of BDC is building a list of step-by-step instructions for our robot. Each instruction tells the robot which screen to go to, which field to fill, and what to type.
Instruction 1: Fill the first screen of transaction FB08.
We need to tell the robot to fill in three key pieces of information and the reversal reason.
* Inside the REVERSE_DOC method
" Instruction for the Document Number field
PERFORM bdc_field USING 'BKPF-BELNR' is_cust1-o_belnr.
" Instruction for the Company Code field
PERFORM bdc_field USING 'BKPF-BUKRS' is_cust1-bukrs.
" Instruction for the Fiscal Year field
PERFORM bdc_field USING 'BKPF-GJAHR' is_cust1-gjahr.
PERFORM bdc_fieldis a helper that adds one instruction to our list.'BKPF-BELNR'is the technical name for the "Document Number" field on the screen.is_cust1-o_belnris the variable holding the actual document number we want to reverse.
Instruction 2: "Press" the Save button.
After filling the fields, a user would click "Save." We give our robot an instruction to do the same thing by providing the technical code for the save button.
* Inside the REVERSE_DOC method
" Instruction to 'press' the save button
PERFORM bdc_field USING 'BDC_OKCODE' '/11'.
BDC_OKCODEis the special field name for user actions./11is the system's internal code for "Save."
Step 3: Running the Robot
Once our list of instructions is complete, we use a single command, CALL TRANSACTION, to execute it. This command tells SAP, "Take this list of instructions and run transaction FB08 in the background, without showing anything on the screen."
* Inside the REVERSE_DOC method
" Execute transaction FB08 using our list of instructions
CALL TRANSACTION 'FB08' USING it_bdcdata " Our list of instructions
MODE 'N' " 'N' = No screen display
UPDATE 'S'. " 'S' = Synchronous update
USING it_bdcdata: This passes our list of robot instructions.MODE 'N': This is what makes it so fast. The robot works invisibly in the background.
Step 4: Checking the Result
Just like a real user, our robot might encounter a success message ("Document was reversed") or an error ("Reversal is not allowed"). The CALL TRANSACTION command captures these messages for us. We can then check them to confirm the job was done.
Under the Hood: The BDC Flow
Let's visualize how our orchestrator uses the reverse_doc method to perform this automated reversal.
This process is incredibly efficient. While it might take a user several seconds to manually reverse one document, our BDC robot can do it in a fraction of a second, allowing us to process hundreds of items in just a few minutes.
Conclusion
Congratulations! You have now mastered the final, crucial step in our PDC processing workflow. The Automated Document Reversal ensures that our books are kept clean and prevents dangerous duplicate postings.
We learned that:
- Reversing a posted noted item is a critical cleanup step, like voiding a cashed cheque.
- Batch Data Communication (BDC) is a powerful technique for automating user actions by creating a high-speed "robot" that mimics screen navigation and data entry.
- The
reverse_docmethod builds a list of instructions for this robot to execute transactionFB08. - The
CALL TRANSACTIONcommand runs this robot invisibly and at high speed.
You have now seen the entire lifecycle of a noted item within our application: from the smart User Input and Screen Logic to displaying the data in the Interactive ALV Report, all coordinated by the PDC Processing Orchestrator. You've seen how we retrieve the data, post the new document, and finally, reverse the original. You are now familiar with all the core components of the PDC project