Enhancement to a Standard Class

9
Enhancement to a Standard Class Sometimes we come across a requirement which cannot be solved using available standard methods. In such cases we can either enhance the existing method or go for our custom development. This document explains how to add a custom method to a standard global class and how to enhance an existing method using exits. Below are three different exits available to enhance a standard method: 1. Pre-Method Exit: Implement this exit if we want to execute our custom logic before standard method gets executed. 2. Post-Method Exit: Implement this exit if we want to execute our custom logic after the standard method gets executed. 3. Overwrite-Method Exit: If we want to completely replace the standard logic then this exit should be implemented. Steps 1) Go to transaction SE24 and open up a standard class (CL_SALV_TABLE) in our case. 2) Now select the option class->Enhance to create an enhancement implementation.

description

Enhancement to a Standard Class

Transcript of Enhancement to a Standard Class

Page 1: Enhancement to a Standard Class

Enhancement to a Standard Class

Sometimes we come across a requirement which cannot be solved using available standard methods. In such cases we can either enhance the existing method or go for our custom development. This document explains how to add a custom method to a standard global class and how to enhance an existing method using exits. Below are three different exits available to enhance a standard method:

1.     Pre-Method Exit: Implement this exit if we want to execute our custom logic before standard method gets executed.

2.     Post-Method Exit: Implement this exit if we want to execute our custom logic after the standard method gets executed.

3.     Overwrite-Method Exit: If we want to completely replace the standard logic then this exit should be implemented.

Steps

1)     Go to transaction SE24 and open up a standard class (CL_SALV_TABLE) in our case.

 

2)     Now select the option class->Enhance to create an enhancement implementation.

Page 2: Enhancement to a Standard Class

3)     A pop up screen will appear where we need to pass enhancement implementation name and description.

4)     Once the implementation gets created successfully a message will appear at the bottom of the screen and standard interface screen opens up in change mode.

5)     Now add a public method as shown below and implement the logic inside this method.

6)     We are going to display an information message inside this method.

Page 3: Enhancement to a Standard Class

7)     Now our next task is to add/modify the logic of existing standard method. For this put your cursor on the method name (DISPLAY in our case) and select the option Edit->Enhancement Operations->Insert Pre-Method / Insert Post-Method/Add Overwrite Method. Pre-Method gets called before the standard method and Post-Method gets called after the standard method. But if we want to completely replace the standard method logic with our won logic we can go for Over write method which gets called (if implementation is active) in place of standard method.

8)     We are going to implement the pre method first. So once we select the ‘Insert Pre-Method’ option, source code button    starts appearing on the Pre-Exit column. Press this button to implement the pre-method of display.

9)     In pre-method of display add logic to display an information message.

Page 4: Enhancement to a Standard Class

10)  Repeat steps 7 & 8 to implement Post-Exit method of display.

11)  Implement the post-exit method to display an information message.

12)  It should be noted that, if at this point we try to implement the over write exit, it will give a compilation error. This happens because we cannot have over write exit along with Pre/Post exit. 

13)  Now create a report with the below logic to display an ALV output using DISPLAY method of class CL_SALV_TABLE.

Page 5: Enhancement to a Standard Class

14)  When we execute the report with above logic, a pop up window gets displayed. This information message appears because of new method (MY_METHOD) which we have added to class CL_SALV_TABLE. Press enter to process further.

15)  New pop up will appear due to logic that has been implemented in Pre-Exit method of display. Press enter to process further.

16)  Now ALV output will get displayed. This is the normal case when we call DISPLAY method of class CL_SALV_TABLE.  Now press back button present at the top of the ALV screen.

Page 6: Enhancement to a Standard Class

17)  When we press the back button from ALV screen another pop up will appear and this comes due to logic we have implemented in post-exit method.

18)  In next step delete the pre/post exit implementations. This is required to implement the over write exit for display method.

Page 7: Enhancement to a Standard Class

19)  Now add over write exit for display method and implement its logic.

20)  Add logic to display an information message in over write exit method of display. It should be noted here that code in the upper part of the below screen shot got generated automatically. We just added the message logic in it.

21)  Now execute the below logic and see the results.

Page 8: Enhancement to a Standard Class

22)  Only a pop up window will appear and there will not be any ALV display. This happened because when we called the display( ) method from our report it calls the implemented over write exit method in place of standard display method.

Summary

To enhance logic of a standard method of a global class we can opt for the available exit:

         Pre-Method exit: Called before the standard method.

         Post-Method exit:  Called after the standard method.

         Overwrite Method exit:  Replaces the standard method.

Source Code

Create a report with below logic:

DATA: lt_sflight TYPE STANDARD TABLE OF sflight.

SELECT * FROM sflight INTO TABLE lt_sflight UP TO 10 ROWS.

DATA: l_or_alv TYPE REF TO cl_salv_table.

  TRY.  CALL METHOD CL_SALV_TABLE=>FACTORY    IMPORTING      R_SALV_TABLE   = l_or_alv    CHANGING      T_TABLE        = lt_sflight.   CATCH CX_SALV_MSG .  ENDTRY.

  l_or_alv->display( ).