Mastering Local Classes in SAP ABAP: A Step-by-Step Guide with Real-Life Example
- Local classes in SAP ABAP are classes that are defined within the context of a program or function module.
- Unlike global classes, which are defined independently and can be accessed from any ABAP program, local classes are only accessible within the program or function module where they are defined.
let’s take a real life requirement so that we can understand it more clearly.
Requirement :-
- Till our previous parts we have created methods in usual ABAP class and called that method in our program.
- Now, Our Requirement is to achieve the same to same requirement from local class.
- The customer wants that he would be giving a range of Sales document number on the selection screen and wants to see details of Sales Document from VBAK table and VBAP table on Output Screen.
Solution :-
-
Step 1 :- Go to ABAP Editor (SE38) and create a executable program.
-
Step 2 :- Create type structure for some fields from VBAK table and some fields from VBAP table and then write a final type structure for combining these fields.
-
Step 3 :- Create the respective internal table and work area for the above type structures.
-
Step 4 :- declare a select option to take input from the user.
-
Step 5 :- Declare a local class and declare a public method GET_DATA.
-
Step 6 :- Implement the method in class implementation.
Note :- We will write all the implementation part above and will store the final result in lt_final internal table which we have created above.
-
Part 1 :- Write the logic to fetch data from vbak and vbap.
-
Part 2 :- Store the data into final internal table.
-
-
Step 7 :- In Start of selection event create the object of our class.
-
Step 8 :- Call the above implemented method.
-
Step 9 :- Use factory method of CL_SALV_TABLE class to display the output in form of tables.
Code :-
*&---------------------------------------------------------------------*
*& Report ZAR_LOCAL_CLASS
*&---------------------------------------------------------------------*
*&
*&---------------------------------------------------------------------*
REPORT zar_local_class.
TYPES : BEGIN OF ty_vbak,
vbeln TYPE vbeln_va,
erdat TYPE erdat,
erzet TYPE erzet,
ernam TYPE ernam,
vbtyp TYPE vbtypl,
END OF ty_vbak.
TYPES : BEGIN OF ty_vbap,
vbeln TYPE vbeln_va,
posnr TYPE posnr_va,
matnr TYPE matnr,
END OF ty_vbap.
TYPES : BEGIN OF ty_final,
vbeln TYPE vbeln_va,
erdat TYPE erdat,
erzet TYPE erzet,
ernam TYPE ernam,
vbtyp TYPE vbtypl,
posnr TYPE posnr_va,
matnr TYPE matnr,
END OF ty_final.
*&---------------------------
*&Internal table and work area of for the same
*&-----------------------------------------------
DATA: lt_vbak type table of ty_vbak,
ls_vbak type ty_vbak,
lt_vbap type table of ty_vbap,
ls_vbap type ty_vbap,
lt_final type table of ty_final,
ls_final type ty_final.
*&--------------------------------
*&Declaring Select Options
*&--------------------------------
DATA : lv_vbeln type vbeln_va.
SELECT-OPTIONS : s_vbeln for lv_vbeln.
*&-------------------------------------------
*&Declaring local class
*&------------------------------------------
Class SALES_ORDER DEFINITION.
PUBLIC SECTION.
METHODS GET_DATA.
ENDCLASS.
*&------------------------------------------
*&Implementing the class
*&-------------------------------------------
CLASS SALES_ORDER IMPLEMENTATION.
METHOD GET_DATA.
Select vbeln erdat erzet ernam vbtyp
from vbak into table lt_vbak
where vbeln in s_vbeln.
IF lt_vbak is NOT INITIAL.
select vbeln posnr matnr
from vbap into table lt_vbap
FOR ALL ENTRIES IN lt_vbak
where vbeln = lt_vbak-vbeln.
endif.
LOOP at lt_vbap into ls_vbap.
ls_final-posnr = ls_vbap-posnr.
ls_final-matnr = ls_vbap-matnr.
Read table lt_vbak into ls_vbak with key vbeln = ls_vbap-vbeln.
if sy-subrc eq 0.
ls_final-vbeln = ls_vbak-vbeln.
ls_final-erdat = ls_vbak-erdat.
ls_final-erzet = ls_vbak-erzet.
ls_final-ernam = ls_vbak-ernam.
ls_final-vbtyp = ls_vbak-vbtyp.
endif.
append ls_final to lt_final.
clear ls_final.
ENDLOOP.
ENDMETHOD.
ENDCLASS.
START-OF-SELECTION.
DATA(lo_object) = new sales_order( ).
lo_object->get_data( ).
TRY.
CALL METHOD cl_salv_table=>factory
EXPORTING
list_display = IF_SALV_C_BOOL_SAP=>FALSE
IMPORTING
r_salv_table = data(lo_disp)
CHANGING
t_table = lt_final
.
CATCH cx_salv_msg.
ENDTRY.
lo_disp->display( ).
*&-------------------------------------------------------------
*&End of Program
*&-----------------------------------------------------------------
Execute the Program :-
- Press F8.
Comments
Post a Comment