How to create a simple Z-BAdI definition?

In this short demonstration, I have tried to create a custom Z-BAdI definition for a project. This allows you to build a product report and classes and allowing the customers to build later custom logic thanks to the enhancement spots and BAdIs.

Until now, I only have always implemented standard BAdIs from SAP directly. In this tutorial I want to show you how to define a such definition by your own.

Definition

First, we need to create a BAdI definition via SE18.

SE18, filling the enhancement spot field with a summarizing name.

After we hit the button “Create”, we can later define a name of this spot.

Now we are ready to create a real BAdI Definition, which is always linked to an enhancement spot.

After we have defined that, we get two sub points (Interface and Implementation). We need to define an interface for this BAdI definition, which than can later be consumed by the implementation classes.

Interface

In our interface we have a simple example of passig two values, which then can be sum up or whatever the implementation wants to do. Important is that the return value gets a new result.

INTERFACE zif_badi_test_sum
  PUBLIC .


  INTERFACES if_badi_interface . " Every BAdI Interface must reference to this interface too.

  METHODS sum_value
    IMPORTING
      VALUE(iv_val_a)      TYPE int4
      VALUE(iv_val_b)      TYPE int4
    RETURNING
      VALUE(rv_val_result) TYPE int4 .
ENDINTERFACE.

Calling the BAdI

Now we setup a simple report, from which we can use the BAdI later. We provide two values (4 and 5) in separate variables. After that we create an instance of the lr_badi by calling the GET BADI command. This command does the magic, by selecting the correct class according to the settings in SE19. Note that GET BADI also accepts the feature “Filters” which, then evaluates excatly, which implementation should be called, if you have multiple of them.

REPORT zbadi_test.

DATA: lr_badi TYPE REF TO zbadi_test_sum.

DATA: lv_value_a TYPE i,
      lv_value_b TYPE i,
      lv_result TYPE i.

lv_value_a = 5.
lv_value_b = 4.

TRY.
    GET BADI lr_badi. " The implementation class is fetched here.
    
    " According to our interface, we have fill the parameters and receive a return value.
    CALL BADI lr_badi->sum_value
      EXPORTING
        iv_val_a      = lv_value_a
        iv_val_b      = lv_value_b
      RECEIVING
        rv_val_result = lv_result.
        
  CATCH cx_badi INTO DATA(lx_data).
    WRITE: 'Using the default way.'.
    lv_result = lv_value_a + lv_value_b.
ENDTRY.

WRITE: /, 'Result: ', lv_result.

As you may also notice, that we encapsulate the GET BADI with a TRY-CATCH block, where we catch all the BADI specific exceptions. So if no implementation has been made yet, it will cause in the exception CX_BADI_NOT_IMPLEMENTED.

Implementation

Now we need to implement our BAdI definition rather our interface. For that we select the sub path “Implementation” and create a new enhancement implementation.

After that we can then check our implemeneting class.

For the implementation it self, the newly defined class automatically implements now the previously defined interface. All the methods inside the interface now will be finally implemented inside this class.

This part, now can be used by the customers to develop own logic, which is abstracted from the main business logic.

class ZCL_BADI_TEST_SUM_IMPL definition
  public
  final
  create public .

public section.

  interfaces IF_BADI_INTERFACE .
  interfaces ZIF_BADI_TEST_SUM .
protected section.
private section.
ENDCLASS.



CLASS ZCL_BADI_TEST_SUM_IMPL IMPLEMENTATION.


* <SIGNATURE>---------------------------------------------------------------------------------------+
* | Instance Public Method ZCL_BADI_TEST_SUM_IMPL->ZIF_BADI_TEST_SUM~SUM_VALUE
* +-------------------------------------------------------------------------------------------------+
* | [--->] IV_VAL_A                       TYPE        INT4
* | [--->] IV_VAL_B                       TYPE        INT4
* | [<-()] RV_VAL_RESULT                  TYPE        INT4
* +--------------------------------------------------------------------------------------</SIGNATURE>
  METHOD zif_badi_test_sum~sum_value.
    rv_val_result = iv_val_a + iv_val_b + 1. " For test purposes, we add here an additional 1.
  ENDMETHOD.
ENDCLASS.

Conclusion

As a short conclusion, you see now how to define and implement your own BAdIs.


Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

Ads Blocker Image Powered by Code Help Pro

Ad blocker detected

We have detected that you are using extensions to block ads. Please support us by disabling your ad blocker.

Powered By
Best Wordpress Adblock Detecting Plugin | CHP Adblock