BADI and Types of BADI in SAP BADI (Business Add-In)
BADI and Types of BADI in SAP BADI (Business Add-In)
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.
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.
Provide short description, based on description choose a method and double click to add owr own
code.
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.
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.
REPORT ZALV_STR.
TABLES: MARA.
DATA : IT_MARA TYPE TABLE OF MARA .
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.
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
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 .
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.
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 .
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' .
START-OF-SELECTION .
PERFORM GET_DATA .
PERFORM CREATE_FCAT .
PERFORM CREATE_LAYOUT.
PERFORM DISPLAY_DATA .
END-OF-SELECTION .
FORM GET_DATA .
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 .
START-OF-SELECTION.
CALL FUNCTION 'LVC_FIELDCATALOG_MERGE' "create field catalog
EXPORTING
I_STRUCTURE_NAME = P_TABLE
CHANGING
CT_FIELDCAT = IT_FCAT.
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>.
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.
ELSE.
WA_EXCLUDE-FCODE = WA_FUN-FCODE. "add all remaining
APPEND WA_EXCLUDE TO IT_EXCLUDE.
CLEAR WA_EXCLUDE.
ENDIF.
ENDLOOP.
START-OF-SELECTION.
SELECT * FROM MARA INTO TABLE IT_MARA UP TO 50 ROWS.
*&---------------------------------------------------------------------*
* 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.
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:
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.
Expand page, right click on min window -> create -> Text.
Whenever we activate a smartfrom, one function module will be generated, when we execute it, it
will go to function module.
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.
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.
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 .
CONCATENATE is a string function which is used to add different variables into a single string
variable.
Syntax : CONCATENATE ... INTO .
WRITE :/ STRING .
*The out put will be 'ABC'
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 .
CONDENSE is a string function/keyword which is used to remove blank spaces in a given string.
Syntax: CONDENSE .
The keyword/ string function STRLEN is used to determine the length of a string in SAP ABAP
programs.
Syntax: STRLEN( ).
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).
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.
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.
START-OF-SELECTION.
SELECT VBELN MATNR ZMENG NETPR
FROM VBAP INTO TABLE IT_VBAP
WHERE ERDAT = P_DATE.
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.
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.
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.
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).
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.
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
Click on execute.
You will get material details for that material, now class with method is created we have to use it in
our program.
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.
Save, go back to methods and double click on GET_MATERIALS_FOR_TYPE and add the below
code.
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
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.
Program input.
Program output.
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.
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.
START-OF-SELECTION.
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
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.
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.
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
START-OF-SELECTION.
CALL METHOD LO_MATERIAL->GET_MATERIAL_DTAILS "call method
EXPORTING
IM_MATNR = P_MATNR
IMPORTING
EX_MARA = WA_MARA.
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.
Once you press enter, you will see a symbol (Constructor symbol), it indicates that the method is a
constructor method.
Click on parameter button and add below parameter( what ever the parameters you add, they will
become importing parameters only).
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.
METHOD GET_MATERIAL_DESCRIPTIONS.
ENDMETHOD.
Step2: Create object for material class, the object can be created using ABAP patterns also.
To create object, click on pattern button.
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.
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
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.
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.
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.
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.
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 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.
ENDMETHOD.
START-OF-SELECTION.
CALL METHOD LO_CLASS->GET_MATERIAL_DETAILS "get material details
EXPORTING
IM_MATNR = P_MATNR
IMPORTING
EX_MARA = WA_MARA.
ENDCLASS.
*CLASS IMPLEMENTATION
CLASS IMPLEMENTATION.
*METHODS, EVENTS IMPLEMENTATION
ENDCLASS.
ENDCLASS.
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
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
*PRINT OUTPUT
WRITE:/ WA_MARA-MATNR, WA_MARA-MTART, WA_MARA-MEINS, WA_MARA-MAKTL.
WRITE:/ WA_MAKT-MATNR, WA_MAKT-MAKTX.
ENDCLASS.
CREATE OBJECT LO_MATERIAL. "CREATE OBJECT
*PRINT OUTPUT
WRITE:/ WA_MARA-MATNR, WA_MARA-MTART, WA_MARA-MEINS, WA_MARA-MATKL.
WRITE:/ WA_MAKT-MATNR, WA_MAKT-MAKTX.
METHOD GET_MATERIAL_DESCRIPTION.
SELECT * FROM MAKT
INTO EX_MAKT
WHERE MATNR = IM_MATNR.
ENDSELECT.
ENDMETHOD.
ENDCLASS.
*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.
*PRINT OUTPUT
LOOP AT IT_MARA INTO WA_MARA.
WRITE:/ WA_MARA-MATNR, WA_MARA-MTART, WA_MARA-MEINS, WA_MARA-MATKL.
ENDLOOP.
*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.