In this blog post I will explain in detail the Create method which is generated by the ABAP BAPI BO Class Code Generator tool.
This blog post is a follow-up blog post of the blog post ABAP BAPI BO Class Generator.
The explaining of the Create method is done by example.
The example is creating a Sales Order – Create method based on BAPI BAPI_SALESORDER_CREATEFROMDAT2.
De explanation of this blog post is based on ABAP BAPI BO Class Generator v.1.2.
For more information of this version go to GitHub: https://github.com/alwinvandeput/abap_bapi_bo_class_generator
Filling BAPI on selection screen
- Start transaction SA38 to execute the ABAP program: ZAB_ABAP_BAPI_BO_CLASS_GEN_PRG.
- When you start the tool, the selection screen is almost empty looks like this.
- Click onthe button “Select business object”.
- Search for the BAPIs with main DB table: VBAK.
- Double click: BUS2032 – Sales Order – BAPI_SALESORDER_CREATEFROMDAT2.
- Because this demo is about the “Create BAPI” and not about the “Update BAPI”, we clear the field “Update BAPI (change)”
- Press Enter to refresh the selection screen.So only the check boxes related to the “Create BAPI” are active.
Business Object class – settings
The Business Object class settings are automatically filled in the previous step.
The settings are explained in blog post ABAP BAPI BO Class Code Generator – Business Object parameters. So for now we skip them.
Business Object class – methods
The methods “Get key and instance method” and the “Create Method” are checked now. So the methods GET_KEY, GET_INSTANCE_BO and CREATE_INSTANCE_BO will be created.
After generation you will see the methods in the class interface and class implementation in the ABAP Workbench (T: SE80). See the pictures below.
Picture: Class interface
Picture: Class implementation
The CREATE_INSTANCE_BO method is a static method. In OO principles static methods should only be used for getting an instance of an object. So it is properly used over here.
The method has one importing parameter and returns the reference to the created instance.
Another name for the importing parameter is the create (mehod) message.
Message data type
The importing parameter of the Create method can also be called a Message. The message type TCREATE_DATA_S is generated in the Class interface. The Message is actually a deep structure. So it is a structure containing element types, structure types and table types.
Based on the Importing, Changing and Tables parameters of the BAPI the message data type is generated.
The function module parameters are wrapped in one deep structure.
The advantage of using a deep structure as the method message instead of separate parameters is that the method call only needs one parameter.
See the method call in the picture below. The parameter is highlighted.
And if the create message is extended, than you don’t have to change parameters of the method.
Also the field mapping to the message can now easily be done in a separate method.
For example like:
DATA(_sales_order_crea_s) = map_create_data( ).
Copy method message parameter to local message variable
Before calling the BAPI the method message will be copied to a local message variable, because a Create BAPI might change the data of CHANGING and TABLES parameters. If the importing parameter is used, than the program will dump, because the BAPI cannot change the data.
Function Module call
See that the method message is mapped to the Importing and Tables parameters. The Exporting parameters will be mapped to local variables.
The return variable _RETURN_T is a local table variable, which will be used for the exception handling later on.
The error handling is done directly after calling the Create BAPI.
For the error handling of the BAPI RETURN messages, 3 steps are needed.
- BAPI RETURN variable is declared,
- BAPI RETURN variable is passed as parameter to the function module call,
- BAPI RETURN variable is handled to an exception class.
1. BAPI RETURN Variable declaration
2. BAPI RETURN variable added to function call
See variable _return_t
3. Class ZAB_RETURN_EXC handles the BAPI RETURN variable
If the table variable _return_t contains an message with type E, X or A, than the exception class will be instantiated by methode CREATE_BY_BAPIRET2_TABLE(). And if it is instantiated, than is will be raised with RAISE EXCEPTION.
Instantiation in method CREATE_INSTANCE_BO
The instantiation in methode CREATE_INSTANCE_BO is done after the error check.
Set key value
After the instantiation the key value is set.
The key is now based on the method message (= importing parameter), but I want to set the key generated by SAP, so I change it to a exporting parameter of the function module.
The methods “Instantiate call”, “Create object call” and “- Out comment data” are checked automatically. This will result in the test methods CREATE_INSTANCE_BO and GET_INSTANCE_BO in the test class.
Generated test data
The “Out comment data” means that the data is out commented as shown below.
You have to uncomment the data which is needed for the test.
The extra comment of the data type, element name, domain, name and length should be deleted.
The test class is instantiated in the LOAD-OF-PROGRAM program event and executed in the START-OF-SELECTION program event.
Activate the create test
By default the CREATE_INSTANCE_BO( ) is out commented. Remove the * to activate the method call.
Adjust test data
As already mentioned, all the data is out commented.
To help you also extra information is generated like data type (C, N, … ) the data element, domain, field length and value tables are added as comment.
Fill the test data…
Run the test
- Activate the code and press F8 to execute the program.
- If the result is OK, than you receive the message
- If it is not OK, than you receive the error messages, for example: