Skip to Content
Author's profile photo Eitan Rosenberg

Sorting home made BADI

Hi,

Recently I am involve in a very big project and we are discussing ways to code in old fashion user exit (form exit ,functions).

One of the options is to use BADI within the enhancement framework .

I became aware of this idea from  A framework for local/region specific requirements in user-exits using BAdIs and the new enhancement framework (revisited). by Bruno Esperança

This small blog post will present an idea how to sort the BADI implementation prior to execution.

For code See attache program Y_R_EITAN_TEST_51_18

The basis of the idea is based on the fact that internal table IMPS that contains the implementations is public.

/wp-content/uploads/2014/10/2014_10_07_06h41_39_557110.png

/wp-content/uploads/2014/10/2014_10_07_06h41_56_557113.png

In the BADI interface we can create a method that will give the implementation the option to return its sorting value .

/wp-content/uploads/2014/10/2014_10_07_08h36_29_557112.png

FORM test_01 is “regular” call .

And the result:

/wp-content/uploads/2014/10/2014_10_07_08h44_20_557114.png

FORM test_02 is “sorted” call .

From each implementation we get the result of get_sort_value to create the a sorting table .

/wp-content/uploads/2014/10/2014_10_07_08h59_08_557128.png

We sort the implementation in descending order .

And the result:

/wp-content/uploads/2014/10/2014_10_07_08h46_03_557115.png

It seems to be working .

Mind you that this sorting code was not tested in actual production environment .

Regards.

Assigned Tags

      12 Comments
      You must be Logged on to comment or reply to a post.
      Author's profile photo Former Member
      Former Member

      Hi,

      May I ask why do you need to sort the implementations?

      Anyway,

      You have a standard way to achieve it:

      For new BAdI, you have in the BAdI implementation a dedicated field called "Layer of a BAdI Implementation" (Data element /DBM/OE_ENHBADI_IMPL_LAYER_D) by default.

      For classic BAdI you might need to work a little harder and first explicitly activate the sorting option via SE18 -> Utilities -> Sort (read further details in SAP Help). Then you should have a similar field in the BAdI implementation (SE19).

      Author's profile photo Eitan Rosenberg
      Eitan Rosenberg
      Blog Post Author

      Hi,

      I have no need for sorting at the moment.

      I am on on ecc6 .

      Maybe I missed that but I do not see any sort under ....

      My phone is public can you call me please.

      Regards.

      Author's profile photo Former Member
      Former Member

      You should see it.

      This option is available in NW 7.0 (or earlier).

      Author's profile photo Eitan Rosenberg
      Eitan Rosenberg
      Blog Post Author

      Hi,

      Thi is what I have:

      /wp-content/uploads/2014/10/screenshot_02_557281.png

      Author's profile photo Eitan Rosenberg
      Eitan Rosenberg
      Blog Post Author

      Found this:

      See at the bottom..... ­čśĽ

      /wp-content/uploads/2014/10/screenshot_03_557339.png

      Author's profile photo Former Member
      Former Member

      This SORT feature is supported for classical BAdIs only. The one that you have defined is a Kernel BAdI and it can be sorted by BADI_SORTER only.

      Author's profile photo Former Member
      Former Member

      Suhas,

      It seems you are right.

      For some reason, I thought that this is default behaviour in new BAdI, but it seems it depends on implementation of BADI_SORTER.

      Author's profile photo Bruno Esperan├ža
      Bruno Esperan├ža

      Very valuable and helpful.

      Thanks for sharing this Eitan!

      Best regards,

      Bruno

      Author's profile photo Krishna vs
      Krishna vs

      Hi Eitan,

      Please could you share the code of Y_R_EITAN_TEST_51_18..is it available on github.

      Thanks,
      Subba

      Author's profile photo Eitan Rosenberg
      Eitan Rosenberg
      Blog Post Author

      Hi,

      I am retired now.

      I do have some code at home.....

      REPORT y_r_eitan_test_51_18 .
      
      * Some reference
      * http://scn.sap.com/community/abap/application-development/objects/blog/2012/02/20/factory-pattern-in-abap-oo
      * http://scn.sap.com/community/abap/blog/2014/02/07/a-framework-for-specific-requirements-in-data-transfer-routines-and-everywhere-else
      * http://scn.sap.com/community/abap/blog/2014/02/14/a-framework-for-localregion-specific-requirements-using-badis-and-the-new-enhancement-framework
      
      PARAMETERS: p_debug  TYPE debug_flg AS CHECKBOX  .
      
      START-OF-SELECTION .
        PERFORM at_start_of_selection .
      
      *----------------------------------------------------------------------*
      FORM at_start_of_selection .
        PERFORM test_01 .
      *  PERFORM test_02 .
      ENDFORM .                    "at_start_of_selection
      *----------------------------------------------------------------------*
      FORM test_01 .
      
        DATA: ob_y_eitan_badi_01 TYPE REF TO y_eitan_badi_01 .
      
        GET BADI ob_y_eitan_badi_01 .
      
        FIELD-SYMBOLS: <st_imps> LIKE LINE OF ob_y_eitan_badi_01->imps .
      
        DATA: it_return TYPE bapirettab .
      
        CALL BADI ob_y_eitan_badi_01->get_message
          CHANGING
            it_return = it_return.
      
        BREAK-POINT .
      
      ENDFORM .                                                   "test_01
      *----------------------------------------------------------------------*
      FORM test_02 .
      
        DATA: ob_y_eitan_badi_01 TYPE REF TO y_eitan_badi_01 .
      
        GET BADI ob_y_eitan_badi_01 .
      
        FIELD-SYMBOLS: <st_imps> LIKE LINE OF ob_y_eitan_badi_01->imps .
      
      * Sorting table
        DATA: BEGIN OF st_sort .
        DATA: sort_value TYPE integer .
        DATA: imp TYPE REF TO y_eitan_badi_01_int .
        DATA: END OF st_sort .
      
        DATA: it_sort LIKE TABLE OF st_sort  .
      
      * Copy the implementations to the "Sorting table"
        LOOP AT ob_y_eitan_badi_01->imps ASSIGNING <st_imps> .
          st_sort-imp ?= <st_imps> .
          st_sort-imp->get_sort_value( CHANGING sort_value = st_sort-sort_value ) .
          APPEND st_sort TO it_sort .
        ENDLOOP.
      
      * Sorting....
        SORT it_sort BY sort_value DESCENDING .
      
        FIELD-SYMBOLS: <st_sort> LIKE LINE OF it_sort .
      
      * Clear: I hope that it does not cause damage...
        CLEAR: ob_y_eitan_badi_01->imps[] .
      
      * Copy back...
        LOOP AT it_sort ASSIGNING <st_sort> .
          APPEND <st_sort>-imp TO ob_y_eitan_badi_01->imps .
        ENDLOOP .
      
        DATA: it_return TYPE bapirettab .
      
        CALL BADI ob_y_eitan_badi_01->get_message
          CHANGING
            it_return = it_return.
      
        BREAK-POINT .
      
      ENDFORM .                                                   "test_02
      *----------------------------------------------------------------------*
      FORM test_03 .
      
        DATA: ob_y_eitan_badi_01 TYPE REF TO y_eitan_badi_01 .
      
        GET BADI ob_y_eitan_badi_01 .
      
        DATA: it_return TYPE bapirettab .
      
        CALL BADI ob_y_eitan_badi_01->set_message
          EXPORTING
            it_return = it_return.
      
        BREAK-POINT .
      
      ENDFORM .                                                   "test_03
      *----------------------------------------------------------------------*
      FORM test_04 .
      
        DATA: st_bal_s_log  TYPE bal_s_log .
      
        st_bal_s_log-extnumber = sy-title .
        st_bal_s_log-aluser    = sy-uname .
        st_bal_s_log-alprog    = sy-repid .
      
        CALL FUNCTION 'BAL_LOG_CREATE'
          EXPORTING
            i_s_log = st_bal_s_log
          EXCEPTIONS
            OTHERS  = 1.
      
      * From program Y_R_EITAN_TEST_51_18
      
      * Simulating EXIT_ parameters .
        DATA: it_scarr   TYPE ty_scarr .
        DATA: it_sflight TYPE ty_flights .
      
        SELECT * INTO TABLE it_scarr   FROM scarr   UP TO 5 ROWS .
        SELECT * INTO TABLE it_sflight FROM sflight UP TO 10 ROWS .
      
      * Simulating it_tab .
        DATA: it_fbname TYPE fbname .
        FIELD-SYMBOLS: <st_fbname> LIKE LINE OF it_fbname .
      
        APPEND 'Y_R_EITAN_TESTS_05_01' TO it_fbname .
        APPEND 'Y_R_EITAN_TESTS_05_02' TO it_fbname .
      
        DATA: it_bapirettab  TYPE bapirettab.
      
        LOOP AT it_fbname ASSIGNING <st_fbname> .
      
          CALL FUNCTION <st_fbname>
            EXPORTING
              it_sflight = it_sflight
            CHANGING
              it_scarr   = it_scarr.
      
        ENDLOOP.
      
        PERFORM do_log_display .
      
      ENDFORM .                                                   "test_04
      *----------------------------------------------------------------------*
      FORM test_05
      * Simulate function table parameter
        TABLES
          xmcafko STRUCTURE  mcafkob
          xmcafpo STRUCTURE  mcafpob
          xmcafvg STRUCTURE  mcafvgb
          xmccomp STRUCTURE  mccompb
          xmckalk STRUCTURE  mckalkb
          x_stat  STRUCTURE  mcfstatus .
      
        DATA: ob_y_eitan_badi_01 TYPE REF TO y_eitan_badi_01 .
      
        GET BADI ob_y_eitan_badi_01 .
      
        FIELD-SYMBOLS: <st_imps> LIKE LINE OF ob_y_eitan_badi_01->imps .
      
        DATA: it_return TYPE bapirettab .
      
        CALL BADI ob_y_eitan_badi_01->exit_saplmcf2_001
          CHANGING
            xmcafko = xmcafko[]
            xmcafpo = xmcafpo[]
            xmcafvg = xmcafvg[]
            xmccomp = xmccomp[]
            xmckalk = xmckalk[]
            x_stat  = x_stat[].
      
      ENDFORM .                                                   "test_05
      *----------------------------------------------------------------------*
      FORM do_log_display .
      
        DATA: st_display_profile TYPE bal_s_prof.
      
      * get a prepared profile
        CALL FUNCTION 'BAL_DSP_PROFILE_POPUP_GET'
          IMPORTING
            e_s_display_profile = st_display_profile
          EXCEPTIONS
            OTHERS              = 1.
      
        st_display_profile-use_grid = abap_true .
      
        CALL FUNCTION 'BAL_DSP_LOG_DISPLAY'
          EXPORTING
            i_s_display_profile = st_display_profile
            i_amodal            = abap_true
          EXCEPTIONS
            OTHERS              = 1.
      
        IF sy-subrc NE 0.
          MESSAGE ID sy-msgid TYPE 'S' NUMBER sy-msgno
                  WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
        ENDIF.
      
      ENDFORM .                    "do_log_display
      
      ----------------------------------------------------------------------------------
      Extracted by Mass Download version 1.5.2 - E.G.Mellodew. 1998-2018. Sap Release 701
      
      Author's profile photo Krishna vs
      Krishna vs

      Thanks Eitan!!

      Appreciate your response and surely trying your approach.I have a question...I have noticed in userexits like MV45AFZZ lot of global data is accessed and assigned to field symbols...

      any suggestion on how to handle that in Object oriented way...do we need to handle them as a dynamic method without naming parameters

      Regards,

      Subba

      Author's profile photo Eitan Rosenberg
      Eitan Rosenberg
      Blog Post Author

      Hi,

      I have no access to any SAP system, so I am not qualified to answer.

      I play a lot with Java these days....

      Regard.