[go: up one dir, main page]

0% found this document useful (0 votes)
606 views64 pages

BADI and Types of BADI in SAP BADI (Business Add-In)

This document discusses BADI (Business Add-in) in SAP and provides examples of using BADI to customize standard SAP functionality. It covers the following key points: - BADI allows customizing standard programs without modifying core code. BADI definitions define interfaces and BADI implementations provide the custom code. - Examples are provided for finding BADIs for transactions, implementing a BADI to validate vendor data, and testing the implemented BADI. - Different types of BADIs are described including single implementation, multiple implementation, and filter BADIs. - The use of ALV grids and function modules for displaying structured data in an interactive report format is demonstrated through an example ALV report on material

Uploaded by

Munnur Pandari
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as ODT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
606 views64 pages

BADI and Types of BADI in SAP BADI (Business Add-In)

This document discusses BADI (Business Add-in) in SAP and provides examples of using BADI to customize standard SAP functionality. It covers the following key points: - BADI allows customizing standard programs without modifying core code. BADI definitions define interfaces and BADI implementations provide the custom code. - Examples are provided for finding BADIs for transactions, implementing a BADI to validate vendor data, and testing the implemented BADI. - Different types of BADIs are described including single implementation, multiple implementation, and filter BADIs. - The use of ALV grids and function modules for displaying structured data in an interactive report format is demonstrated through an example ALV report on material

Uploaded by

Munnur Pandari
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as ODT, PDF, TXT or read online on Scribd
You are on page 1/ 64

BADI and types of BADI in SAP

BADI (Business Add-in)


BADI is another way of implementing enhancements to the standard programs with out
modifying the original code.
BADI's are implemented using oo programming technique Technically a BADI is nothing
but an interface.
Each BADI consists of the method with out implementation called as BADI definition.
We need to create classes to write the abap code by implementing the methods called as
BADI implementation.
SE18 is the T-code for BADI definition, SE19 is the T-code for BADI implementation.
Advantages of BADI
The main advantage of using BADI's is , we can create multiple implementations for a
single BADI definition.
Where as with the exits, we can create a single implementation.i.e a single project for a
enhancement.
We cannot create another project (implementation) for enhancement which is already used.
That is why we go for BADI's.
Types of BADI
Single implementation BADI.
Multiple implementation BADI.
Filter BADI.
Custom BADI (Not used/Rarely used)
Single implementation BADI:- A BADI which has only one implementation (single class) is called
single implementation BADI.
Multiple implementation BADI:- A BADI which has multiple implementations is called multiple
implementation BADI. By default all the implementations will be executed.
We cannot control the sequence of execution of multiple implementations.
Filter BADI It is type of BADI which has a filter value so that only those implementations which
satisfy the filter value are executed. The remaining implementations are not executed this type of
BADI is called a filter BADI.
Properties of BADI
If WITH IN SAP checkbox is selected then this BADI is only used by sap.
If multiple use checkbox is selected then it is a multiple implementation BADI.
If multiple use checkbox is not selected then it is a single implementation BADI.
If filter dependent checkbox is selected then it is filter dependent BADI. We need to specify
the filter type such as land1,bukrs,werks.for the filter BADI.

Finding BADI for a T-Code in SAP


There are two ways of finding BADI for transactions.
1. Using standard class CL_EXITHANDLER.
2. Using SE84 T-code.
Using CL_EXITHANDLER Class to find BADI
CL_EXITHANDLER is a class in SAP, it will trigger for each and every transaction in SAP, in this
class there is a method called GET_INSTANCE, by using this method we can find the list of
BADI`s for a transaction.

Steps to find BADI using CL_EXITHANDLER


Go to T-code SE24,provide class name as CL_EXITHANDLER and display.
Double click on the method GET_INSTANCE and put a break-point on the below method
GET_CLASS_NAME_BY_INTERFACE.
Execute the transaction (example VA01), the break-point will trigger, note down the values that
comes to variable exit_name. (Press F8 to get next exit name, continue process until screen
displays).

Using SE84 to find BADI


Find package for a T-code.Go to SE93, provide t-code name(VA01), display.
Go to SE84, expand enhancements, expand Business Add-ins and select definition.
Provide package name and execute, you will find list of BADI`s.

Real time example of using BADI in SAP


Requirement: Raise an error message whenever the vendor belongs to
German and region is not specified.
Requirement analysis In SAP vendors are created using T-code XK01, the created vendors will be
stored in LFA1 (Vendor Master table), so we need to add custom functionality to validate region
when creating vendor using XK01 (also when changing in XK02),to do this we go for BADI.
To implement the BADI for XK01 we need to follow the below steps.
Step1: Find the BADI.
Step2: Select suitable BADI .
Step3: Implement the BADI.
Step4: Test the BADI.

Find the BADI for XK01/XK02


First step to implement BADI is finding the badi
.
Go to SE24 and put the break point in GET_INSTANCE method of CL_EXITHANDLER class.
Go to LFA1, take a vendor no and go to XK02, provide vendor no, enter.

You will go to next screen, change any one value and click on Save (Ctrl S).
The break-point will be triggered, note down all BADI names.

Below are the list of BADI`s you will get


VENDOR_FIELDSTATUS
GOS_SRV_SELECT
ADDRESS_CHECK
ADDR_TXJCD_CHECK
CVI_CALL_BTE
ADDRESS_UPDATE
VENDOR_ADD_DATA

Select the suitable BADI and verify the BADI


To select the suitable BADI, go to SE18, provide BADI name, display

Based on description and interface signature (importing and exporting parameters) we can choose
and verify some BADI`s
Now VENDOR_ADD_DATA will be useful for our requirement.

Implement the BADI


BADI uses object oriented concepts, BADI is nothing but a interface, to implement the BADI, we
need to create implementation for the interface.
To implement the BADI go to SE19(BADI Builder), provide BADI name, create implementation.
A popup will open, provide a implementaion name ex: ZVENDOR_ADD_DATA, continue.

Provide short description, based on description choose a method and double click to add owr own
code.

Here CHECK_ALL_DATA will be useful for final checks, so double click on


CHECK_ALL_DATA.
METHOD IF_EX_VENDER_ADD_DATA~CHECK_ALL_DATA.
BREAK-POINT.
ENDMETHOD.

Go to XK02, provide vendor no, select address check box, change any one, save. Now our break
point will trigger, so we can implement this because this is very much suitable for our requirement.
Remove the break point and write the below code for implementing exit.
METHOD IF_EX_VENDER_ADD_DATA~CHECK_ALL_DATA.
IF I_LFA1=LAND1= 'DE' AND I_LFA1-REGIO= ' '.
MESSAGE 'Region is required for German Vendors' TYPE 'E'.
ENDMETHOD.

Save, activate the method, go back activate the implementation.


Now BADI is implemented.
Test the BADI
To test the BADI, go to XK02, provide a vendor no, enter.
Change country to DE and Region to Space.

Save (Ctrl S).


You will get a error message.

The BADI is implemented successfully.

SAP ABAP ALV Reports


ALV (ABAP List Viewer) is pre-defined report format in SAP. Advantages of ALV Reports

Better look and feel.


ALV report consists of some pre-defined options like sort, filters, sum, downloading, print,
changing the layout structure and many more.
List of Function Modules used to develop ALV reports
The below are the list of Function Modules we use to display ALV reports in different formats.
REUSE_ALV_GRID_DISPLAY. "Display ALV grid format
REUSE_ALV_LIST_DISPLAY. "Display ALV List format
REUSE_ALV_COMMENTARY_WRITE. "Display Top of page, logo, etc.
REUSE_ALV_FIELDCATELOGUE_MERGE. "Used to generate field catalogue
REUSE_ALV_EVENTS_GET. "Use events in ALV
REUSE_ALV_HEIRARCHY_LIST_DISPLAY. "Display ALV Hierarchy
REUSE_ALV_BLOCKED_LIST_DISPLAY. "Display blocked list
ALV Report with Structure in SAP ABAP
Requirement: Develop an ALV report to display material details with all fields from MARA table
for a material range input(Select-Options input).

In this requirement, we have to display all fields from MARA (Material master table) in ALV
format. We use REUSE_ALV_GRID_DISPLAY Function module to display an ALV report.

Steps to create ALV Report with Structure.


Step1: Declare Internal table for MARA table.
Step2: Print Select-Options.
Step3: Get data from database using select statements.
Step4: Call Function Module REUSE_ALV_GRID_DISPLAY and pass structure name, program
name and internal table name.
TABLES : MARA. "DECLARE TABLE FOR SELECT-OPTIONS
DATA : IT_MARA TYPE TABLE OF MARA. "DECLARE INTERNAL TABLE FOR MARA
DATA : WA_MARA TYPE MARA.
SELECT-OPTIONS: S_MATNR FOR MARA-MATNR. "PRINT SELECT-OPTIONS FOR MATNR
START-OF-SELECTION.
SELECT * FROM MARA INTO TABLE IT_MARA
WHERE MATNR IN S_MATNR .

CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY' "CALL FUNCTION MODULE TO DISPLAY ALV GRID


EXPORTING
I_CALLBACK_PROGRAM = SY-REPID "SY-REPID IS A SYSTEM VARIABLE WHICH STORES
CURRENT PROGRAM NAME
I_STRUCTURE_NAME = 'MARA'
TABLES
T_OUTTAB = IT_MARA. "PASS INTERNAL TABLE TO DISPLAY ALV FORMAT

The above report after modularization


The one below is the full report after using modularization techniques.
*&---------------------------------------------------------------------*
*& Report ZALV_STR
*&
*&---------------------------------------------------------------------*

REPORT ZALV_STR.
TABLES: MARA.
DATA : IT_MARA TYPE TABLE OF MARA .

SELECT-OPTIONS : S_MATNR FOR MARA-MATNR.

START-OF-SELECTION .
PERFORM GET_MARA_DATA . "Double click to create below form

END-OF-SELECTION .
PERFORM DISP_MARA_ALV . "Double click to create below form

*&---------------------------------------------------------------------*
*& Form GET_MARA_DATA
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
FORM GET_MARA_DATA .
SELECT * FROM MARA
INTO TABLE IT_MARA
WHERE MATNR IN S_MATNR
.
ENDFORM. " GET__MARA_DATA
*&---------------------------------------------------------------------*
*& Form DISP_MARA_ALV
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
FORM DISP_MARA_ALV .
CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
EXPORTING
I_CALLBACK_PROGRAM = SY-REPID
I_STRUCTURE_NAME = 'MARA'
TABLES
T_OUTTAB = IT_MARA.

ENDFORM. " DISP_MARA_ALV

ALV Report with Field Catalog SAP ABAP


Field Catalog
Field catalog is an internal table which is used to pass a list of fields to display in ALV report, we
can set different properties to fields which are going to display in ALV.

Type Group
It is a data dictionary object which contains all the reusable user-defined types.
Example for a type group is SLIS, which contains all the user-defined types for developing ALV
reports.
TYPE-POOLS is a keyword which is used to assign the type-group to a ALV report .
Syntax :
TYPE-POOLS SLIS . "TO USE FIELD CATALOG WE HAVE TO INCLUDE SLIS TYPE-POOLS
DATA : <IT_FCAT> TYPE SLIS_T_FIELDCAT_ALV . "INTERNAL TABLE FOR FIELD CATALOG
DATA : <WA_FCAT> TYPE SLIS_FIELDCAT_ALV . " WORK AREA FOR FIELD CATLOG

Options/properties of field catalog.


WA_FCAT-COL_POS = '1' . "Specify position of a field
WA_FCAT-FIELDNAME = 'MATNR' . "Specify field name
WA_FCAT-TABNAME = 'IT_MARA' . "Specify internal table name
WA_FCAT-SELTEXT_M = 'MATERIALNO' . "Specify text to display column header
WA_FCAT-KEY = 'X' . "Specify if it is a key field
APPEND WA_FCAT TO IT_FCAT . "Append to field catalog internal table

Useful resource Using append in SAP ABAP and also Using Clear in SAP ABAP
Requirement: Develop an ALV report to display Material no (MATNR), Material type (MTART),
Industry Sector (MBRSH) and Basic Unit Of measure (MEINS) for a range of material input
(Select-Options).
To develop above report, we have to use field catalog (because we have to display four fields only
from MARA) and we have to pass field catalog parameter to Function Module
REUSE_ALV_GIRD_DISPLAY.
REPORT ZSAPN_ALV_FCAT.
TABLES : MARA.
TYPE-POOLS SLIS .

TYPES : BEGIN OF TY_MARA, "User defined internal table type


MATNR TYPE MARA-MATNR,
MTART TYPE MARA-MTART,
MBRSH TYPE MARA-MBRSH,
MEINS TYPE MARA-MEINS,
END OF TY_MARA.

DATA : IT_MARA TYPE TABLE OF TY_MARA .


DATA : WA_MARA TYPE TY_MARA .

DATA : IT_FCAT TYPE SLIS_T_FIELDCAT_ALV .


DATA : WA_FCAT LIKE LINE OF IT_FCAT .
SELECT-OPTIONS: S_MATNR FOR MARA-MATNR.

START-OF-SELECTION .
PERFORM GET_DATA .
PERFORM CREATE_FCAT.

END-OF-SELECTION .
PERFORM DISP_ALV .

*&---------------------------------------------------------------------*
*& Form GET_DATA
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
FORM GET_DATA .
SELECT MATNR MTART MBRSH MEINS FROM MARA
INTO TABLE IT_MARA
WHERE MATNR IN S_MATNR.
.
ENDFORM. " GET_DATA
*&---------------------------------------------------------------------*
*& Form DISP_ALV
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
FORM DISP_ALV .
CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
EXPORTING
I_CALLBACK_PROGRAM = SY-REPID
IT_FIELDCAT = IT_FCAT "PASS FIELD CATALOG TO ALV
TABLES
T_OUTTAB = IT_MARA.

ENDFORM. " DISP_ALV


*&---------------------------------------------------------------------*
*& Form CREATE_FCAT
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
FORM CREATE_FCAT .
WA_FCAT-COL_POS = '1' .
WA_FCAT-FIELDNAME = 'MATNR' .
WA_FCAT-TABNAME = 'IT_MARA' .
WA_FCAT-SELTEXT_M = 'MATERIALNO' .
WA_FCAT-KEY = 'X' .
APPEND WA_FCAT TO IT_FCAT .
CLEAR WA_FCAT .

WA_FCAT-COL_POS = '2' .
WA_FCAT-FIELDNAME = 'MTART' .
WA_FCAT-TABNAME = 'IT_MARA' .
WA_FCAT-SELTEXT_M = 'MATERIALTYPE' .
* WA_FCAT-NO_OUT = 'X' .
WA_FCAT-HOTSPOT = 'X' .
APPEND WA_FCAT TO IT_FCAT .
CLEAR WA_FCAT .

WA_FCAT-COL_POS = '3' .
WA_FCAT-FIELDNAME = 'MBRSH' .
WA_FCAT-REF_FIELDNAME = 'MBRSH' .
WA_FCAT-REF_TABNAME = 'MARA' .
* WA_FCAT-TABNAME = 'IT_MARA' .
* WA_FCAT-SELTEXT_M = 'INDSECTOR' .
* WA_FCAT-EDIT = 'X' .
APPEND WA_FCAT TO IT_FCAT .
CLEAR WA_FCAT .

WA_FCAT-COL_POS = '4' .
WA_FCAT-FIELDNAME = 'MEINS' .
WA_FCAT-TABNAME = 'IT_MARA' .
WA_FCAT-SELTEXT_M = 'MAT.UNITS' .
WA_FCAT-EMPHASIZE = 'C610'.
APPEND WA_FCAT TO IT_FCAT .
CLEAR WA_FCAT .

ENDFORM. " CREATE_FCAT

ALV Report with layout SAP ABAP


Layout:
Layout is a structure which is used to decorate or embellish the output of ALV Report.
Requirement: Display list of materials for a material type, with all fields of output is editable, no
horizontal and vertical lines and hotspot on material no.
Input Screen

Out put Screen (all rows are editable and hotspot on material no)

Please follow previous lesson steps to create a fieldcatlog in ALV, in addition to that we will be
adding additional properties hotspot, editable to output fields using layout options.
The below is the code to add layout to ALV.
DATA : WA_LAYOUT TYPE SLIS_LAYOUT_ALV .
WA_LAYOUT-ZEBRA = 'X' .
WA_LAYOUT-COLWIDTH_OPTIMIZE = 'X' .
WA_LAYOUT-EDIT = 'X' .
WA_LAYOUT-NO_VLINE = 'X' .
WA_LAYOUT-NO_HLINE = 'X' .

We will supply remaining options through fieldcatlog also.

Full referance code for using layout in ALV


REPORT ZSAPN_ALV_LAYOUT.
TYPE-POOLS : SLIS .
*DATA DECLARAATIONS
DATA : I_MARA TYPE TABLE OF MARA .
DATA : WA_MARA TYPE MARA .
*ALV DECLARATIONS
DATA : I_FCAT TYPE SLIS_T_FIELDCAT_ALV .
DATA : WA_FCAT TYPE SLIS_FIELDCAT_ALV .
DATA : V_POS TYPE I .
DATA : WA_LAYOUT TYPE SLIS_LAYOUT_ALV .
PARAMETERS : P_MTART TYPE MARA-MTART.

START-OF-SELECTION .
PERFORM GET_DATA .
PERFORM CREATE_FCAT .
PERFORM CREATE_LAYOUT.
PERFORM DISPLAY_DATA .
END-OF-SELECTION .
FORM GET_DATA .

SELECT * FROM MARA


INTO TABLE I_MARA
UP TO 100 ROWS WHERE MTART = P_MTART .
ENDFORM. " GET_DATA
FORM DISPLAY_DATA .

CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'


EXPORTING
I_CALLBACK_PROGRAM = SY-REPID
IS_LAYOUT = WA_LAYOUT
IT_FIELDCAT = I_FCAT
TABLES
T_OUTTAB = I_MARA.
ENDFORM. " DISPLAY_DATA
FORM DISPLAY_DATA_LIST .
CALL FUNCTION 'REUSE_ALV_LIST_DISPLAY'
EXPORTING
I_STRUCTURE_NAME = 'MARA'
TABLES
T_OUTTAB = I_MARA.
IF SY-SUBRC NE 0.
* MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
* WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
ENDIF.

ENDFORM. " DISPLAY_DATA_LIST


FORM CREATE_FCAT .
V_POS = 1 .
WA_FCAT-COL_POS = V_POS.
WA_FCAT-FIELDNAME = 'MATNR' .
WA_FCAT-SELTEXT_M = 'Material.NO' .
WA_FCAT-HOTSPOT = 'X' .
APPEND WA_FCAT TO I_FCAT .
CLEAR WA_FCAT .

V_POS = V_POS 1.
WA_FCAT-COL_POS = V_POS .
WA_FCAT-FIELDNAME = 'MTART' .
WA_FCAT-SELTEXT_M = 'Material.Type' .
APPEND WA_FCAT TO I_FCAT .
CLEAR WA_FCAT .

V_POS = V_POS 1.
WA_FCAT-COL_POS = V_POS .
WA_FCAT-FIELDNAME = 'MBRSH' .
WA_FCAT-SELTEXT_M = 'Ind.Sector' .
APPEND WA_FCAT TO I_FCAT .
CLEAR WA_FCAT .

V_POS = V_POS 1.
WA_FCAT-COL_POS = V_POS .
WA_FCAT-FIELDNAME = 'MATKL' .
WA_FCAT-SELTEXT_M = 'Mat.Grp' .
APPEND WA_FCAT TO I_FCAT .
CLEAR WA_FCAT .

V_POS = V_POS 1.
WA_FCAT-COL_POS = V_POS .
WA_FCAT-FIELDNAME = 'MEINS' .
WA_FCAT-SELTEXT_M = 'Units' .
APPEND WA_FCAT TO I_FCAT .
CLEAR WA_FCAT .

ENDFORM. " CREATE_FCAT


FORM CREATE_LAYOUT .
WA_LAYOUT-ZEBRA = 'X' .
WA_LAYOUT-COLWIDTH_OPTIMIZE = 'X' .
WA_LAYOUT-EDIT = 'X' .
WA_LAYOUT-NO_VLINE = 'X' .
WA_LAYOUT-NO_HLINE = 'X' .

ENDFORM. " CREATE_LAYOUT

Dynamic ALV to display SE11 table data


In the below example, we are going to display SE11 table data (ant
transparent table) in the form if ALV, follow the below steps.
Step1: Create field catalog for the table.

Step2: Create dynamic table.

Step3: Get table data.

Step4: Display ALV.


Step1: build field catalog for the input table.
PARAMETERS: P_TABLE TYPE DD02L-TABNAME. "table input

START-OF-SELECTION.
CALL FUNCTION 'LVC_FIELDCATALOG_MERGE' "create field catalog
EXPORTING
I_STRUCTURE_NAME = P_TABLE
CHANGING
CT_FIELDCAT = IT_FCAT.

Step2: Create dynamic table.


Create dynamic table using 'CREATE_DYNAMIC_TABLE' method of class
'CL_ALV_TABLE_CREATE'.
DATA : FS_TAB TYPE REF TO DATA.
CALL METHOD CL_ALV_TABLE_CREATE=>CREATE_DYNAMIC_TABLE
EXPORTING
* I_STYLE_TABLE =
IT_FIELDCATALOG = IT_FCAT
* I_LENGTH_IN_BYTE =
IMPORTING
EP_TABLE = FS_TAB.

Get data from table using select statement


Get data from table using select statements.
FIELD-SYMBOLS: <fs_tab> TYPE STANDARD TABLE. "declare field symbol
ASSIGN FS_TAB->* TO <fs_tab>. "assign field symbol memory
SELECT * FROM (P_TABLE) INTO CORRESPONDING FIELDS OF TABLE <fs_tab> UP TO 50
ROWS. "get data

Step4:Display ALV
Display data in ALV using field catalog and table.
CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
EXPORTING
I_STRUCTURE_NAME = P_TABLE
TABLES
T_OUTTAB = <fs_tab>.

Final Code will be


REPORT ZSAN_RFCTEST.
DATA : IT_FCAT TYPE LVC_T_FCAT.
PARAMETERS: P_TABLE TYPE DD02L-TABNAME.
DATA : WA_TABLE TYPE DD02L.

AT SELECTION-SCREEN.
IF P_TABLE IS NOT INITIAL.
SELECT SINGLE * FROM DD02L INTO WA_TABLE WHERE TABNAME = P_TABLE AND
TABCLASS = 'TRANSP'.
ELSE.
MESSAGE 'Please enter a table' TYPE 'E'.
STOP.
ENDIF.
IF WA_TABLE IS INITIAL.
MESSAGE 'Table Dosent exit or is not transparent table' TYPE 'E'.
STOP.
ENDIF.

START-OF-SELECTION.
CALL FUNCTION 'LVC_FIELDCATALOG_MERGE'
EXPORTING
I_STRUCTURE_NAME = P_TABLE
CHANGING
CT_FIELDCAT = IT_FCAT.

DATA : FS_TAB TYPE REF TO DATA.


CALL METHOD CL_ALV_TABLE_CREATE=>CREATE_DYNAMIC_TABLE
EXPORTING
* I_STYLE_TABLE =
IT_FIELDCATALOG = IT_FCAT
* I_LENGTH_IN_BYTE =
IMPORTING
EP_TABLE = FS_TAB.

FIELD-SYMBOLS: <FS_TAB> TYPE STANDARD TABLE.


ASSIGN FS_TAB->* TO <FS_TAB>.
*data : it_tab type TABLE OF p_table.
SELECT * FROM (P_TABLE) INTO CORRESPONDING FIELDS OF TABLE <FS_TAB> UP TO 50
ROWS.
CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
EXPORTING
I_STRUCTURE_NAME = P_TABLE
TABLES
T_OUTTAB = <FS_TAB>.
IF SY-SUBRC <> 0.
* Implement suitable error handling here
ENDIF.

ALV Blocked List Display


Blocked list ALV is used to display multiple ALV`s on the same screen with
blocks.

List of Function Modules used for blocked list ALV


REUSE_ALV_BLOCK_LIST_INIT
REUSE_ALV_BLOCK_LIST_APPEND
REUSE_ALV_BLOCK_LIST_DISPLAY
REUSE_ALV_BLOCK_LIST_INIT: is used to initialize blocked list ALV.
REUSE_ALV_BLOCK_LIST_APPEND: is used to add blocked list ALV's(we can add multiple).
REUSE_ALV_BLOCK_LIST_DISPLAY: is used to display blocked list ALV.

Hide ALV toolbar buttons using ALV Exclude


Sometimes, we may need to hide some ALV toolbar buttons in ALV grid
Display using REUSE_ALV_GRID_DISPLAY.
The below are the steps we are going to do to hide ALV tool bar buttons.
1. Get Standard GUI Function Codes.
2. Exclude ALV toolbar functions using IT_EXCLUDE exporting parameter.

Get Standard GUI function codes


We can get standard GUI function codes using function module
RS_CUA_GET_STATUS_FUNCTIONS bypassing standard program SAPLKKBL and
statusSTANDARD_FULLSCREEN.
DATA : FUN TYPE TABLE OF RSEUL_FUN.
DATA : WA_FUN LIKE LINE OF FUN.
CALL FUNCTION 'RS_CUA_GET_STATUS_FUNCTIONS'
EXPORTING
LANGUAGE = 'E'
PROGRAM = 'SAPLKKBL'
STATUS = 'STANDARD_FULLSCREEN'
TABLES
FUNCTIONS = FUN.
IF SY-SUBRC <> 0.
* Implement suitable error handling here
ENDIF.

Exclude functions using IT_EXCLUDE parameter of ALV grid


Loop through the standard GUI function which we got in the previous step, and pass to exclude
table.
DATA : IT_EXCLUDE TYPE SLIS_T_EXTAB,
WA_EXCLUDE TYPE SLIS_EXTAB.
LOOP AT FUN INTO WA_FUN. "loop through all functions
IF WA_FUN-FCODE EQ '&F03' OR WA_FUN-FCODE EQ '&F15' OR WA_FUN-FCODE EQ
'&F12'. "don`t add back, exit, stop functions

ELSE.
WA_EXCLUDE-FCODE = WA_FUN-FCODE. "add all remaining
APPEND WA_EXCLUDE TO IT_EXCLUDE.
CLEAR WA_EXCLUDE.
ENDIF.
ENDLOOP.

Full and Final Code


REPORT ZSAPN_ALV_FUNCTIONS.
DATA: IT_MARA TYPE TABLE OF MARA,
WA_MARA TYPE MARA.
DATA : FUN TYPE TABLE OF RSEUL_FUN.
DATA : WA_FUN LIKE LINE OF FUN.
DATA : IT_EXCLUDE TYPE SLIS_T_EXTAB,
WA_EXCLUDE TYPE SLIS_EXTAB.

START-OF-SELECTION.
SELECT * FROM MARA INTO TABLE IT_MARA UP TO 50 ROWS.

CALL FUNCTION 'RS_CUA_GET_STATUS_FUNCTIONS'


EXPORTING
LANGUAGE = 'E'
PROGRAM = 'SAPLKKBL'
STATUS = 'STANDARD_FULLSCREEN'
TABLES
FUNCTIONS = FUN.
IF SY-SUBRC <> 0.
* Implement suitable error handling here
ENDIF.

*&---------------------------------------------------------------------*
* FOR EXCLUDING STANDARD BUTTONS FROM ALV TOOLBAR
*&---------------------------------------------------------------------*
LOOP AT FUN INTO WA_FUN. "loop through all functions
IF WA_FUN-FCODE EQ '&F03' OR WA_FUN-FCODE EQ '&F15' OR WA_FUN-FCODE EQ
'&F12'. "don`t add back, exit, stop functions

ELSE.
WA_EXCLUDE-FCODE = WA_FUN-FCODE. "add all remaining
APPEND WA_EXCLUDE TO IT_EXCLUDE.
CLEAR WA_EXCLUDE.
ENDIF.
ENDLOOP.

CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'


EXPORTING
* I_CALLBACK_PF_STATUS_SET = 'PF_STATUS'
I_STRUCTURE_NAME = 'MARA'
IT_EXCLUDING = IT_EXCLUDE "exclude functions
TABLES
T_OUTTAB = IT_MARA. "MARA table data
IF SY-SUBRC <> 0.
* Implement suitable error handling here
ENDIF.

SAP Smartforms
Smartform
Smart form is a GUI Tool which is used to design the business legal documents such as
Delivery note,Purchase order,Invoice etc.
The transaction code is SMARTFORMS.
Smartforms are client independent objects.
Smartforms are advanced version of SAP Scripts.
It is a GUI tool and it is user friendly.
Smartform architecture:

Smatrtforms and SAP Scripts differences .


Smartforms SAP Scripts
SAP Scripts are client
Smartforms are client independent.
dependent.
Main window is
Main window is not compulsory.
compulsory.
No Function Module will
Smartforms generates a Function module when activated.
be generated.
Scripts allows black and
Smartforms texts supports various colors.
white colors only.
There is no transfer of control between program and form, once the Scripts has repeated
control is transfered to Function Module, it will never come back. transfer of control.
Multiple page formats are
Only single page format is possible.
available.

COMPONENTS OF SMARTFORMS.
There are two main nodes in Smartforms.
1.Global Settings
Form Attribute.
Form Interface.
Global Definition.
2.Pages and Windows.

Global settings:
It is used to provide the basic settings for the smartform.

Form Attributes:
It specifies the general attributes like who created,Date,time, package,translate options, default
smartstyle, Page format (DINA4/DINA5).

Form interface:
It acts as a mediator between a driver program and a smart form.
The main functionality of form interface is, it will import the parameters which are exported by
driver program.
The parameters can be variables, work areas, internal tables..etc.

Global definition:
It will contain the variables to be used within the smart form.
We can define variables, user defined data types, field symbols, initialization code,Subroutine,
currency/quantity fields.
It is mainly used for declaring or defining the above variables.

Pages and Windows


Page.
Window.
Graphic.
Address.
Text.
Table.
Header.
Body.
Footer.
Template.
Folder.
Program Lines.
Alternative.
Command.
Loop.

Pages and windows


This will contain all the pages and the windows, which are used in the smartform.
By default, a page will be created by name %page1.
By default, a window will be created by name 'Main' under the page1.
For each page, we need to specify the next page.
Eg: page = %page1
Next page = %page1 or %page2

Nodes under pages


Page : It is used to define the layout of a smartform.
We can have different pages with different layouts.
Window : it is used to display information or text at a particular place on a page.
Graphic : it is used to display logos or images on the smartforms.
Address : It is used to display the address of customer or vendor or employe, organization address ,
workplace address etc.
Just provide address no, it will automatically display the address as per the country formats.
Nodes under the window
Text : it is used to display the information or text in a window.
Table : It is used to display the information in the form of table.
When ever we create table by default HEADER, MAIN AREA, FOOTER will be displayed.
The main functionality of a table is, it expands automatically depending on the internal table data.
We need to provide the name of the internal table for a table.
Template : template is also like a table which does not expand. That means it will have fixed
number of rows and columns.
Program lines : it is used to write some lines of abap code.

Window Types in Smartforms


Main window: For continuous output .
Secondary window: For output with a fixed length.
Final window: Special type of secondary window for outputting the information that is not known
until the end of form processing .
Copies window: Special type of secondary window for marking pages as copy or original.
Example Smartform GUI

Simple Smartform to Display Text


Go to SMARTFORMS T-code.

Provide form as ZSAPN_TEXT, click on create.


You will find screen below.

Expand page, right click on min window -> create -> Text.

Go to general attributes tab, add some text.

Click save, save it in a local object, activate and click on execute.

Whenever we activate a smartfrom, one function module will be generated, when we execute it, it
will go to function module.

Just click execute (F8).


And again press F8, execute.

Provide output device as LP01, click on print preview.


STRING OPERATIONS AND FIELD SYMBOLS

Field symbols are similar to pointers in C language, field symbols does not
have any memory instead they will be pointing to a memory location.
The concept of field symbols is very important in order to increase the performance of SAP
applications, but unethical use of field-symbols leads to application issues.
Field symbol name should always be within <>, example:.
Syntax for declaring a field symbol.
FIELD-SYMBOLS : <FIELD_SYMBOL> TYPE MARA-MATNR. "here MARA-MATNR is a variable
type
FIELD-SYMBOLS : <FIELD_SYMBOL> TYPE MARA. "here MARA is a structure
FIELD-SYMBOLS : <FIELD_SYMBOL> TYPE REF TO DATA . "here DATA is a reference type

ASSIGNING and ASSIGN are the keywords which are used to assign a value to the field symbol.

Example of using field symbol as work area


In the below example we are going to use field symbol as work area.
REPORT ZSAPN_FIELDSYMBOLS.

DATA : IT_MARA TYPE TABLE OF MARA.


DATA
FIELD-SYMBOLS : <FS_MARA> TYPE MARA.
SELECT * FROM MARA
INTO TABLE IT_MARA UP TO 50 ROWS.

LOOP AT IT_MARA ASSIGNING <FS_MARA>.


IF <FS_MARA> IS ASSIGNED.
WRITE :/ <FS_MARA>-MATNR, <FS_MARA>-MTART, <FS_MARA>-MEINS.
ENDIF.
ENDLOOP.

Check weather the field symbol is assigned or not before processing field symbol, it is very
important to check field symbol, if it is not assigned it will get a run-time error, use the blow
syntax to check field symbol assignment.
IF <FS_MARA> IS ASSIGNED.
**process field symbol
ENDIF.

Using SPLIT in SAP ABAP


SPLIT is a key word which is used to cut a string into pieces at a specified value.
Keyword syntax is SPLIT AT '<VALUE> INTO < IT_TABLE >

Example of using SPLIT in SAP ABAP


REPORT ZSAPN_STRING.
DATA : LV_STRING TYPE STRING . "declaration for main string
DATA : LV_STRING1 TYPE STRING, "declaration for splitting string into
LV_STRING2 TYPE STRING, "declaration for splitting string into
LV_STRING3 TYPE STRING, "declaration for splitting string into
LV_STRING4 TYPE STRING. "declaration for splitting string into
LV_STRING = 'SPLIT ME AT SPACE'. "main string value
SPLIT LV_STRING AT ' ' INTO LV_STRING1 LV_STRING2 LV_STRING3 LV_STRING4. "split

the main string into specified fields at space


WRITE :/ LV_STRING1. "print split fields
WRITE :/ LV_STRING2.
WRITE :/ LV_STRING3.
WRITE :/ LV_STRING4.

Example of using SPLIT into internal table


Instead of splitting the data into individual fields we can split data into an internal table.
REPORT ZSAPN_STRING.

DATA : LV_STRING TYPE STRING .


TYPES: BEGIN OF TY_STRING,

STR(25) TYPE C,
END OF TY_STRING.
DATA IT_STRING TYPE TABLE OF TY_STRING.
DATA WA_STRING TYPE TY_STRING .
LV_STRING = 'SPLIT ME AT SPACE'.
SPLIT LV_STRING AT ' ' INTO TABLE IT_STRING .

LOOP AT IT_STRING INTO WA_STRING.


WRITE :/ WA_STRING-STR.
ENDLOOP.

CONCATENATE is a string function which is used to add different variables into a single string
variable.
Syntax : CONCATENATE ... INTO .

Example of Using CONCATENATE


REPORT ZSAPN_CONCATENATE.
DATA : STR1 TYPE STRING.
DATA : STR2 TYPE STRING.
DATA : STR3 TYPE STRING.

DATA : STRING TYPE STRING.


STR1 = 'A'.
STR2 = 'B'.
STR3 = 'C'.
CONCATENATE STR1 STR2 STR3 INTO STRING. "Concatenate into string

WRITE :/ STRING .
*The out put will be 'ABC'

CONCATENATE with saperated by


REPORT ZSAPN_CONCATENATE.
DATA : STR1 TYPE STRING.
DATA : STR2 TYPE STRING.
DATA : STR3 TYPE STRING.

DATA : STRING TYPE STRING.


STR1 = 'THIS IS'.
STR2 = 'EXAMPLE OF'.
STR3 = 'CONCATENATE'.
CONCATENATE STR1 STR2 STR3 INTO STRING SEPARATED BY ' '. "Concatenate into
string separated by space

WRITE :/ STRING .
*The out put will be 'THIS IS EXAMPLE OF CONCATENATE'

TRANSLATE is a string function which is used to change the case of a string i:e from lower case
to upper case and upper case to lower.
TRANSLATE TO .

Example of Using TRANSLATE


REPORT ZSAPN_TRANSLATE
.
DATA : LV_STRING TYPE STRING.
LV_STRING = 'small to upper case'.
TRANSLATE LV_STRING TO UPPER CASE.
WRITE : / LV_STRING.

CONDENSE is a string function/keyword which is used to remove blank spaces in a given string.

Syntax: CONDENSE .

Example of using CONDENSE


ZSAPN_CONSENSE.
DATA LV_STRING TYPE STRING.
LV_STRING = 'THIS IS STRING WITH SPACES'.
CONDENSE LV_STRING.
WRITE : LV_STRING. "out put will be THISISSTRINGWITHSPACES

The keyword/ string function STRLEN is used to determine the length of a string in SAP ABAP
programs.
Syntax: STRLEN( ).

Using STRLEN to find a string length


REPORT ZSAPN_STRLEN.
DATA : STRING TYPE STRING.
DATA LV_LENGTH TYPE I . "variable to store length
STRING = 'THIS IS SOME STRING'.
LV_LENGTH = STRLEN( STRING ).
WRITE : LV_LENGTH. "out put will be 19

Run-time analysis
Run-time analysis is used to check the efficiency of a program or function module or t-code
in terms of what is the load on the database server, application server, presentation server
etc.
The run-time analysis will display the load in a graph with %'s and the time in micro
seconds.
The graph will be displayed with either red color or green color.
If the graph contains green color, then the program execution time is very good or very less.
If the graph contains red color, the program execution time is very bad or very long .
Always make sure the the load on database server should be less than 40% and should be
green.
T-code for run-time analysis is SE30 or SAT(Latest Versions).

Example program on run time analysys in SAP ABAP


Program1: Create a program to get MARA details and display as below.
REPORT ZSAN_SE30.
**Runtime analysis using SE30
DATA : IT_MARA TYPE TABLE OF MARA.
DATA : WA_MARA TYPE MARA.
SELECT * FROM MARA INTO TABLE IT_MARA UP TO 500 ROWS.

LOOP AT IT_MARA INTO WA_MARA.


WRITE:/ WA_MARA-MATNR, WA_MARA-MTART, WA_MARA-MBRSH, WA_MARA-MATKL, WA_MARA-
MEINS.
ENDLOOP.

Go to SE30, provide program name, click on execute button, the out put will bi displayed, click on
back and click on evaluate to see run-time analysis.
The result will be like below(may vary based on server capacity).

In the above image you can see ABAP and database layers are in red color, means the time taken to
process these layers is more.
Program:2
Now change the above program like below and test again.
REPORT ZSAN_SE30.
TYPES: BEGIN OF TY_MARA,
MATNR TYPE MARA-MATNR,
MTART TYPE MARA-MTART,
MBRSH TYPE MARA-MBRSH,
MATKL TYPE MARA-MATKL,
MEINS TYPE MARA-MEINS,
END OF TY_MARA.
**Runtime analysis using SE30
DATA : IT_MARA TYPE TABLE OF TY_MARA.
DATA : WA_MARA TYPE ty_MARA.
SELECT MATNR MTART MBRSH MATKL MEINS FROM MARA INTO TABLE IT_MARA UP TO 500
ROWS.
LOOP AT IT_MARA INTO WA_MARA.
WRITE:/ WA_MARA-MATNR, WA_MARA-MTART, WA_MARA-MBRSH, WA_MARA-MATKL, WA_MARA-
MEINS.
ENDLOOP.

Now go to SE30 and follow the above process.

Now the evaluation will be like below.

You can see the database layer is in green color means the standards are good.
A good program with coding standards always have database layers is in green color.

Control Break statements in SAP ABAP


Control break statements are statements which are used to control the
sequence of execution of statements with in loop.....endloop.
These statements are executed only with in loop...endloop.

Control break statements are


AT FIRST - This statement is executed/triggered for the first iteration of loop (SY-TABIX = 1 ).
AT LAST - This statement is executed/triggered for the last iteration of loop.
AT NEW <FIELD> - This is executed whenever there is a new value on specified field.
AT END OF - This statement is executed whenever the new value ends on specific field.
ON CHANGE OF <FIELD> - It is same as AT NEW and is obsolete in ECC 6.0.

Example Report using control break statements in SAP ABAP


Requirement: Develop a report to display list of sales orders with totals
and page sub-totals for a particular date.
Selection-screen : input date.
SUM : SUM is a key word which is used to add numerical values of a field in control break
statements.
REPORT ZSAPN_CONTROL_BREAK.
TYPES : BEGIN OF TY_VBAP,
VBELN TYPE VBAP-VBELN,
MATNR TYPE VBAP-MATNR,
ZMENG TYPE VBAP-ZMENG,
NETPR TYPE VBAP-NETPR,
END OF TY_VBAP.
DATA : IT_VBAP TYPE TABLE OF TY_VBAP.
DATA : WA_VBAP TYPE TY_VBAP.
PARAMETERS : P_DATE TYPE VBAP-ERDAT .

START-OF-SELECTION.
SELECT VBELN MATNR ZMENG NETPR
FROM VBAP INTO TABLE IT_VBAP
WHERE ERDAT = P_DATE.

LOOP AT IT_VBAP INTO WA_VBAP.

AT FIRST .
WRITE :/2 'SALES DOC', 18 'MATRIAL', 28'QUANTITY', 40 'PRICE'.
ENDAT.

AT NEW VBELN.
WRITE :/2 WA_VBAP-VBELN, 18 WA_VBAP-MATNR, 28 WA_VBAP-ZMENG, 40 WA_VBAP-
NETPR.
ENDAT.

AT END OF VBELN.
SUM.
WRITE :/23 'SUB TOTAL IS :' COLOR 6, WA_VBAP-NETPR.
ENDAT.
AT LAST.
SUM.
WRITE:/23 'Total is:', WA_VBAP-NETPR .
ENDAT.

ENDLOOP.

What is Object Oriented ABAP and Concepts


The main feature of Object Oriented programming is representing real-time objects in the
form of class objects.
Object Oriented ABAP focus on representing real-time objects of classes.
SAP ABAP Object Oriented programming is available in two flavors .
One is Global Classes and another one is local class.

Global Class
Global Class is an ABAP object which can be accessible via SAP Class Builder, T-code for SAP
Class Builder is SE24.

Local Class
Local classes are classes which are available in ABAP programs, we can access them via ABAP
editor SE38.
ABAP Classes and It`s components
What is a Class ? A class is a user defined data type with attributes, methods, events, user-defined
types, interfaces etc for a particular entity or business application .
What are Objects ? Objects are nothing but instances of classes, each object has a unique identity
that is memory and it`s own attributes.

Declaring Classes and Objects


Syntax: DATA TYPE REF TO . "Declaring global class in ABAP program using type
ref to(type reference to )
CREATE OBJECT . "Create object for the declared instance

Components of a Class
Attributes: Attributes are variables, constants declared within a class .
Methods: Methods are coding blocks which provides some functionality .
These methods are similar to Function Modules in ABAP.
Methods can access all attributes of it`s own class.
Methods are defined in definition part and implemented in implementation part.
We can call methods using CALL METHOD statement.
Events: Event is a mechanism through which one method of a class can raise method of other class,
without hazard of instantiating that class.
Interfaces: Interfaces are similar to classes which contain methods Without any implementation.
Interfaces are mainly used to extend the scope or functionality of the class.
Instance and Static Components
Instance components : These components exist separately in each instance (object) of the class and
are referred using instance component selector using ->
Static components : These components exists globally for a class and are referred to using static
component selector => .
Visibility of Components of Class
Each class component has a visibility.
In ABAP Objects, the whole class definition is separated into three visibility sections:
PUBLIC .
PROTECTED .
PRIVATE.
Public section: Data declared in public section can be accessed by the class itself, by its subclasses
as well as by other users outside the class.
Protected section: Data declared in the protected section can be accessed by the class itself, and
also by its subclasses but not by external users outside the class.
Private Section: Data declared in the private section can be accessed by the class only, but not by
its subclasses and by external users outside the class.
Global Class and Local Class
Classes in ABAP Objects can be declared either globally or locally.
Global Class: Global classes and interfaces are defined in the Class Builder (Transaction SE24) in
the ABAP Workbench.
All of the ABAP programs in an R/3 System can access the global classes.
Local Class: Local classes are define in an ABAP program (Transaction SE38) and can only be
used in the program in which they are defined.

SAP Class Builder (SE24) introduction


Transaction code for SAP class builder is SE24. SAP class builder(SE24) is
used to create, display and change SAP classes.
Go to SE24 and provide some class example ex:
CL_ABAP_CHAR_UTILITIES, to see what are the components of class.

Properties: The properties tab contains class properties like created user name, last changed user
name, package etc.
Interfaces: Contains the list of the interfaces that are implemented in this class( will discuss in later
lessons).
Friends: contains friend classes ( we will discuss later lessons).
Attributes: The attributes tab contains the list of attributes declared in that class.
Methods : Contains the methods with importing and exporting parameters and with some business
functionality (source code).
Events: Contains events declared and implemented in that class.
Types: The types tab contains user defined type decelerations.
Aliases: Contains alias names for interface methods ( will discuss in later lessons).

Class methods and Creating methods in SAP classes


What is a method in a Class ?
Methods are coding blocks of a class, which can provide some business functionality (ex: read
material data etc), these methods are similar to Function Modules.
Methods can access all attributes of a class (defined under attributes tab), can access user defined
types ( declared under types tab).
The methods can be called using key word CALL METHOD in SAP ABAP programs.
Uses of methods in SAP classes?
These methods can be reusable in multiple ABAP programs, a class may contain more than one
method.
We will define methods under methods tab of a class, to define a method provide a method name,
level( instance or static), visibility (Public,Private,Protected) and method description.

The importing and exporting parameters of a method will defined under parameters area.
Add a method, click on parameter button and you will go to parameter interface where you can add
parameters to a particular method. (Parameters are method specific).
To write source code, click on methods tab and double click on method name (METHOD in the
above example), you will go to source code editor where you can add code.

This is how we create methods in SAP classes

Create a Class method to get material details in SAP OOABAP


Requirement: Create a class method to get material details for a material
number. Requirement analysis: For the above requirement we need to
create a class method to get details of a material for a material input, for
this method the importing parameter is material no (to pass material input
to method) and exporting parameter is of type MARA(work area one
material no contains only one record in MARA).Go to SE24, provide class
name as ZCL_SAPN_MATERIALS.

Provide description, save.


Save, save it in a local object, go to methods tab and declare a
method.GET_MATERIAL_DETAILS-INSTANCE-PUBLIC-Get Material details

Select parameters button and declare two parameters as belowIM_MATNR-IMPORTING- TYPE-


MARA-MATNR-Material NumberEX_MARA-EXPORTING-TYPE-MARA-General Master Data

Double click on CODE icon or Go back to methods and double click on method name and add
below code.
*Select material data from mara table into exporting parameter ex_mara (work

area) for a material no im_matnr


SELECT SINGLE * FROM MARA
INTO EX_MARA
WHERE MATNR = IM_MATNR.

Save, activate and execute the method( press F8).

Click on execute.

Provide a material no and execute.

You will get material details for that material, now class with method is created we have to use it in
our program.

Using Class method in SE38 Program


Go to SE38 and create a program ZSAPN_GET_MATERIAL_DETAILS and follow below steps to
add code.
Step1: Declare class and create object.
DATA : LO_MATERIAL TYPE REF TO ZCL_SAPN_MATERIALS.
CREATE OBJECT LO_MATERIAL.

Step2: Program declerations.


PARAMETERS : P_MATNR TYPE MARA-MATNR.
DATA : WA_MARA TYPE MARA. "work area to store material details

Step3: Call class method and print out put.


CALL METHOD LO_MATERIAL->GET_MATERIAL_DTAILS
EXPORTING
IM_MATNR = P_MATNR
IMPORTING
EX_MARA = WA_MARA.
WRITE :/ WA_MARA-MATNR, WA_MARA-MTART, WA_MARA-MEINS.

Final code will be


REPORT ZSAPN_GET_MATERIAL_DETAILS.
DATA : LO_MATERIAL TYPE REF TO ZCL_SAPN_MATERIALS.

PARAMETERS : P_MATNR TYPE MARA-MATNR.


DATA : WA_MARA TYPE MARA.
CREATE OBJECT LO_MATERIAL.

START-OF-SELECTION.
CALL METHOD LO_MATERIAL->GET_MATERIAL_DTAILS
EXPORTING
IM_MATNR = P_MATNR
IMPORTING
EX_MARA = WA_MARA.
WRITE :/ WA_MARA-MATNR, WA_MARA-MTART, WA_MARA-MEINS.

Using table types in methods in SAP classes


Requirement: Develop a report to display list of materials for a material type, use Object Oriented
ABAP to get materials. Requirement analysis: For this requirement we need to get materials from
MARA(material Master) table for the specified material type, a material type may have more than
one materials, so we have to get materials into an internal table.In SAP classes we don`t have
options to directly specify tables, for this one we need to create table type in SE11(Data
Dictionary).
Learners directly coming for this lesson, please refer previous lesson creating class method to
display materials as we are adding one more method for the same class.
Before creating method to get material details for a material type, we need to create a table type in
SE11 .

Go to SE11, select data type radio, provide name as ZSAPN_MARA, create.

A popup will open, select table type radio, enter.

Provide short text, provide line type as MARA.

Save and Activate.


Now table type is created for MARA, to create a method to get material details for a material type,
go to SE24, provide class name as ZCL_SAPN_MATERIALS( we previously created this class see
previous lesson) and click change.
Go to methods tab and add a method with name GET_MATERIALS_FOR_TYPE-INSTANCE-
PUBLIC-Get materials for material type.

Click on parameters button and add parameters as below.

Save, go back to methods and double click on GET_MATERIALS_FOR_TYPE and add the below
code.

*Get data from mara for a material type MTART


SELECT * FROM MARA
INTO TABLE ET_MARA
up to 50 rows
WHERE MTART = IM_MTART.
*For testing purpose im getting first 50 rows for a material type

Save, activate and test the method.

Using class method in SE38 program to get Material details for a material type.
Go to SE38 and create a program ZSAPN_GET_MATERIALS_FOR_TYPE and add below code.
REPORT ZSAPN_GET_MATERIALS_FOR_TYPE.
DATA : IT_MARA TYPE TABLE OF MARA. "internal table to store materials
DATA : WA_MARA TYPE MARA. "work area for materials to loop
DATA : LO_MATERIALS TYPE REF TO ZCL_SAPN_MATERIALS. "declare materials class

PARAMETERS : P_MTART TYPE MARA-MTART. "material type input

CREATE OBJECT LO_MATERIALS. "Create object for material class

START-OF-SELECTION.
CALL METHOD LO_MATERIALS->GET_MATERIALS_FOR_TYPE "call method to get materials
EXPORTING
IM_MTART = P_MTART
IMPORTING
ET_MARA = IT_MARA.

LOOP AT IT_MARA INTO WA_MARA.


WRITE :/ WA_MARA-MATNR, WA_MARA-MTART, WA_MARA-MEINS.
ENDLOOP.

Program input.
Program output.

User-defined types in SAP Classes


Requirement: Develop a report to display material number(MATNR), material type(mtart),
material group(MAKTL) and unit of measure(MEINS) for a given date( Created Date).
Requirement analysis: For this requirement we need to get limited fields (MATNR, MTART,
MATKL, MEINS, ERSDA from MARA (material Master) table for the specified date(ERSDA-
Created date), materials for the given date may be more than one so we need to use tables and we
need to get limited fields(columns) only (no need to get all fields data from MARA), for this one we
need to use concept of user defined table in SAP class method.
Learners directly coming for this lesson, please refer previous lesson creating class method to
display materials as we are adding one more method for the same class.
Go to SE24, provide class name as ZCL_SAPN_MATERIALS( we previously created this class see
previous lesson) and click change.

Go to methods tab and add a method with name GET_MATERIALS_FOR_DATE-INSTANCE-


PUBLIC-Get materials for a date.

We can declare user-defined types under types tab, go to types tab.

Click on Direct Type entry icon, save, it will take you to a editor
Don' change any thing, just replace types TY_MARA . with the below code.

TYPES: BEGIN OF TY_MARA,


MATNR TYPE MARA-MATNR,
ERSDA TYPE MARA-ERSDA,
MTART TYPE MARA-MTART,
MATKL TYPE MARA-MATKL,
MEINS TYPE MARA-MEINS,
END OF TY_MARA.

TYPES : TT_MARA TYPE TABLE OF TY_MARA.

Save, go back to methods, put mouse cursor on GET_MATERIAL_FOR_DATE method, select


parameters button and add below parameters.

Go back to methods and double click on method GET_MATERIAL_FOR_DATE, write the below
code.

METHOD GET_MATERIAL_FOR_DATE.
*Get material no, created date, material type, material group, units of measue
*from MARA table
SELECT MATNR ERSDA MTART MATKL MEINS
FROM MARA INTO TABLE ET_MARA.
ENDMETHOD.

Save and activate.


Go to SE38 and create a program and add below code.
REPORT ZSAPN_GET_MATERIALS_FOR_DATE.

TYPES: BEGIN OF TY_MARA,


MATNR TYPE MARA-MATNR,
ERSDA TYPE MARA-ERSDA,
MTART TYPE MARA-MTART,
MATKL TYPE MARA-MATKL,
MEINS TYPE MARA-MEINS,
END OF TY_MARA .
DATA : IT_MARA TYPE TABLE OF TY_MARA.
DATA : WA_MARA TYPE TY_MARA.

DATA : LO_MATERIAL TYPE REF TO ZCL_SAPN_MATERIALS.


PARAMETERS : P_DATE TYPE MARA-ERSDA.
CREATE OBJECT LO_MATERIAL.

START-OF-SELECTION.

CALL METHOD LO_MATERIAL->GET_MATERIAL_FOR_DATE


EXPORTING
IM_DATE = P_DATE
IMPORTING
ET_MARA = IT_MARA.

LOOP AT IT_MARA INTO WA_MARA.


WRITE:/ WA_MARA-MATNR, WA_MARA-ERSDA, WA_MARA-MTART, WA_MARA-MATKL, WA_MARA-
MEINS.
ENDLOOP.

Activate the program and test.

What are Events in SAP classes ?


What are events in SAP Classes?

Event is a mechanism by which method of one class can raise method of another class, without the
hazard of instantiating that class.
Follow the below steps to add a event
Define an event .
Define a method.
Link event and method and convert the method into event-handler method.
Create a triggering method which will raise the event.
Use set handler and register event handler method to a particular instance in the program.
Use below syntax to register a event handler method for a instance
SET HANDLER for . "here instance is the object created by using create object

Example of using Events in SAP OOABAP


classes
This is the basic example of using events in SAP classes.

Learners directly coming for this lesson, please refer previous lesson Events in SAP classes and
Working with class methods in SAP as we are adding one more method for the same class.
Go to SE24, provide class name as ZCL_SAPN_MATERIALS( we previously created this class see
previous lesson) and click change.

Steps for creating and using event in SAP classes


Step 1: Define an event .
Step 2: Define a method.
Step 3: Link event and method and convert the method into event-handler method.
Step 4: Create a triggering method which will raise the event.
Step 5: Use set handler and register event handler method to a particular instance in the
program.

Define an event
Go to Events tab and add a event as below.
NO_MATERIAL-INSTANCE-PUBLIC-No material entered

Define a method
Go to Methods tab and create a method as below
NO_MATERIAL_HANDLER-INSTANCE-PUBLIC-Event Handler Method.

Save, double click on the method and add some code.


WRITE:/ 'NO material entered'.

Save and activate immediately.

Link event and method and convert the method into event-handler method.
Now we have to link the method to event to make the method as event handler.
Go to methods and put cursor on method NO_MATERIAL_HANDLER, click on detail view
icon(see below image).

A pop up will open, provide description, select Event Handler for check box, provide our class
name as ZCL_SAPN_MATERISLS and event name as NO_MATERIAL (Press F4), enter.
You will see an icon(event handler icon), which means the method is event handler method.
Create a triggering method which will raise the event
Event handler method is created, now we have to trigger that event, the event can be triggered by
using below syntax.
RAISE EVENT <EVENT NAME>.

We will trigger the event for the method GET_MATERIAL_DTAILS (we previously created Get
material details ), double click on the method GET_MATERIAL_DTAILS and add the below code.
METHOD GET_MATERIAL_DTAILS.
*Select material data from mara table into exporting parameter ex_mara (work
area) for a material no im_matnr
IF IM_MATNR IS INITIAL .
RAISE EVENT NO_MATERIAL.
ELSE.
SELECT SINGLE * FROM MARA
INTO EX_MARA
WHERE MATNR = IM_MATNR.

ENDIF.
ENDMETHOD.

Use set handler and register event handler method to a particular instance in the program
Now we have to register this event in our SE38 program, to register a event handler method we use
below syntax.
SET HANDLER <INSTANCE>-><EVENT HANDLER METHOD> FOR <INSTANCE>. "here INSTANCE is
object and EVENT HANDLER METHOD handler method created in se24

Go to SE38, create a program ZSAPN_CLASS_EVENT and write the below code


REPORT ZSAPN_CLASS_EVENT.
DATA : LO_MATERIAL TYPE REF TO ZCL_SAPN_MATERIALS. "class decleration
DATA WA_MARA TYPE MARA. "work area to store material data

PARAMETERS P_MATNR TYPE MARA-MATNR. "material input

CREATE OBJECT LO_MATERIAL. "create object for material calsss


SET HANDLER LO_MATERIAL->NO_MATERIAL_HANDLER FOR LO_MATERIAL. "register event
handler method

START-OF-SELECTION.
CALL METHOD LO_MATERIAL->GET_MATERIAL_DTAILS "call method
EXPORTING
IM_MATNR = P_MATNR
IMPORTING
EX_MARA = WA_MARA.

WRITE : WA_MARA-MATNR, WA_MARA-MTART, WA_MARA-MEINS.

Now execute the program with out giving any input, the result will be
Assignment for you: Create and implement a event and event handler method for material not
found in the above class, if user provided the input but the data is not there in MARA table, raise an
event.

What are CONSTRUCTORS in SAP


OOABAP ?
What is a constructor in a class ?
CONSTRUCTOR is a special type of method, it is executed automatically whenever a object is
created or instantiated.
These methods are mainly used to set default values in classes.
CONSTRUCTORS are two types in SAP classes.
1. CONSTRUCTOR ( Instance Constructor).
2. CLASS CONSTRUCTOR (Static Constructor).
CONSTRUCTOR.
These methods can only have importing parameters, there will not be any exporting parameters.
The name of the CONSTRUCTOR method must be CONSTRUCTOR only.
CLASS CONSTRUCTOR (Also called as STATIC CONSTRUCTOR).
It is a type of constructor, this method will be executed automatically whenever a first call to the
class is made, the first call may be through instance or through class.
These CLASS CONSTRUCTORS are mainly used to set the default values globally i:e irrespective
of instances, these methods will not have any importing and exporting parameters.These methods
will be executed only once.
Name of the CLASS CONSTRUCTOR must be CLASS_CONSTRUCTOR.

Using Constructor method in SAP OOABAP


This CONSTRUCTOR method is very useful in setting default value in a class, the below is the
example of using CONSTRUCTOR method in a SAP Class.Requirement: Display for material
description for a material, and depends upon language selected.
All material descriptions are stored in MAKT table, for a material there may be different
descriptions example for English one description, for German one description etc, we need to get
description based on the language, for this one we create a class method for re-usablity(the same
method can be used in different programs to get descriptions based on the selected languages).
Go to SE24,provide class ZCL_SAPN_MATERIAL and click on change.
Go to Methods and provide method name as CONSTRUCTOR, enter

Once you press enter, you will see a symbol (Constructor symbol), it indicates that the method is a
constructor method.

Now put cursor on method name and click on parameters button.

Click on parameter button and add below parameter( what ever the parameters you add, they will
become importing parameters only).

Select attributes tab and add below attribute.

Go to methods and double click on CONSTRUCTOR method and add below code.

Save and activate, go to methods tab and add one more method
GET_MATERIAL_DESCRIPTIONS to get material descriptions.

Select parameters button and add below parameters.

Save, go back to methods and double click on method name GET_MATERIAL_DESCRIPTIONS,


add below code.

METHOD GET_MATERIAL_DESCRIPTIONS.

SELECT * FROM MAKT INTO EX_MAKT


WHERE MATNR = IM_MATNR
AND SPRAS = LANGUAGE. "LANGUAGE IS THE ATTRIBUTE DEFINED IN METHOD
ENDSELECT.

ENDMETHOD.

Using CONSTRUCTOR method with ABAP program


Go to SE38 and create a program ZSAPN_MATERIAL_DESCRIPTION and follow below steps.
Step1: Data declarations
DATA : LO_MATERIAL TYPE REF TO ZCL_SAPN_MATERIALS. "Declare class
DATA : WA_MAKT TYPE MAKT. "declare work area for makt
PARAMETERS P_MATNR TYPE MARA-MATNR. "material input

Step2: Create object for material class, the object can be created using ABAP patterns also.
To create object, click on pattern button.

A popup will open, select ABAP object patterns and enter.

One more pop up will come, select create object, provide instance name (We declared at the
declerations step LO_MATERIAL ), provide class name (our class name is
ZCL_SALN_MATERIALS) and enter.

Now the CONSTRUCTOR method is triggered, it will ask for a parameter IM_SPRAS (Which is
importing parameter of constructor method).It will set language key as English.
Finally call the method and get material descriptions.
The final code will be .
REPORT ZSAPN_GET_MATERIAL_DESCRIPTION.
DATA : LO_MATERIAL TYPE REF TO ZCL_SAPN_MATERIALS.
DATA : WA_MAKT TYPE MAKT.
PARAMETERS P_MATNR TYPE MARA-MATNR.
CREATE OBJECT LO_MATERIAL
EXPORTING
IM_SPRAS = 'E'.

START-OF-SELECTION.
CALL METHOD LO_MATERIAL->GET_MATERIAL_DESCRIPTIONS
EXPORTING
IM_MATNR = P_MATNR
IMPORTING
EX_MAKT = WA_MAKT.

WRITE : WA_MAKT-MATNR, WA_MAKT-MAKTX.

Testing the above program


Go to MAKT table and get a material number with multiple language descriptions.
Execute the program, provide the material no, you will get out put where language = 'E' only
(Constructor method handles this).

Using CLASS CONSTRUCTOR in SAP


OOABAP
This is a type of CONSTRUCTOR, this method is executed whenever a first call to the class is
made, the call may be through instance or through class name.
These are also called as STATIC CONSTRUCTORS, the name must be CLASS_CONSTRUCTOR.
If you are coming directly for this lesson we highly recommend go through previous lesson Using
CONSTRUCTOR method in SAP classes for better understanding.
Example: We will add a class constructor to make default material type as 'FERT'(Finished Product)
Follow the below steps to create a class constructor.
Go to SE24, provide the class name ZCL_SAPN_MATERIALS, click on change.
Go to attributes tab and add an attribute to make default material type.

Go to methods and add a method name CLASS_CONSTRUCTOR as below.

We can not add any exporting and importing parameters to a CLASS_CONSTRUCTOR method.
Double click on the method CLASS_CONSTRUCTOR and add below code.

METHOD CLASS_CONSTRUCTOR.
*Set default material type as FERT
MAT_TYPE = 'FERT'.
ENDMETHOD.

Here we take a small example to test this one.The below on is a simple example and explains you a
lot.
REPORT ZSAPN_CLASS_CONSTRUCTOR.
DATA : LO_MATERIAL TYPE REF TO ZCL_SAPN_MATERIALS. "Declare class

CREATE OBJECT LO_MATERIAL "create object CONSTRUCTOR method will trigger


EXPORTING
IM_SPRAS = 'E'.
**When ever first call to a class is made class constructor will trigger
WRITE:/ 'Executed through class constructor', ZCL_SAPN_MATERIALS=>MAT_TYPE.
"Executed through Class Constructor

WRITE:/ 'Executed through Constructor', LO_MATERIAL->LANGUAGE . "Executed


through constructor method

If we have both CONSTRUCTOR and CLASS_CONSTRUCTOR in our class, upon a class call
which will trigger first.....First CLASS_CONSTRUCTOR will be triggered.

Interface Concept in Object Oriented ABAP


Before going to interfaces, we must know about Polymorphism .

What is Polymorphism in Object Oriented Programming model ?


It is a concept by which the same method names will behave differently in different classes i.e each
method will have its own implementation in different different classes but with the same name.
Interface is one of the concept in Object Oriented ABAP to achieve Polymorphism.

What is an interface in Object Oriented ABAP ?


Interfaces are independent structure which are used in a class to extend the functionality of a
class.
Interfaces contains methods without any implementation. Where as a class contains methods
with implementation.
We need to define an interface with the required method names in SE24 TCODE.
The interfaces can be used by no of classes to extend the functionality of the class.
To implement an interface in a class just give the name of interface under the 'Interfaces' tab.
All the interface methods will be automatically copied to the classes in a particular class
without effecting the other classes.
The main use of interfaces is re-usability and maintain standard project framework.
In the above diagram, application is business application, Class A, Class B, Class C, Class D and
Class E are independent classes, all these classes are using one Interface called Interface( in
diagram).
In all the classes,the methods names are same but the implementation will be different from one
class to another class thereby achieving the concept called POLYMORPHISM. Polymorphism is
implemented in the form of interface.

Creating Class Interface in SAP OOABAP


As per SAP Standard documentation all the standard interfaces in SAP starts with IF_ and all the
custom interfaces starts with ZIF_ , in the below example we will be learning how to create a use
interfaces in SAP.
All the custom classes is SAP starts with ZCL_ and all the custom interfaces will start with ZIF_,
based on the provided name (at the time of creation), SAP will automatically determine and creates
(interface or class).
Go to SE24, provide name as ZIF_SAPN_MATERIAL_EXAMPL, create.

Provide description, save, save it in a local object.

Go to methods tab, add method name as GET_MATERIAL_DETAILS-INSTANCE, click on


parameters button and add below parameters.
Save, Go back to methods and add one more method GET_MATERIAL_DESCRIPTIONS.

Click on parameters and provide parameters as below.

Save and activate.


We have created an interface with two methods (which dosen`t have implementation).
No Go to SE24 and create a class ZCL_SAPN_CLASS_EX

Provide description, save it in a local object.

Go to interfaces tab and add interface name as ZIF_SAPN_MATERIAL_EXAMPLE, enter

Now go to methods tab, you can see all the methods in the interface are automatically copied.

Save, double click on each method and create your own implementation.
Double click on ZIF_SAPN_MATERIAL_EXAMPLE~GET_MATERIAL_DETAILS, and add
below code.

SELECT SINGLE * FROM MARA


INTO EX_MARA
WHERE MATNR = IM_MATNR.

Double click on ZIF_SAPN_MATERIAL_EXAMPLE~GET_MATERIAL_DESCRIPTIONS, add


below code

SELECT * FROM MAKT INTO EX_MAKT


WHERE MATNR = IM_MATNR
AND SPRAS = 'E'. "English description only
ENDSELECT.

Save and activate the class.


Using class in program
Create a SE38 program and follow the steps below. The interface method will be called by using
INTERFACENAME~METHODNAME.
REPORT ZSAPN_CLASS_INTERFACE.
DATA : LO_CLASS TYPE REF TO ZCL_SAPN_CLASS_EX. "declare class

DATA : WA_MARA TYPE MARA. "mara decleration


DATA : WA_MAKT TYPE MAKT. "makt decleration

PARAMETERS P_MATNR TYPE MARA-MATNR.

CREATE OBJECT LO_CLASS. "create object for the class

START-OF-SELECTION.
CALL METHOD LO_CLASS->ZIF_SAPN_MATERIAL_EXAMPLE~GET_MATERIAL_DETAILS
EXPORTING
IM_MATNR = P_MATNR
IMPORTING
EX_MARA = WA_MARA.
CALL METHOD LO_CLASS->ZIF_SAPN_MATERIAL_EXAMPLE~GET_MATERIAL_DESCRIPTIONS
EXPORTING
IM_MATNR = P_MATNR
IMPORTING
EX_MAKT = WA_MAKT.
WRITE :/ 'Material Details - ' COLOR 5, WA_MARA-MATNR, WA_MARA-MTART, WA_MARA-
MBRSH, WA_MARA-MATKL.
WRITE :/ 'Material Descriptions - 'COLOR 6, WA_MAKT-MATNR, WA_MAKT-MAKTX,
WA_MAKT-SPRAS.

Similarly create another class and repeat the same process, we can add interfaces into multiple
classes.

Using aliases for interface methods in SAP


OOABAP
Aliases is a concept of providing alternate method names for interface methods in implemented
class(where interface is implemented).
Whenever we implement an interface in a class, the method will be copied with
INTERFACENAME~METHODNAME, see below example.

By using aliases concept we can provide alternate name for interface methods and we
can call methods with that name, to add alternate names click on aliases tab and provide altternate
names.

Calling alias method is same as normal method CALL METHOD OBJECT->ALIASMETHDO


NAME, see below example.
Please refer previous lesson Using interface class in SAP Classes for better understanding.
REPORT ZSAPN_CLASS_INTERFACE.
DATA : LO_CLASS TYPE REF TO ZCL_SAPN_CLASS_EX. "declare class

DATA : WA_MARA TYPE MARA. "mara decleration


DATA : WA_MAKT TYPE MAKT. "makt decleration
PARAMETERS P_MATNR TYPE MARA-MATNR.

CREATE OBJECT LO_CLASS. "create object for the class

START-OF-SELECTION.
CALL METHOD LO_CLASS->GET_MATERIAL_DETAILS "alias name
EXPORTING
IM_MATNR = P_MATNR
IMPORTING
EX_MARA = WA_MARA.
CALL METHOD LO_CLASS->GET_MATERIAL_DESCRIPTIONS "alias name
EXPORTING
IM_MATNR = P_MATNR
IMPORTING
EX_MAKT = WA_MAKT.
WRITE :/ 'Material Details - ' COLOR 5, WA_MARA-MATNR, WA_MARA-MTART, WA_MARA-
MBRSH, WA_MARA-MATKL.
WRITE :/ 'Material Descriptions - 'COLOR 6, WA_MAKT-MATNR, WA_MAKT-MAKTX,
WA_MAKT-SPRAS.

Inheritance in SAP OOABAP


What is Inheritance in SAP OOABAP ?

Inheritance is the concept of passing the behavior of one class to another class.
You can use an existing class to derive a new class.
Derived class inherits the data and methods of a super class.
However they can overwrite the existing methods also add new code. Advantage of this property is
re-usability.
This means we can add additional features to an existing class without modifying it.
SUPER is the keyword used to represent the super class in oops, you can access the methods and
attributes the super class using this word super.
REDIFINATION is the keyword which is used to overwrite the parent class methods with new
definition.
NOTE: In Object Oriented ABAP we have only single inheritance. There is no multiple inheritance.

Inheritance in general
Super Class Is a main class by using this we can derive a new class which will be called as Child
class.
Final Class is a class which can not be used for inheritance, it is a class property (check box under
properties of class).
If the final check box is selected, we can not use this class for inheritance, to use inheritance remove
the final check box.

Using inheritance in SAP OOABAP


IMPORTING
EX_MARA = EX_MARA.
***Additional code to get material descriptions
SELECT * FROM MAKT
INTO LS_MAKT
WHERE MATNR = EX_MARA-MATNR.
ENDSELECT.

ENDMETHOD.

Using Class inprogram


Create a program in SE38 and follow below steps(code).
REPORT ZSAPN_CLASS_INHERITANCE.
DATA : LO_CLASS TYPE REF TO ZCL_SAPN_CHILDCLASS. "declare class
DATA : WA_MARA TYPE MARA. "declare MARA
DATA WA_MAKT TYPE MAKT. "declare MAKT

PARAMETERS P_MATNR TYPE MARA-MATNR. "material input


CREATE OBJECT LO_CLASS. "create instance for class

START-OF-SELECTION.
CALL METHOD LO_CLASS->GET_MATERIAL_DETAILS "get material details
EXPORTING
IM_MATNR = P_MATNR
IMPORTING
EX_MARA = WA_MARA.

WA_MAKT = LO_CLASS->LS_MAKT. "access material description from class attribute


WRITE:/ WA_MARA-MATNR, WA_MARA-MTART, WA_MARA-MEINS, WA_MARA-MATKL. "print
material details
WRITE:/ WA_MAKT-MATNR, WA_MAKT-MAKTX. "print material descriptions
What is a local Class in OOABAP ? Properties
of local class in OOABAP
Classes are two types in SAP Classes, one is Global class which can be created in SE24 and second
one is local class.Local class is a class which have definition and implementation in a program .
Syntax for creating local class is :
*CLASS DEFINITION
CLASS DEFINITION. "THIS IS CLASS DEFINITION

ENDCLASS.

*CLASS IMPLEMENTATION
CLASS IMPLEMENTATION.
*METHODS, EVENTS IMPLEMENTATION
ENDCLASS.

Defining attributes, methods and events in local classes


All the attributes, methods, events must be defined in between CLASS DEFINITION and
ENDCLASS.
Syntax for declaring attributes in local classes
CLASS CL_EXAMPLE DEFINITION. "CLASS DEFINITION
PUBLIC SECTION. "VISIBILITY
DATA : TYPE . "DECLARING INSTANCE ATTRIBUTE
CLASS-DATA: TYPE . "DECLARING STATIC ATTRIBUTE
ENDCLASS.

Syntax for declaring methods in local classes


CLASS CL_EXAMPLE DEFINITION. "CLASS DEFINITION
PUBLIC SECTION. "VISIBILITY
*DECLARING INSTANCE METHOD
METHODS :
IMPORTING TYPE
EXPORTING TYPE .

*DECLARING STATIC METHOD


CLASS-METHODS :
IMPORTING TYPE
EXPORTING TYPE .
ENDCLASS.

Syntax for declaring EVENTS in local classes


CLASS CL_EXAMPLE DEFINITION. "CLASS DEFINITION
PUBLIC SECTION. "VISIBILITY
*DECLARING INSTANCE EVENT
EVENT : .

*DECLARING STATIC EVENT


CLASS-EVENT : .
ENDCLASS.

Syntax for declaring CONSTRUCTOR in local classes


CLASS CL_EXAMPLE DEFINITION. "CLASS DEFINITION
PUBLIC SECTION. "VISIBILITY
*DECLARING INSTANCE CONSTRUCTOR
METHODS : CONSTRUCTOR
IMPORTING TYPE .
*DECLARING STATIC CONSTRUCTOR(CLASS CONSTRUCTOR)
CLASS-METHODS: CLASS_CONSTRUCTOR.

ENDCLASS.

Local class with attributes in OOABAP


Local class is a class definition and implementation is available in a program.
Go to SE38, create a program ZSAPN_CLASS_ATTRIBUTES and follow steps to add code

Define a class
CLASS CL_ATTRIBUTES DEFINITION. "DOUBLE CLICK ON CLASS NAME TO CREATE
IMPLEMENTATION
PUBLIC SECTION.
DATA : PA_NAME TYPE CHAR25. "PUBLIC INSTANCE ATTRIBUTE
CLASS-DATA : PA_NAME_ST TYPE CHA25. "PUBLIC STATIC ATTRIBUTE
ENDCLASS.

Implement class
CLASS CL_ATTRIBUTES IMPLEMENTATION.
*LEAVE BLANK FOR THIS EXAMPLE
ENDCLASS.

Using class
DATA : LO_ATTRIBUTES TYPE REF TO CL_ATTRIBUTES. "DECLARE CLASS
CREATE OBJECT LO_ATTRIBUTES. "CREATE OBJECT

LO_ATTRIBUTES->PA_NAME = 'SACHNTHENDULAKR'. "ASSIGN A VALUE TO INSTANCE


ATTRIBUTE
CL_ATTRIBUTES=>PA_NANE_ST = 'PONTING'. "ASSIGN VALUE TO STATIC ATTRIBUTES

*PRINT ATTRIBUTES VIA CLASS


WRITE:/ 'INSTANCE ATTRIBUTE:', LO_ATTRIBUTES->PA_NAME.
WRITE:/ 'STATIC ATTRIBUTE :', CL_ATTRIBUTE=>PA_NAME_ST.

DEFINITION DEFERED is a keyword which indicates the class definition is delayed or


postponed or definition at some place in program.

Final code will be


CLASS CL_ATTRIBUTES DEFINITION DEFERED.
DATA : LO_ATTRIBUTES TYPE REF TO CL_ATTRIBUTES. "DECLARE CLASS
CREATE OBJECT LO_ATTRIBUTES. "CREATE OBJECT

CLASS CL_ATTRIBUTES DEFINITION. "DOUBLE CLICK ON CLASS NAME TO CREATE


IMPLEMENTATION
PUBLIC SECTION.
DATA : PA_NAME TYPE CHAR25. "PUBLIC INSTANCE ATTRIBUTE
CLASS-DATA : PA_NAME_ST TYPE CHA25. "PUBLIC STATIC ATTRIBUTE
ENDCLASS.

LO_ATTRIBUTES->PA_NAME = 'SACHNTHENDULAKR'. "ASSIGN A VALUE TO INSTANCE


ATTRIBUTE
CL_ATTRIBUTES=>PA_NANE_ST = 'PONTING'. "ASSIGN VALUE TO STATIC ATTRIBUTES

*PRINT ATTRIBUTES VIA CLASS


WRITE:/ 'INSTANCE ATTRIBUTE:', LO_ATTRIBUTES->PA_NAME.
WRITE:/ 'STATIC ATTRIBUTE :', CL_ATTRIBUTE=>PA_NAME_ST.
CLASS CL_ATTRIBUTES IMPLEMENTATION.
*LEAVE BLANK FOR THIS EXAMPLE
ENDCLASS.

Local class with methods in SAP OOABAP


Local class is a class definition and implementation is available in a program.
Go to SE38, create a program ZSAPN_CLASS_METHODS1 and follow steps to add code

Define a class
CLASS CL_METHODS_EXAMPLE DEFINITION. "DOUBLE CLICK ON CLASS NAME TO CREATE

IMPLEMENTATION
PUBLIC SECTION.
METHODS : GET_MATERAIAL_DETAILS
IMPORTING IM_MATNR TYPE MATNR
EXPORTING EX_MARA TYPE MARA.
CLASS-METHODS : GET_MATERIAL_DESCRIPTION
IMPORTING IM_MATNR TYPE MATNR
EXPORTING EX_MARA TYPE MARA.

ENDCLASS.

Implement class
CLASS CL_METHODS_EXAMPLE IMPLEMENTATION.
METHOD GET_MATERIAL_DETAILS.
SELECT SINGLE * FROM MARA
INTO EX_MARA
WHERE MATNR = IM_MATNR.
ENDMETHOD.

METHOD GET_MATERIAL_DESCRIPTION.
SELECT * FROM MAKT
INTO EX_MAKT
WHERE MATNR = IM_MATNR.
ENDSELECT.
ENDMETHOD.
ENDCLASS.

Using class
DATA : WA_MARA TYPE MARA.
DATA : WA_MAKT TYPE MAKT.
PARAMETERS : P_MATNR TYPE MARA-MATNR.
DATA : LO_MATERIAL TYPE REF TO CL_METHODS_EXAMPLE. "DECLARE CLASS
CREATE OBJECT LO_MATERIAL. "CREATE OBJECT

CALL METHOD LO_MATERIAL->GET_MATERIAL_DETAILS


EXPORTING
IM_MATNR = P_MATNR
IMPORTING
EX_MARA = WA_MARA .

CALL METHOD LO_MATERIAL->GET_MATERIAL_DESCRIPTION


EXPORTING
IM_MATNR = P_MATNR
IMPORTING
EX_MAKT = WA_MAKT .

*PRINT OUTPUT
WRITE:/ WA_MARA-MATNR, WA_MARA-MTART, WA_MARA-MEINS, WA_MARA-MAKTL.
WRITE:/ WA_MAKT-MATNR, WA_MAKT-MAKTX.

DEFINITION DEFERED is a kwyword which indicates the class definition is delayed or


postponed or definition at some place in program.

Final code will be


REPORT ZSAPN_LOCAL_CLASS_METHODS.
CLASS CL_METHODS_EXAMPLE DEFINITION DEFERRED.
DATA : WA_MARA TYPE MARA.
DATA : WA_MAKT TYPE MAKT.
PARAMETERS : P_MATNR TYPE MARA-MATNR.
DATA : LO_MATERIAL TYPE REF TO CL_METHODS_EXAMPLE. "DECLARE CLASS

CLASS CL_METHODS_EXAMPLE DEFINITION. "DOUBLE CLICK ON CLASS NAME TO CREATE


IMPLEMENTATION
PUBLIC SECTION.
METHODS : GET_MATERIAL_DETAILS
IMPORTING IM_MATNR TYPE MATNR
EXPORTING EX_MARA TYPE MARA.
CLASS-METHODS : GET_MATERIAL_DESCRIPTION
IMPORTING IM_MATNR TYPE MATNR
EXPORTING EX_MAKT TYPE MAKT.

ENDCLASS.
CREATE OBJECT LO_MATERIAL. "CREATE OBJECT

CALL METHOD LO_MATERIAL->GET_MATERIAL_DETAILS


EXPORTING
IM_MATNR = P_MATNR
IMPORTING
EX_MARA = WA_MARA.

CALL METHOD CL_METHODS_EXAMPLE=>GET_MATERIAL_DESCRIPTION


EXPORTING
IM_MATNR = P_MATNR
IMPORTING
EX_MAKT = WA_MAKT.

*PRINT OUTPUT
WRITE:/ WA_MARA-MATNR, WA_MARA-MTART, WA_MARA-MEINS, WA_MARA-MATKL.
WRITE:/ WA_MAKT-MATNR, WA_MAKT-MAKTX.

CLASS CL_METHODS_EXAMPLE IMPLEMENTATION.


METHOD GET_MATERIAL_DETAILS.
SELECT SINGLE * FROM MARA
INTO EX_MARA
WHERE MATNR = IM_MATNR.
ENDMETHOD.

METHOD GET_MATERIAL_DESCRIPTION.
SELECT * FROM MAKT
INTO EX_MAKT
WHERE MATNR = IM_MATNR.
ENDSELECT.
ENDMETHOD.
ENDCLASS.

Local class with tables in SAP OOABAP


The bwlow example explains you of using table in SAP local classes using SAP ABAP
programming language.

Declare a table type


Create a table type in SE11(in this example we use ZSAPN_MARA which we have created
already).

Define class and method


CLASS CL_USERDEFINED_TYPES DEFINITION.
PUBLIC SECTION.
METHODS : GET_MATERIALS_FOR_TYPE
IMPORTING IM_MTART TYPE MARA-MTART
EXPORTING ET_MARA TYPE ZSAPN_MARA. "table type in SE11
ENDCLASS.

Implement the defined class


CLASS CL_USERDEFINED_TYPES IMPLEMENTATION.
METHOD GET_MATERIALS_FOR_TYPE.
SELECT * FROM MARA
INTO TABLE ET_MARA
WHERE MTART = IM_MTART .
ENDMETHOD.
ENDCLASS.

Use the class and method


Create an object for the class and call the method
DATA : IT_MARA TYPE TABLE OF MARA.
DATA : WA_MARA TYPE MARA.
CREATE OBJECT LO_MATERIAL. "CREATE OBJECT
CALL METHOD LO_MATERIAL->GET_MATERIALS_FOR_TYPE
EXPORTING
IM_MTART = P_MTART
IMPORTING
ET_MARA = IT_MARA.
*PRINT OUTPUT
LOOP AT IT_MARA INTO WA_MARA.
WRITE:/ WA_MARA-MATNR, WA_MARA-MTART, WA_MARA-MEINS, WA_MARA-MATKL.
ENDLOOP.

The final code will be


REPORT ZSAPN_LOCAL_CLASS_METHODS.
CLASS CL_USERDEFINED_TYPES DEFINITION DEFERRED.

PARAMETERS : P_MTART TYPE MARA-MTART.


DATA : LO_MATERIAL TYPE REF TO CL_USERDEFINED_TYPES. "DECLARE CLASS
DATA : IT_MARA TYPE TABLE OF MARA.
DATA : WA_MARA TYPE MARA.
CLASS CL_USERDEFINED_TYPES DEFINITION.
PUBLIC SECTION.
METHODS : GET_MATERIALS_FOR_TYPE
IMPORTING IM_MTART TYPE MARA-MTART
EXPORTING ET_MARA TYPE ZSAPN_MARA.
ENDCLASS.
CREATE OBJECT LO_MATERIAL. "CREATE OBJECT
CALL METHOD LO_MATERIAL->GET_MATERIALS_FOR_TYPE
EXPORTING
IM_MTART = P_MTART
IMPORTING
ET_MARA = IT_MARA.

*PRINT OUTPUT
LOOP AT IT_MARA INTO WA_MARA.
WRITE:/ WA_MARA-MATNR, WA_MARA-MTART, WA_MARA-MEINS, WA_MARA-MATKL.
ENDLOOP.
CLASS CL_USERDEFINED_TYPES IMPLEMENTATION.
METHOD GET_MATERIALS_FOR_TYPE.
SELECT * FROM MARA
INTO TABLE ET_MARA
WHERE MTART = IM_MTART .
ENDMETHOD.
ENDCLASS.

Local class with user-defined types in SAP


OOABAP
It is very important for us to use user-defined types when using local classes in SAP ABAP
programs, the below example explains using user-defined types in SAP ABAP programs.
The below example explains you of using user defined types in SAP Local classes in SAP ABAP
programming.

Declare a table type


Declare a table type with required fields
TYPES : BEGIN OF TY_MARA,
MATNR TYPE MARA-MATNR,
MTART TYPE MARA-MTART,
MEINS TYPE MARA-MEINS,
MATKL TYPE MARA-MATKL,
END OF TY_MARA.
TYPES : TT_MARA TYPE TABLE OF TY_MARA.

Define class and method


CLASS CL_USERDEFINED_TYPES DEFINITION.
PUBLIC SECTION.
METHODS : GET_MATERIALS_FOR_TYPE
IMPORTING IM_MTART TYPE MARA-MTART
EXPORTING ET_MARA TYPE TT_MARA.
ENDCLASS.

Implement the defined class


CLASS CL_USERDEFINED_TYPES IMPLEMENTATION.
METHOD GET_MATERIALS_FOR_TYPE.
SELECT MATNR MTART MEINS MATKL FROM MARA
INTO TABLE ET_MARA
WHERE MTART = IM_MTART .
ENDMETHOD.
ENDCLASS.

Use the class and method


Create an object for the class and call the method
DATA : IT_MARA TYPE TABLE OF TY_MARA.
DATA : WA_MARA TYPE TY_MARA.
CREATE OBJECT LO_MATERIAL. "CREATE OBJECT
CALL METHOD LO_MATERIAL->GET_MATERIALS_FOR_TYPE
EXPORTING
IM_MTART = P_MTART
IMPORTING
ET_MARA = IT_MARA.

*PRINT OUTPUT
LOOP AT IT_MARA INTO WA_MARA.
WRITE:/ WA_MARA-MATNR, WA_MARA-MTART, WA_MARA-MEINS, WA_MARA-MATKL.
ENDLOOP.

The final code will be


REPORT ZSAPN_LOCAL_CLASS_METHODS.
CLASS CL_USERDEFINED_TYPES DEFINITION DEFERRED.

PARAMETERS : P_MTART TYPE MARA-MTART.


DATA : LO_MATERIAL TYPE REF TO CL_USERDEFINED_TYPES. "DECLARE CLASS
TYPES : BEGIN OF TY_MARA,
MATNR TYPE MARA-MATNR,
MTART TYPE MARA-MTART,
MEINS TYPE MARA-MEINS,
MATKL TYPE MARA-MATKL,
END OF TY_MARA.
TYPES : TT_MARA TYPE TABLE OF TY_MARA.
DATA : IT_MARA TYPE TABLE OF TY_MARA.
DATA : WA_MARA TYPE TY_MARA.
CLASS CL_USERDEFINED_TYPES DEFINITION.
PUBLIC SECTION.
METHODS : GET_MATERIALS_FOR_TYPE
IMPORTING IM_MTART TYPE MARA-MTART
EXPORTING ET_MARA TYPE TT_MARA.
ENDCLASS.
CREATE OBJECT LO_MATERIAL. "CREATE OBJECT
CALL METHOD LO_MATERIAL->GET_MATERIALS_FOR_TYPE
EXPORTING
IM_MTART = P_MTART
IMPORTING
ET_MARA = IT_MARA.

*PRINT OUTPUT
LOOP AT IT_MARA INTO WA_MARA.
WRITE:/ WA_MARA-MATNR, WA_MARA-MTART, WA_MARA-MEINS, WA_MARA-MATKL.
ENDLOOP.
CLASS CL_USERDEFINED_TYPES IMPLEMENTATION.
METHOD GET_MATERIALS_FOR_TYPE.
SELECT MATNR MTART MEINS MATKL FROM MARA
INTO TABLE ET_MARA
UP TO 50 ROWS
WHERE MTART = IM_MTART .
ENDMETHOD.
ENDCLASS.

You might also like