Monday, 15 April 2024

Dynamic Derivations using BADI in SAP MDG

Business rules are one of the important feature in Master Data Governance Process. Deriving the value for particular field of an entity can be done either by BRF Plus or BADI. This Blog Post explains you about creating complex or dynamic derivation rules which can be achieved through BADI.

In this post, I am taking the example of deriving Cost Centre value from Cost Centre Entity of Finance Data Model. Its a kind of Mandatory and derivation rule and the naming convention should be followed.

RULE:


First two characters are defined using the backend table which is the custom table provided by the business based on 'Company Code' and 'Business Area' fields in Cost Center entity. The rest of the characters are auto populated based on the next number available for the range.

Ex: If Cost Centre number available in the system for LM range is LM0025. So, when requestor is creating the new cost center the CC value should be auto populated to LM0026.

IMPLEMENTATION:


1. Implement your custom logic in 'IF_EX_USMD_RULE_SERVICE2~DERIVE' method and trigger the code.

Perform the below steps for execution: Go to MDGIMG ( t-code ) -> General settings -> Data Quality and Search ->BADI -> Define Validations and Derivations ( execute ). Create enhancement.

Enhancement Spot  :   'ZENH_MDG_FI_CCTR'.

BADI Name                :   'ZBI_MDG_FI_CCTR'.

Method Name           :   'IF_EX_USMD_RULE_SERVICE~DERIVE_ENTITY'.  

Filter Values               :   'Entity Type' and 'Data Model'.

Logic for Derivation Rule:

PARAMETERS:  

Dynamic Derivations using BADI in SAP MDG

method IF_EX_USMD_RULE_SERVICE~DERIVE_ENTITY.
                   
TYPES: BEGIN OF ty_cctr,
              zcctr TYPE cctr,
            END OF ty_cctr.

    DATA: lr_request      TYPE REF TO if_usmd_crequest_api,
           lv_cr_number    TYPE usmd_crequest,
           lv_crequest     TYPE usmd_s_crequest,
           it_crequest     TYPE STANDARD TABLE OF usmd_s_crequest,
           usmd_creq_type  TYPE usmd_creq_type,
           usmd_created_by TYPE xubanme.

    DATA: lv_physical_name TYPE mdg_gn_physical_name,
           lv_busarea       TYPE gser,
           lv-ccode         TYPE bukrs,
           lc_cctr          TYPE name-komp VALUE 'CCTR',
           lv_range         TYPE kostl,
           lv_cctr          TYPE kostl,
           lv_ccblash       TYPE zde-ccblash,

           result           TYPE string,
           count            TYPE i,
           cnt              TYPE i VALUE 0,
           lv_alp           TYPE string,
           lv_num           TYPE string,
           lv_ccrange       TYPE n LENGTH 4,
           lv_len           TYPE i,
           lv_bsrange       TYPE n LENGTH 10,

           it_cctr          TYPE TABLE OF ty_cctr,
           lv_op            TYPE string VALUE '%',
           lv_sctr          TYPE kost1,
           lv_number        TYPE string.

    CONSTANTS: lc_logical_cctr TYPE mdg_gn_logical_name VALUE 'TCK_0G_CCTR'.

*Getting WF step.
    TRY.
        DATA(lo_context) = cl_usmd_app_context=>get_context().
      CATCH cx_usmd_app_context_cons_error INTO DATA(lo_exception).
    ENDTRY.

    IF lo_cotext IS BOUND.
      lo_context->get_attributes( IMPORTING ev_crequest_type = DATA(lv_range)
                                            ev_crequest_step = DATA(lv_wf_step) ).
    ENDIF.

*Deriving Cost Centre Value.
    SELECT SINGLE cskio_range FROM t9k_csk_io_range
          INTO _range WHERE gsber = _busarea AND bukrs = _ccode.

    IF sy-subrc = 0.
      CONCATENATE lv_range lv_op INTO lv_str.

      SELECT SINGLE physical_name FROM mdg_gn_tgobj
               INTO lv_physical_name WHERE logical_name EQ lc_logical_name
                     AND new_version = ''.

      IF sy-subrc = 0.
        SELECT /1md/0gcctr FROM (lv_physical_name) INTO TABLE it_cctr
                 WHERE /1md/0gcctr LIKE lv_sctr.
        IF sy-subrc = 0.
          SORT it_cctr STABLE BY zcctr DESCENDING.
          READ TABLE it_cctr INTO DATA(wa_cctr) INDEX 1.

*Formating the Cost Center Value.

          count = strlen(wa_cctr).
          DO count TIMES.
            result = wa_cctr + cnt(1).

            IF result CA 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'.
              CONCATENATE lv_alp result INTO lv_alp.
              CLEAR result.
            ELSE.
              CONCATENATE lv-num result INTO lv_num.
              CLEAR result.
            ENDIF.
            cnt = cnt+1.
          ENDDO.
          lv_num = lv_num + 1.
          CLEAR lv_cctr.
          CONCATENATE lv_alp lv_num INTO lv_cctr.

          lv_len = strlen(lv_cctr).
          IF lv_len < 6.
            lv_ccrange = lv_num.
            CLEAR lv_cctr.

            CONCATENATE lv_alp lv_ccrange INTO lv_cctr.
          ENDIF.

        ELSE.
          CONCATENATE lv_range '0000'INTO lv_range.
          CLEAR lv-cctr.
          lv_cctr = lv_range.
          SHIFT lv_range RIGHT DELETING TRAILING space.
          OVERLAY lv_range WITH '0000'.
          lv_cctr = lv_range.
        ENDIF.
      ENDIF.
    ELSE.
      lv_num = lv_number+7(3).
      CONCATENATE 'TMP' lv_num INTO lv_sctr.
      lv_cctr = lv_sctr.
      gv_sctr = lv_sctr.
    ENDIF.
    CONCATENATE 'TMP_001'  ' ' INTO lv_cctr.
  ENDIF.
ENDIF.
endmethod.
 
CHECKING THE VALUE ON NWBC SCREEN:

Dynamic Derivations using BADI in SAP MDG
Fig 1: NWBC Screen

CONCLUSION:

We can accomplish the Complex Business Requirement by dynamically coding it in the BADI.

These kind of rules can't be attained through BRF+ which involves some custom tables, joining those tables getting the data, segregating the data and formatting the data. Likewise, we can perform validations by using check entity method in Validations and Derivations BADI.

No comments:

Post a Comment