Thursday 10 October 2019

ABAP BAPI BO Class Code Generator – Create method

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.

ABAP Development, SAP ABAP Tutorial and Materials, SAP ABAP Learning, SAP ABAP Guides, SAP ABAP Online Exam

ABAP Development, SAP ABAP Tutorial and Materials, SAP ABAP Learning, SAP ABAP Guides, SAP ABAP Online Exam
ABAP Development, SAP ABAP Tutorial and Materials, SAP ABAP Learning, SAP ABAP Guides, SAP ABAP Online Exam

ABAP Development, SAP ABAP Tutorial and Materials, SAP ABAP Learning, SAP ABAP Guides, SAP ABAP Online Exam

Click onthe button “Select business object”.

ABAP Development, SAP ABAP Tutorial and Materials, SAP ABAP Learning, SAP ABAP Guides, SAP ABAP Online Exam

Search for the BAPIs with main DB table: VBAK.

ABAP Development, SAP ABAP Tutorial and Materials, SAP ABAP Learning, SAP ABAP Guides, SAP ABAP Online Exam

Double click: BUS2032 – Sales Order – BAPI_SALESORDER_CREATEFROMDAT2.

BAPI


ABAP Development, SAP ABAP Tutorial and Materials, SAP ABAP Learning, SAP ABAP Guides, SAP ABAP Online Exam

◈ 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.

ABAP Development, SAP ABAP Tutorial and Materials, SAP ABAP Learning, SAP ABAP Guides, SAP ABAP Online Exam

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.

ABAP Development, SAP ABAP Tutorial and Materials, SAP ABAP Learning, SAP ABAP Guides, SAP ABAP Online Exam

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

ABAP Development, SAP ABAP Tutorial and Materials, SAP ABAP Learning, SAP ABAP Guides, SAP ABAP Online Exam

Picture: Class implementation

ABAP Development, SAP ABAP Tutorial and Materials, SAP ABAP Learning, SAP ABAP Guides, SAP ABAP Online Exam

Create method


Definition


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.

ABAP Development, SAP ABAP Tutorial and Materials, SAP ABAP Learning, SAP ABAP Guides, SAP ABAP Online Exam

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.

ABAP Development, SAP ABAP Tutorial and Materials, SAP ABAP Learning, SAP ABAP Guides, SAP ABAP Online Exam

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.

ABAP Development, SAP ABAP Tutorial and Materials, SAP ABAP Learning, SAP ABAP Guides, SAP ABAP Online Exam

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.

ABAP Development, SAP ABAP Tutorial and Materials, SAP ABAP Learning, SAP ABAP Guides, SAP ABAP Online Exam

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( ).

Method implementationCopy method message parameter to local message variable


ABAP Development, SAP ABAP Tutorial and Materials, SAP ABAP Learning, SAP ABAP Guides, SAP ABAP Online Exam

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.

ABAP Development, SAP ABAP Tutorial and Materials, SAP ABAP Learning, SAP ABAP Guides, SAP ABAP Online Exam

Error handling


The error handling is done directly after calling the Create BAPI.

ABAP Development, SAP ABAP Tutorial and Materials, SAP ABAP Learning, SAP ABAP Guides, SAP ABAP Online Exam

For the error handling of the BAPI RETURN messages, 3 steps are needed.

1. BAPI RETURN variable is declared,
2. BAPI RETURN variable is passed as parameter to the function module call,
3. BAPI RETURN variable is handled to an exception class.

1. BAPI RETURN Variable declaration

ABAP Development, SAP ABAP Tutorial and Materials, SAP ABAP Learning, SAP ABAP Guides, SAP ABAP Online Exam

2. BAPI RETURN variable added to function call

ABAP Development, SAP ABAP Tutorial and Materials, SAP ABAP Learning, SAP ABAP Guides, SAP ABAP Online Exam

See variable _return_t

3. Class ZAB_RETURN_EXC handles the BAPI RETURN variable

ABAP Development, SAP ABAP Tutorial and Materials, SAP ABAP Learning, SAP ABAP Guides, SAP ABAP Online Exam

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.

ABAP Development, SAP ABAP Tutorial and Materials, SAP ABAP Learning, SAP ABAP Guides, SAP ABAP Online Exam

Set key value


After the instantiation the key value is set.

ABAP Development, SAP ABAP Tutorial and Materials, SAP ABAP Learning, SAP ABAP Guides, SAP ABAP Online Exam

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.

ABAP Development, SAP ABAP Tutorial and Materials, SAP ABAP Learning, SAP ABAP Guides, SAP ABAP Online Exam

Test program


ABAP Development, SAP ABAP Tutorial and Materials, SAP ABAP Learning, SAP ABAP Guides, SAP ABAP Online Exam

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.

ABAP Development, SAP ABAP Tutorial and Materials, SAP ABAP Learning, SAP ABAP Guides, SAP ABAP Online Exam

Generated test data


The “Out comment data” means that the data is out commented as shown below.

ABAP Development, SAP ABAP Tutorial and Materials, SAP ABAP Learning, SAP ABAP Guides, SAP ABAP Online Exam

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.

Test class


Start


The test class is instantiated in the LOAD-OF-PROGRAM program event and executed in the START-OF-SELECTION program event

ABAP Development, SAP ABAP Tutorial and Materials, SAP ABAP Learning, SAP ABAP Guides, SAP ABAP Online Exam

Activate the create test


By default the CREATE_INSTANCE_BO( ) is out commented. Remove the * to activate the method call.

ABAP Development, SAP ABAP Tutorial and Materials, SAP ABAP Learning, SAP ABAP Guides, SAP ABAP Online Exam

Adjust test data


As already mentioned, all the data is out commented.

ABAP Development, SAP ABAP Tutorial and Materials, SAP ABAP Learning, SAP ABAP Guides, SAP ABAP Online Exam

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…

ABAP Development, SAP ABAP Tutorial and Materials, SAP ABAP Learning, SAP ABAP Guides, SAP ABAP Online Exam

Run the test


◈ Activate the code and press F8 to execute the program.
◈ If the result is OK, than you receive the message

ABAP Development, SAP ABAP Tutorial and Materials, SAP ABAP Learning, SAP ABAP Guides, SAP ABAP Online Exam

◈ If it is not OK, than you receive the error messages, for example:

ABAP Development, SAP ABAP Tutorial and Materials, SAP ABAP Learning, SAP ABAP Guides, SAP ABAP Online Exam

No comments:

Post a Comment