Skip to Content

Applies to:

SAP BPC 7.5/SAP BW 7.3x

Summary:

This document explains Badi usage in BPC context .BADIs are always faster than Script Logic and can become useful where we have comlex business requirements .

Document covers Introduction of BADI wrt BPC ,creation of BADIs, useful methods during BADI Creation and Testing/Debugging Created BADIs .

Author(s)     :  Amit Kumar Trivedi

Company    :   Infosys Limited

Created on  :  21 August 2013

Author Bio

AMIT KT.jpg

Amit Kumar Trivedi works as Senior Systems Engineer in Infosys Limited. He has 3.7 years of experience in

SAP BW/ ABAP. He has worked on Various Support and implementation Projects.

Table of Contents

1) Basic Introduction

2) Creating Custom Badi/Script Logic/DM Package

3) Useful Methods in Writing Badi

4) Debugging/Testing Badi.

5) Execution Sequence

1) Basic Introduction

a)     a) BADI vs Script Logic: Make use of Badi wherein you have complex logic to implement. Badi’s are always faster if it is of good design.

BADIs have capability of providing platform to write complex logics using ABAP. It can use capabilities like internal table, methods available to read master  data /transactional data which will be useful to write complex codes and achieve complex business requirements.

It also has its own disadvantages that you will not be able to see the BADIs from BPC Client which creates readability issues .Also you should have knowledge of ABAP to implement it.

Script Logics can be useful when we have limited scope for execution .But these are difficult to implement when we have complex business requirements. Also from execution point of view they do not provide good performance because of multiple parsing for each call due to which multiple READ/WRITE operation takes place.

Note: We should avoid putting all our logic implementation in WriteBack Badi as this is executed every call of Input Schedule .Instead we can make use of Custom Badi to have different logics implemented as there is no limit on Number of Custom Badi in an application, whereas in a single implementation only One WriteBack Badi is written.

b)     b) WriteBack Badi  vs Custom Badi

WriteBack Badi is used when we want to write back the records with certain fashion like disaggregation, distribution etc. For ex: There would be scenarios where the planning is done at higher levels and the data needs to be disaggregated to the child members , in this case we need to have WriteBack Badi to disaggregate data till child members . It’s a Preprocess logic that gets executed first in every call. This is written under Enhancement Spot “UJR_WRITE_BACK”.

Custom Badi is used to execute logic which can be linked with Data Manager Packages. We can achieve a performance boost by using Custom Badi instead of script logics. We can use this in Currency Conversions, Unit of Measure Conversions etc. This is written under Enhancement Spot UJ_CUSTOM_LOGIC.

2)  Creating Custom Badi/Script Logic/DM Package

a)      Creating a Custom Badi

Note: [The Steps shown below doesn’t cover creation of Enhancement Spot. Already enough documents are present for the same to go through]

Step 1: Go to SE 19.Enter the Enhancement Spot name (UJ_CUSTOM_LOGIC for Custom Badi) and click on create Impl. as shown below.

/wp-content/uploads/2013/08/1_265239.png

Step 2: A new window will open. Enter the Enhacement Implementation Name and short text and press on Continue .

/wp-content/uploads/2013/08/2_265240.png

Step 3: Enter the Badi Implementation , Implementing Class ,BADI Definition and press on continue as shown below .

/wp-content/uploads/2013/08/3_265265.png

Step 4: In new window you will get details for the created Enhancement Implementation.

For ex: – Badi definition Name, Interface used for creating Enhancement Spot, Whether Implementation is Active.

/wp-content/uploads/2013/08/4_265266.png

Step 5: Go to Filter Val. And provide combination value which will be used in Script logic to call the Custom Badi.

/wp-content/uploads/2013/08/5_265267.png

Click on Create Combination .Below option will come as shown below.

/wp-content/uploads/2013/08/6_265274.png

After that, click on Change Filter Value; provide Value for Filter and press continue.

/wp-content/uploads/2013/08/7_265275.png

/wp-content/uploads/2013/08/8_265276.png

Step 6: Writing the Code for implementation .Double Click on Implementing Class as shown below.

/wp-content/uploads/2013/08/9_265277.png

Double Click on Method to open the ABAP window to write the actual code .Couple of Dialog Windows will open just press on “Yes” and continue.

Step 7: Blank window will get open. You need to write your Logic in between the Start of Method and End of Method.

/wp-content/uploads/2013/08/10_265278.png

Step 8: A simple Custom Badi to Take a RECORD AND Copy it to Another Key.

method IF_UJ_CUSTOM_LOGIC~EXECUTE.
   TYPES:
BEGIN OF data,
       measures
TYPE c LENGTH 20,
       account
TYPE c LENGTH 20,
       company
TYPE c LENGTH 20,
       geography
TYPE c LENGTH 20,
       product
TYPE c LENGTH 20,
       currency
TYPE c LENGTH 20,
       unit
TYPE c LENGTH 20,
       time
TYPE c LENGTH 20,
       version
TYPE c LENGTH 20,
    
END OF data.

  DATA: wa_data TYPE data,
        wa_final_data
TYPE data,
        it_data 
TYPE STANDARD TABLE OF data,
        it_final_data
TYPE STANDARD TABLE OF data.

  IF ct_data[] IS NOT INITIAL.   ” Checking If ct_data is not Blank .

    it_data = ct_data.   ” Copying CT_DATA into an internal Table .
   
SORT it_data BY company.   ” Sorting By Company
   
DELETE it_data WHERE company <> ‘ABC’.   ” Deleting All other Company Except ABC .
   
SORT it_data BY account.   “Then Sorting based on Account
   
DELETE it_data WHERE account <> ‘XYZ’.   “Deleing All Other Accounts Except XYZ .

    LOOP AT it_data INTO wa_data.
      wa_final_data = wa_data.
      wa_final_data-account =
‘C0049’.
     
APPEND wa_final_data TO it_final_data.
      wa_final_data-account =
‘C0002’.
     
APPEND wa_final_data TO it_final_data.
    ENDLOOP.

    SORT it_final_data.
   
DELETE ADJACENT DUPLICATES FROM it_final_data.
*  Overwrite buffer table with final result set.
    ct_data = it_final_data.
  ENDIF.
endmethod.

Step 9: Finally Activate the Badi and Badi is ready to be used in Script logic call.

b)    b)   Creating a Script Logic and linking it with Custom Badi

Step1 :  Go to BPC Admin Client and go for “Create New Logic” .Enter a Technical Name and click on Ok to continue .

/wp-content/uploads/2013/08/11_265289.png

Step2:  Write below code and click on “Validate and Save”. Script Logic is created.

  /wp-content/uploads/2013/08/12_265290.png

.

              

c)     c) Creating a Data Manager Package and linking it with Script Logic.

Step1: Go to BPC Excel client and then to Planning and Consolidation tab as shown below.

Capture.JPG

                      

Step 2: Follow the standard procedure to create a DM Package. Already enough documents are available in SDN for the same. One Sample DM Package linking with Script Logic is shown below.

Capture.JPG

3) Useful Methods in Writing Badi

a)     a) Reading the Dimension Member Data. : – Reading a GEOGRAPHY Dimension Member data.

DATA: lv_appset_id TYPE uj_appset_id VALUE ‘ABC’,        “ APPSET ID
      lo_geography
TYPE REF TO if_uja_dim_data,          ” Object Reference

      ls_hier_info TYPE uja_s_hier,
      lt_hier_name
TYPE uja_t_hier_name,

      ls_attr_list TYPE uja_s_attr,

      lt_attr_name_geo TYPE uja_t_attr_name ,

      lr_data TYPE REF TO data.

FIELD-SYMBOLS:

      <lt_geo_mbr> TYPE HASHED TABLE,        ” All entity members

“Object Creation


lo_geography = cl_uja_admin_mgr=>create_dim_ref( i_appset_id = lv_appset_id
                                                 i_dim_name =
‘GEOGRAPHY’ ).

“Choosing Attributes of Dimension to be read.

If we want to read only two Attributes “Country” and “Area” & Hierarchy from GEOGRAPHY Dimension, then take out those attributes and put them in table lt_attr_name_geo and lt_hire_name.

ls_hier_info-hier_name =

‘PARENTH1’.

APPEND ls_hier_info-hier_name TO lt_hier_name.

ls_attr_list-attribute_name =

‘COUNTRY’ .

APPEND ls_attr_list-attribute_name TO lt_attr_name_geo.

ls_attr_list-attribute_name = ‘AREA’.

APPEND ls_attr_list-attribute_name TO lt_attr_name_geo.

* Retrieving the Dimension member for Geography .
CALL METHOD lo_geography->read_mbr_data
   
EXPORTING
       if_ret_hashtab = abap_true
       it_attr_list   = lt_attr_name_geo         
” columns:attributes name list
       it_hier_list   = lt_hier_name             
” columns:hieracies name list
   
IMPORTING
       er_data        = lr_data.
   
ASSIGN lr_data->* TO <lt_geo_mbr>.

<lt_geo_mbr> will have the data read from the Dimension GEOGRAPHY from BPC and can be used in Badi as per your requirement.

b)     b)  Reading the Dimension Names of an Application.

Below is the technique of referring to the cube dimensions indirectly through application name so that you we are immune to cube changes due to full optimize.

DATA: lo_appl TYPE REF TO cl_uja_application,

      lv_appset_id TYPE uj_appset_id VALUE ‘ABC’,
      lv_application_id
TYPE uj_appl_id VALUE ‘APPLICATION’,

      lt_appl_dim TYPE uja_t_appl_dim,

      lt_dim_name TYPE ujq_t_dim,

      ls_appl_dim LIKE LINE OF lt_appl_dim,

ls_dim_name LIKE LINE OF lt_dim_name,

* Retrieving the  Dimension Name
     
CREATE OBJECT lo_appl
       
EXPORTING
          i_appset_id     
= lv_appset_id
          i_application_id
= lv_application_id.

      REFRESH lt_appl_dim.
      lo_appl
->get_appl_dim(
               
EXPORTING
                    i_appl_id  
= lv_application_id
               
IMPORTING
                    et_appl_dim
= lt_appl_dim ).

      REFRESH lt_dim_name.
* Retrieving the Dimension name Of The SMP Application.
     
LOOP AT lt_appl_dim INTO ls_appl_dim.
        ls_dim_name
= ls_appl_dimdimension.
       
APPEND ls_dim_name TO lt_dim_name.
       
CLEAR ls_dim_name.
     
ENDLOOP.

Further lt_dim_name can be used to read the transactional data from cube. Please see next points how to read transactional data from cube.

c)    c)   Reading the Transactional Data from the cube.

DATA:   lv_appset_id TYPE uj_appset_id VALUE ‘ABC’,
        lv_application_id1
TYPE uj_appl_id VALUE ‘APPLICATION’,
        lt_dim_name
TYPE ujq_t_dim,

        it_sel TYPE uj0_t_sel,

                   wa_sel TYPE uj0_s_sel,

“Selection table to filter the Result Set from the Cube Read. Data for Geography INDIA will be read from the cube.

CLEAR : it_sel.
        wa_sel
dimension = ‘GEOGRAPHY’.
        wa_sel
attribute = ‘ID’.
        wa_sel
sign      = ‘I’.
        wa_sel
option    = ‘EQ’.
        wa_sel
low       = INDIA’.
        wa_sel
high      = .
       
APPEND wa_sel TO it_sel.
       
CLEAR wa_sel.
** Call SQE to read transaction data
       
TRY.
           
CALL FUNCTION ‘UJQ_RUN_RSDRI_QUERY’
             
EXPORTING
                i_appset_id      
= lv_appset_id  “ Appset Id
                i_appl_id        
= lv_application_id1   “ Application
                it_dim_name      
= lt_dim_name “ Dimensions
                it_sel           
= it_sel “ Selection Table
                if_check_security
= abap_true “ to enable Security Check
             
IMPORTING
                et_data          
= it_rate_data. “ Output
         
CATCH cx_ujq_exception.
       
ENDTRY.

Other Important Parameters available in Function Module.

a)      Package Size: – You can define your own Package Size and then the output will have same number of records in each data package.

b)      Security Check: – Default value for this is “ABAP_TRUE”. If this value is set Cube read checks for the security.

c)      Database Aggregate: – Default value for this is “ABAP_TRUE”. If this value is set to true, cube read makes use of Database Aggregates.

d)      d)  Reading the Child members of a Hierarchy.

DATA: lo_geography TYPE REF TO if_uja_dim_data,

*To get the children if any
     
CALL METHOD lo_geography->get_children_mbr
       
EXPORTING
          i_parent_mbr    
= ‘ASIA’ ” Parent
          i_level         
= 99 ” -99 = All children in any level; -1 = direct child
          if_only_base_mbr
= abap_true ” Only base member
       
IMPORTING
          et_member       
= lt_geography_mbr.

lt_geography_mbr   will have  all the Child Members for Geography ASIA .

4) Debugging/Testing Badi

Debugging is the best way of understanding ABAP code .There is a small difference in debugging process for Data Manager Packages and for Input Schedule.

                                                                                                                               

1)      Debugging Badi for Data Manager Package Run/Offline Run :-

Ø  Go to SE24, Enter Class name: – CL_UJD_START_PACKAGE.

Click on Display.

/wp-content/uploads/2013/08/15_265307.png

Ø  Double click on Method “RUN_PACKAGE”. You will be taken to ABAP Editor.

/wp-content/uploads/2013/08/16_265308.png

Ø  Go to Method Call RUN_PACKAGE_P and apply External Breakpoint at this line.

/wp-content/uploads/2013/08/17_265309.png

Ø  Go to your Badi and apply External Breakpoint at the line from where you want to start the Debugging .Debugging Pointer will directly come at that line.

/wp-content/uploads/2013/08/18_265313.png

Ø  After that you can run the Data Manager Package giving your selections .

Capture.JPG

Ø  You will be redirected to the below screen .Double click on LF_SYNCHRONOUES and change the value of it to ‘X and press enter’. Initially it will be blank .After that press F7 to navigate into the Badi Screen .

/wp-content/uploads/2013/08/20_265316.png

/wp-content/uploads/2013/08/21_265317.png

Ø  Once you click on F7 you will be directed to your Custom Badi , where you can follow normal  way of doing the debugging .

/wp-content/uploads/2013/08/22_265318.png

     To Execute Single Step: – Press F5

     To Execute: – Press F6

     To Return: – F7

     To Continue: – Press F8.

2)      Debugging Badi for Input Schedule/Online Run: – The only difference in Input Schedule Badi debugging is that you don’t need to put Break Point in class rather you can directly put Breakpoint in Custom Badi and you will be  taken into custom Badi Debugging screen.

5) Execution Sequence

Below is the sequence of execution of different objects in SAP BPC.

WB->CR->DL->WB->CR->CB1->WB->CR->CB2… and so on .

Notation Used

WB  -> WriteBack Badi

CR    -> Cube Read based on scoping

DL    -> Default Logic

CB1  -> Custom Badi 1

CB2  -> Custom Badi 2

To report this post you need to login first.

10 Comments

You must be Logged on to comment or reply to a post.

  1. Uma Arjunan

    Useful one to trigger the debugger from Run Package.

    But  I could not see the method RUN_PACKAGE_P in the class CL_UJD_START_PACKAGE.

    CL_UJD_START_PACKAGE – What is the alternative class in BPC 10  ?

    Got answer from sdn thru another blog

    http://scn.sap.com/people/daniel.settanni/blog/2011/07/13/debugging-bpc-10-for-netweaver

    Below points might be helpful for those who wants to debug BADI in BPC 10 without UJKT.

    Class name is CL_UJXD_PACKAGES_RES

    Method name is DO_POST – > Set ext brk point in RUN_PACKAGE method

    before proceeding to the next step,  set the debug parameter

         LS_PACKAGE_RUN-IF_DEBUG = ‘X’

    then the break-point which is already set at the custom BADI will stop there.

    This way BADI can be debugged directly from BPC run_package screen

    (0) 
    1. Amit Kumar Trivedi Post author

      HI Uma ,

      RUN_PACKAGE_P method is not inside CL_UJD_START_PACKAGE . You have to double click on RUN_PACKAGE Method in this class , which will open a method which uses this program .

      Regarding BPC10.0 i will let you know. As I don’t have information about that .

      Regards,

      Amit

      (0) 
  2. Vadim Kalinin

    Hi Amit,

    Just one correction to your good document:

    When you explain the possible usage of write back badi you mention only “disaggregation, distribution etc.”. But in reality, the write back badi is used also as a replacement of default.lgf if the calculations in this script are significantly complex. Additional benefit of write back badi compared to default.lgf is that only changed data will be processed (default.lgf receives a scope as a combination of all members of all dimensions of changed values).

    B.R. Vadim

    (0) 
    1. Gustavo Fra Otero

      Hi Vadim and Amit,

      What is the different between use write back badi or default.lgf?

      For example, I have an Input Schedule. I send data. ¿Is Default.lgf executed? ¿Is write back badi executed?

      And If I have a Reportd and I refresh it.  ¿Is Default.lgf executed? ¿Is write back badi executed?

      What is the best practice?

      For example: Default.lgf

      Define Scope

      When acc.type=”EXP”

        start_Badi complex_add

        end_badi

          When acc.type=”INC”

              start Badi complex_rest

              end badi

          end when

      end when

      or

      For example: write back badi. Manage logic inside badi.

      ¿What is the bes performance?

      Thanks.

      Regards

      Gustavo

      (0) 
      1. Vadim Kalinin

        Hi Gustavo,

        Write back badi is launched on every write to cube (with a table of records to be written). It will be launched before default.lgf and after default.lgf will write new data.

        Report refresh do nothing with default.lgf and write back badi – report is only reading data.

        Vadim

        (0) 

Leave a Reply