Declaring dynamic call of BAdi?

1.1k views Asked by At

I want to define an object for BAdI implementation that will not initialize the BAdI by its name in declaration.

Example of what I don't want:

DATA l_split_badi TYPE REF TO fieb_get_bank_stmts_x.

Question 1 : I want something like:

DATA l_split_badi TYPE REF TO object.
lv_class_name = 'fieb_get_bank_stmts_x'.
create object l_split_badi type (lv_class_name).

If I declare like above, I get the following syntax error:

"L_SPLIT_BADI" is not a valid BAdI handle here.

The reason why I need to perform such implementation is, while importing the Change request to a system which has an older SAP version, the import fails because of BAdI declaration with TYPE REF TO (because that BAdI doesn't exist in the system).

My idea is with dynamic declaration to avoid the pre-check on importing the Change request.

Any idea is welcome ! Thanks to all !


EDIT Question 2 : after solution proposed by Sandra Rossi to use DATA l_split_badi TYPE REF TO cl_badi_base and GET BADI l_split_badi TYPE ('FIEB_GET_BANK_STMTS_X'), I get the same syntax error at line CALL BADI l_split_badi->split below:

CALL BADI l_split_badi->split
  EXPORTING
    i_string           = lv_cont
  IMPORTING
    et_string          = lt_xml_string
  EXCEPTIONS
    split_not_possible = 1
    wrong_format       = 2.
1

There are 1 answers

6
Sandra Rossi On

Question 1

For BAdIs which are part of an Enhancement Spot (display the BAdI via the transaction code SE18 and you will know), you must not use CREATE OBJECT, but instead GET BADI which has a dynamic variant:

DATA badi TYPE REF TO cl_badi_base.
TRY.
    GET BADI badi TYPE ('FIEB_GET_BANK_STMTS_X')...
  CATCH cx_badi_unknown_error INTO DATA(lx).
    " The BAdI doesn't exist, handle the case...
ENDTRY.

EDIT: notice that the instance declaration is referencing CL_BADI_BASE, the super class of all BAdI definitions.

Question 2

Calling the method SPLIT statically is invalid because SPLIT doesn't exist in CL_BADI_BASE. You must use the dynamic variant of CALL BADI:

CALL BADI l_split_badi->('SPLIT')
  EXPORTING
    i_string           = lv_cont
  IMPORTING
    et_string          = lt_xml_string
  EXCEPTIONS
    split_not_possible = 1
    wrong_format       = 2.