Tuesday 30 October 2012

How to Implement BADI for Enhancing the Datasource in ECC

Hi all, 

In this post, the document describes about writing datasource enhancements for BI extraction in ECC system using BADI. It contains Customized code which helps you to use for any BI datasource enhancement in ECC with minimal changes.

The SAP enhancement RSAP0001 is used to fill the fields which are added to the extraction structure of the datasource, From Release 6.0, the Business Add-In (BAdi) RSU5_SAPI_BADI is available for datasource enhancements. So you will have several advantages while using BAdi instead of User exits. 

Note: In User exit, only one enhancement will be used for all the datasources,using BAdi, we can use multiple enhancements. Each enhancement will be implemented in a separate method of the class.

Business Scenario 

Data source 0FI_AR_4 is appended with fields ZZSPART– Division, ZZVKORG- Sales organization, ZZVTWEG-Distribution channel. The data for these fields should be filled from VBRK table. 

Step by Step Solution:
 
              Create Implementation with the standard BAdi Definition
                 Implementing the DATA_TRANSFORM method
                 Writing Source code in the DATA_TRANSFORM method
                 Creating a customized method for each datasource enhancement
                 Source code in the method M_0FI_AR_4  

We can write all our enhancements in separate methods in the same class. By this we improve performance and flexibility to work on respective enhancements without disturbing other enhancements.

Step By Step Procedure to Enhance Standard BI/BW Extractor Using BADI


Hi all, 

In this post, the document describes the step by step procedure to enhance Standard Business Content Extractors(Both Master Data and Transactional Data) using "RSU5_SAPI_BADI".

Generally SAP user exit RSAP001 is used to add any additional fields to the Standard BW Extractors whcih includes both Master Data and Transcation Data extractors. 

But as per SAP’s recommendation BADI’s are more effecient way to enhance the standard extractors than using the exit RSAP001.  

Enhacing a Standard DataSource using BADI has the following advantage over using the exit RSAP001: 

BADI can have mulitple implementations (only if Multiple use flag is set for the Badi) so several developers can work simultaneously on different implementions and different logics. 

RSU5_SAPI_BADI is used to enhance the standard BI DataSources.

Scenario 

In this document the Standard Master Data Source 0PROFIT_CTR_ATTR is appended with the field BUKRS – Company Code. The data for this field is filled from CEPC – BUKRS.

Procedure 
                 Enhancing Extract Structure 
                 Filling Data to the Enhanced field
                 Implementation of created BADI
                 Testing through RSA3

How to enhance SAP BW datasource/extractor using BADI or User Exit - Step by Step Procedure




Hi all,

In this post, I described Step by Step Procedure of how to enhance SAP BW datasource/extractor using BADI or User Exit.

We need to enhance SAP standard or custom extractors when the required fields are not available in the standard datasource.

Datasouces(Extractors) can be enhanced using two methods:
                                                                                                    1. User Exit
                                                                                                    2. BADI

In the User Exit method, we will write the logic for extracting new fields in an exit function module. This approach has some drawbacks like transport dependency, messier code, etc. This is the old approach. SAP has recently (couple of years ago?) introduced BAdis to alleviate the drawbacks of user exits. Using BAdis will allow developers to write object oriented code for extractor logic. SAP recommends BAdis over user exits. 

Irrespective of the approach, we choose the first step is to add the new fields to extraction structure. First check if the fields we are looking for are hidden in the datasource before adding the new fields to the structure.

Step A : Appending new fileds to Extraction Structure

1. Go to transaction code RSA6
2. Find the datasource that we want to enhance and click on change button.
3. If we need the fields are hidden, hide them.
Enhace_a_BW_Datasource_-_Append_Structure_1
4. Save the datasource and click on "Enhance Extraction Structure"
Enhace_a_BW_Datasource_-_Append_Structure_55.  Give a name to append structure.
Enhace_a_BW_Datasource_-_Append_Structure_3 
6. Enter the fields prefixed by ZZ in the first column and enter component type (For component type you can enter data element for that field which you can get from table definition in SE11).
Enhace_a_BW_Datasource_-_Append_Structure_47. Activate the append structure.

8. Hit back button twice and go to datasource definition. We will find the new    
    appended fields at the end, do not hide them.


First part of datasouce enhacement is complete. We have added the fields that we want to enhace to the the datasource. The next step is to write some code to populate these fields. As mentioned eariler there are two different ways to go about this. The first method is to use BADI which is recommended. Below steps show how to enhance an extractor using BADI. We can also use user exits for this. See the links for the documents, if you want to use user exits.

Step B: Writing code to populate new fields using BADI

1. First step is to create Implementation with the standard BADI Definition. For this go to transaction code SE18 and give BADI Name as RSU5_SAPI_BADI and select create from 'Enhancement Implementation' menu.
  Enhance_BW_Datasource_Using_BADIS-_Screenshot_1 
2. Enter Implementation Name as ZRSU5_SAP_BADI and hit check icon.
Enhance_BW_Datasource_Using_BADIS-_Screenshot_2 
3.  Give the description and activate the implementation.
Enhance_BW_Datasource_Using_BADIS-_Screenshot_3With this, we have created BADI implementation. We are going to use this implementation to fill the new fields.

4. Go to transaction code SE19 and give the implementation that we just created and hit display.
Enhance_BW_Datasource_Using_BADIS-_Screenshot_B15. Go to interface tab and double click on the implementing class name as shown below. This will take you to the methods section of the implementation class.
Enhance_BW_Datasource_Using_BADIS-_Screenshot_B26. In this class first two methods are SAP standard methods.
Enhance_BW_Datasource_Using_BADIS-_Screenshot_B37. The first method (DATA_TRANSFORM) method allows us put in the logic/code to fill in the our added fields. Double click on this method.

8. Click on 'Parameters' button to see the parameters passed to this method.
Enhance_BW_Datasource_Using_BADIS-_Screenshot_B49. You should see following SAP standard paramters. Add these parameters if you don't want to see them in your system. Go back to methods tab by hitting 'Methods' button.
Enhance_BW_Datasource_Using_BADIS-_Screenshot_B510. After come back to methods tab, double click on first method (DATA_TRANSFORM). Copy and paste below ABAP code.

method IF_EX_RSU5_SAPI_BADI~DATA_TRANSFORM.

  DATA : L_METHOD TYPE SEOCMPNAME.
   CONCATENATE 'Z_' I_DATASOURCE INTO L_METHOD.
   CHECK C_T_DATA[] IS NOT INITIAL.
   SELECT SINGLE CMPNAME FROM SEOCOMPO INTO L_METHOD WHERE
        CLSNAME = 'ZCL_IM_RSU5_SAP_BADI' AND
        CMPNAME = L_METHOD.
    CHECK SY-SUBRC EQ 0.
 CALL METHOD (L_METHOD)
    EXPORTING
      I_UPDMODE    = I_UPDMODE
      I_T_SELECT   = I_T_SELECT
      I_T_FIELDS   = I_T_FIELDS
    CHANGING
      C_T_DATA     = C_T_DATA
      C_T_MESSAGES = C_T_MESSAGES.

 endmethod.

With this, we did one time configuration. We need not do all the above steps for every datasource. To enhance a datasource all we need to do add a new cutom method to above class (ZCL_IM_RSU5_SAP_BADI) and write some ABAP codo to populate new fields. 

Step C: Create a custom method to populate new fields

1. Create a custome method for the class (ZCL_IM_RSU5_SAP_BADI). Method name should be Z_datasourcename . Here we are appending Z_ because method names can not start with number and most of the BW datasources start with a number. You can use you choice of prefix Z_ or M_ but you need to make sure that you are also using same prefix in the concatenate statement in the in the standard method (IF_EX_RSU5_SAPI_BADI~DATA_TRANSFORM) which calls our custom method at the tie of data extraction.

2. So, for the example in this article the method name should be Z_0FC_BP_ITEMS. To create the method in the method name field and select level as static and visibility as Publick and give some description.
SAP_BW_Enhancing_Datasource_Using_BADI_Screenshot_-_C13. Copy and paste following code.
  
Note: You need to change the table and field names in this code as per your requirement.
method Z_0FC_BP_ITEMS.

 *   You need to change FKKOPBW_DELTA to the extraction structure of your datasource in below statement

   FIELD-SYMBOLS: <L_S_DATA> TYPE FKKOPBW_DELTA.

 *  declare a structure with fields required the table on which you do the lookup. 

   TYPES: BEGIN OF IT_DFKKKO,
             V_OPBEL TYPE OPBEL_KK,
             V_FIKEY TYPE FIKEY_KK,
             V_ERNAM TYPE ERNAM,
             V_CPUTM TYPE CPUTM,             
         END OF IT_DFKKKO.

 *    Declare an internal table and work area with above type

  DATA: ZBW_DFKKKO TYPE STANDARD TABLE OF IT_DFKKKO,
        L_T_DATA TYPE STANDARD TABLE OF FKKOPBW_DELTA,  " Replace FKKOPBW_DELTA with extraction structure of your datasource
         WA_DFKKKO TYPE IT_DFKKKO.

 *  Copy the incoming data into another internal table. C_T_DATA contains all the extracted records.  

   L_T_DATA[] = C_T_DATA[].
   IF NOT L_T_DATA IS INITIAL.

 *   read the  fields from DFKKKO table for all entries of L_T_DATA and put them in to IT_DFKKKO

    SELECT OPBEL FIKEY ERNAM CPUTM WWERT RLGRD STBEL STMET BLTYP AGINF AWSYS TATYP
       FROM DFKKKO INTO TABLE ZBW_DFKKKO
       FOR ALL ENTRIES IN L_T_DATA
     WHERE OPBEL = L_T_DATA-OPBEL.
     LOOP AT L_T_DATA ASSIGNING <L_S_DATA>.
      READ TABLE ZBW_DFKKKO INTO WA_DFKKKO
         WITH KEY V_OPBEL = <L_S_DATA>-OPBEL.
      IF SY-SUBRC = 0.
        <L_S_DATA>-ZZFIKEY = WA_DFKKKO-V_FIKEY.
        <L_S_DATA>-ZZERNAM = WA_DFKKKO-V_ERNAM.
        <L_S_DATA>-ZZCPUTM = WA_DFKKKO-V_CPUTM.        
      ENDIF.
    ENDLOOP.
  ENDIF.

 * Now that we have filled new fileds, copy everything back to C_T_DATA which will be passed on to BW system

  C_T_DATA[] = L_T_DATA[].
 REFRESH: L_T_DATA,
         ZBW_DFKKKO.

 endmethod.

That's it. We have enhanced the datasource using BADI. The ABAP code in the above method will be exuceuted when we run the infopackage to load the data from the datasource. 

Go to transaction code RSA3 to test and make sure that everything is working fine.

If we want to enhance the datasource using user exit (CMOD), we can follow step by step procedure @ View the document

Read More (Reference Documents):

ABAP Development for SAP BW - User Exits and BAdis @ View the document)

How To Use ABAP Classes Instead of “CMOD” for BW Datasource Enhancements @ View the document

How to Implement BADI for Enhancing the Datasource in ECC @ View the document