Вы находитесь на странице: 1из 33

MODULARIZATION TECHNIQUES IN SAP ABAP

CONTENT

● MODULARIZATION TECHNIQUES

● WHAT ARE FUNCTION MODULES ? HOW TO CREATE A FUNCTION


MODULE IN SAP ?

● USING EXCEPTIONS IN FUNCTION MODULES IN SAP

● USING CHANGING AND TABLE PARAMETERS IN FUNCTION MODULES


IN SAP

● SUBROUTINES IN SAP ABAP

● SUBROUTINES TYPES AND PASSING VALUES TO SUBROUTINES IN SAP


ABAP

● HOW TO REUSE SUBROUTINES IN SAP ABAP ?

● DIFFERENCE BETWEEN CHANGING AND TABLE PARAMETERS IN


FUNCTION MODULES SAP ABAP

● DEBUGGING IN SAP ABAP PROGRAMMING

MODULARIZATION TECHNIQUES
The concept of modularization is dividing the main program into sub-programs
for better readability and re-usability. Modularization can be implemented in the
following ways.
● Include Programs.
● Function Modules.
● Subroutines.
● Classes.
● MACRO`s ( Use in HR ABAP only ).

Include Programs:
● These are sub-programs which contains a set of re-usable statements .
● These programs can not be executed directly.
● These include programs must be embedded inside a main program for
execution.
● These programs does not contain parameter interface, that is no importing
and exporting parameters.

Syntax : INCLUDE <include name>.

Example of using include programs:


TYPES : BEGIN OF TY_MARA,
MATNR TYPE MARA-MATNR,
MTART TYPE MARA-MTART,
MEINS TYPE MARA-MEINS,
MBRSH TYPE MARA-MBRSH,
END OF TY_MARA.<br>DATA : IT_MARA TYPE TABLE OF TY_MARA.
DATA : WA_MARA TYPE TY_MARA.
SELECT MATNR, MTART, MEINS, MBRSH FROM MARA INTO TABLE
IT_MATA.
LOOP AT IT_MARA INTO WA_MARA .

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


WA_MARA-MTART.
ENDLOOP.

To make the above program with includes, follow below steps.


INCLUDE ZDATA_DECLERATIONS . "DOUBLE CLICK ON
ZDATA_DECLERATIONS AND CREATE A INCLUDE AND ADD DATA
DECLERATIONS AND ACTIVATE
INCLUDE ZMAIN_LOGIC. " DOUBLE CLICK ON ZMAIN_LOGIC , CREATE A
INCLUDE AND ADD THE REMAINING LOGIC (FROM SELECT....TO
....ENDLOOP).

Function Modules
● These are also sub-programs which contains set of reusable statements for
better readability and re-usability .
● Function modules contains parameter interface, that is importing and
exporting parameters.
● These Function Modules can be executed independently .
● Function modules contains exceptions to catch certain type of errors.

Examples for Function Modules are : GUI_DOWNLOAD, GUI_UPLOAD.


T-code for Function Module explorer is SE37.

In our next chapters we will learn how to create and use a function module
Creating Function Modules in SAP ABAP

Sub-routines in SAP ABAP.


● Sub-routines are also sub-programs in SAP ABAP which contains certain
re-usable statements.
● Most of the times we use sub-routines for re-usability inside the program.
● We may use sub-routines for external re-usability.
● These sub-routines contains importing and exporting parameters .

Syntax :
**DEFINING SUBROUTINE
PERFORM <SUBROUTINE NAME> USING <AV1> <AV2>
CHANGING <CHANGING1> <CHANGING2>
TABLES <TABLE1> <TABLE2>.
**IMPLEMENTING SUBROUTINE
FORM <SUBROUTINE NAME> USING <FV1> <FV2>
CHANGING <CHANGING1> <CHANGING2>
TABLES <TABLE1> <TABLE2>.

ENDFORM.

**IN THE ABOVE SYNTAX <AV1> AND <AV2> ARE ACTUAL PARAMETERS
AND <FV1> <FV2> ARE FORMAL PARAMETERS

We will learn creating and using sub-routines in next chapters Using


subroutines in SAP ABAP

Using Classes in SAP ABAP


● Classes are Object Oriented implementations of SAP ABAP.
● Classes are used for better re-usability.

We will learn Object Oriented ABAP in next course Using Classes in SAP
ABAP

WHAT ARE FUNCTION MODULES ? HOW TO CREATE A


FUNCTION MODULE IN SAP ?
Function Modules are sub-programs which contains set of re-usable statements
with importing, exporting parameters, exceptions. Unlike include programs
Function Modules can be executed independently.

Function Group in SAP ABAP


Function group is a container of Function modules, every Function Module
must be saved in a Function group. T-codes for Function Modules and Function
groups are SE37 OR SE80. We can save N number of Function Modules in a
Function Group.

Components of Function Module:


Import: These are input parameters of a Function Module.

Export: These are output parameters of a Function Module.

Changing: These are parameters which act as importing and exporting


parameters to a Function Module.

Tables: These are internal tables which also acts as importing and exporting
parameters.

Exceptions: Exceptions in Function Modules are used to catch certain type of


errors.

In this lesson we are going to learn how to create Function Group and Function
Module, in order to create a Function Module we need a Function Group (We
can save in existing one also), follow the below steps to create a Function
Group.

Go to SE80.
Select 'Function Group' from drop-down list.
Provide a Function Group name ex: ZTFUNCTIONGROUP and press enter.
A Popup will open Click on 'Yes' and provide short text, click on save.

Select Local Object.


Right-Click on Function Group name (ZTFUNCTIONGROUP) and activate.

Creating Function Module:


Here we take a small requirement and create a Function Module.

Requirement: Develop a Function Module, which displays Material details for


a material no.

Go to SE37, provide a Function Module name ZSAPN_GET_MATERIAL,


click on create.

A pop up will open, provide a Function Group name ZTEST_SAPNUTS and


short text, Save .
An information message will come saying that Function Module name is
reserved for SAP , just click on continue icon

Add below parameters.


Import: Select Import tab and enter IM_MATNR under 'Parameter name',
TYPE under typing and MARA-MATNR under 'Associated Type', enter. See
below image

Export: Select Export tab and enter EX_MARA under 'Parameter name',
TYPE under typing and MARA under Associated Type, enter. See below
image

Source Code.
Select Source Code tab and write below code to get material details, save as
local object and activate.

SELECT SINGLE * FROM MARA


INTO EX_MARA
WHERE MATNR = IM_MATNR.

Test Function Module.


Click on Execute icon (F8), and give a material no, execute (F8).

It will get material details

Using Function Modules in ABAP Programs


CALL FUNCTION is a keyword which is used to call a Function Module in
ABAP Programs, now we use the above created Function Module in our ABAP
program.

Go to SE38 and Provide name as ZSAPN_GET_MATERIAL and click on


Create.

Provide a title, select Executable Program from drop-down and Save as local
object.
Source Code
Declare work area and an input field (parameter).
DATA : WA_MARA TYPE MARA . "work area to store material details

PARAMETERS: P_MATNR TYPE MARA-MATNR . "Material no input

START-OF-SELECTION. "Default event

To Call a Function Module, click on 'Pattern'


A pop up will open, select CALL FUNCTION radio, provide Function Module
name and click on continue icon (tick mark at the bottom).
The below code will be generated.

CALL FUNCTION 'ZSAPN_GET_MATERIAL'


EXPORTING
IM_MATNR =
IMPORTING
EX_MARA =
IF SY-SUBRC <> 0.
* Implement suitable error handling here
ENDIF.

Now pass material no parameter and receive material data into a work area, full
program code is below.

REPORT ZSAPN_GET_MATERIAL.
DATA : WA_MARA TYPE MARA .

PARAMETERS : P_MATNR TYPE MARA-MATNR.

START-OF-SELECTION.

CALL FUNCTION 'ZSAPN_GET_MATERIAL'


EXPORTING
IM_MATNR = P_MATNR
IMPORTING
EX_MARA = WA_MARA.
IF SY-SUBRC <> 0.
* Implement suitable error handling here

ENDIF.

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


MBRSH . "Display result

Execute (F8) and test the program.

Input Screen

Output

USING EXCEPTIONS IN FUNCTION MODULES IN SAP

Exceptions: Exceptions are used to catch certain type of errors in Function


Modules.
Whenever an exception is raised, it will be caught in system variable SY-
SUBRC.

Continuation to the previous lesson Creating and using of Function Modules in


SAP ABAP, we add an exception for no input.

In the previous example, we are getting material details for a material input,
now we add an exception to check whether the input is provided or not.

Go to 'Exceptions' tab and add NO_MATNR under Exception and short text.

Write below code under Source Code .


Save, Activate and execute

A popup will open, just click on 'Save' without giving any value.
Now you can see NO_MATNR exception.

Using Function module exceptions in SE38 program.


When ever an exception is raised, it will be stored in system variable SY-
SUBRC .

REPORT ZSAPN_GET_MATERIAL.

DATA : WA_MARA TYPE MARA .


PARAMETERS : P_MATNR TYPE MARA-MATNR .

START-OF-SELECTION.

CALL FUNCTION 'ZSAPN_GET_MATERIAL'


EXPORTING
IM_MATNR = P_MATNR
IMPORTING
EX_MARA = WA_MARA
EXCEPTIONS
NO_MATNR = 1
OTHERS = 2.
IF SY-SUBRC = 1.
* Implement suitable error handling here
MESSAGE 'Please enter a material no' TYPE 'E'.
ENDIF.

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


MBRSH . "Display result

Execute the program without giving any input, we will get an error message.
USING CHANGING AND TABLE PARAMETERS IN FUNCTION
MODULES IN SAP

Requirement: Develop a Function Module to get a list of materials for a


material type.

Analysis: As per business a material type can have 'n' number of materials(we
have to use internal tables), so we need to get materials from MARA for a
material type input, we have to use function module tables.

As per SAP, tables in function modules obsolete, instead using them, we can
create a table type in SE11, use it as exporting parameter.

Changing and Tables are parameters of Function Modules. Changing: Changing


parameter acts as both exporting and importing parameters. Tables: These are
internal tables which can import and export.

Using Tables in SAP Function Modules


Go to SE37, provide name as ZSAPN_FM_TABLES, click on create.

A pop up will open, provide a function group (we have created already), a short
text and enter.

Go to import tab, provide parameter name as IM_MTART -> TYPE -> MARA-
MTART, enter.
Go to 'Tables' tab, provide parameter name as T_MARA -> LIKE -> MARA,
enter.You will get a warning message saying tables are obsolete just ignore it,
hit enter.

Go to source code tab, add below logic to material details.

Execute the Function Module(F8), provide a material type ex: FERT,


execute(F8), you will get list of materials for that material type.
SUBROUTINES IN SAP ABAP

These are also sub programs, which contains set of re-usable statements for
doing some specific task.

These are mainly used for re-usability within the same program, however, we
can re-use these Subroutines in other programs too.

Subroutines contains importing and exporting parameters.

Function Modules V/S Subroutines


The main difference between Function Modules and Subroutines are sub-
routines does not contain any exceptions and Subroutines are mainly used for
local modularization where as function modules contains exceptions and used
for global modularization.
sub-routines have definition and implementation parts.
Syntax:DEFINITION PART:
PERFORM <SUBROUTINE NAME> USING <V1> <V2> "DOUBLE CLICK ON SUBROUTINE
NAME TO CREATE
CHANGING <CV1> <CV2>
TABLES <IT1> <IT2> .

**<V1> <V2> (VARIABLES), <CV1> <CV2> (CHANGING VARIABLES) AND <IT1> <IT2>
(IMPORTING TABLES) ARE ACTUAL PARAMETERS

Syntax IMPLEMENTATION PART


FORM <SUBROUTINE NAME> USING <FV1> <FV2>
CHANGING <FCV1> <FCV2>
TABLES <FIT1> <FIT2>.
ENDFORM.
**<FV1> <FV2>, <FCV1> <FCV2> AND <FIT1> <FIT2> ARE FORMAL PARAMETERS

SUBROUTINES TYPES AND PASSING VALUES TO SUBROUTINES


IN SAP ABAP

Basically there are two types of Subroutines in SAP ABAP programming.


1. Local Subroutines.
2. External Subroutines.

Local Subroutines
These are Subroutines in which definition (perform) and implementation (from)
are available in the same program.

External Subroutines
Subroutines which contains definition and implementation are available in
different programs are called as external Subroutines. The implemented
program must be of type sub-routine pool.

Passing values with Subroutines


There are three ways to pass the values with Subroutines.
1. Pass by reference.
2. Pass by value.
3. Pass by value and return.

1.Pass by reference
In this type, the actual and formal parameters are referred to the same memory.
If the formal parameter is changed, the actual parameter is also changed.

PERFORM <PERFORM NAME> USING <A> . "A IS ACTUAL PARAMETER


FORM <PERFORM NAME> USING <F> .
" F IS FORMAL PARAMETER

<F> = SOME IMPLEMENTATION


ENDFORM.

2.Pass by Value
In this type, the actual and formal parameters will be referring to separate
memory. The formal parameter is changed, the actual parameter will not be
changed.

The key word VALUE( ) will identify that it is pass by value else it is pass by
reference.

PERFORM <PERFORM NAME> USING VALUE (<A>) . "A IS ACTUAL PARAMETER


FORM <PERFORM NAME> USING <F> . " F IS FORMAL PARAMETER

<F> = SOME IMPLEMENTATION


ENDFORM.

3.Pass by value and return


In this type, the actual and formal parameters will be referring to separate
memories. If the formal parameter is changed, the actual parameter is also
changed after executing the FORM...ENDFORM.

The key word CHANGING( ) will identify that it is pass by value else it is pass
by reference.

PERFORM <PERFORM NAME> CHANGING VALUE (<A>) . "A IS ACTUAL PARAMETER


FORM <PERFORM NAME> USING <F> . " F IS FORMAL PARAMETER

<F> = SOME IMPLEMENTATION


ENDFORM.

Subroutines statements addition


Addition Method

using v1 Pass by reference

changing v1 Pass by reference

using value(v1) Pass by value

changing value(v1) Pass by value and result

Things to remember before creating Subroutines


● Perform and form must contain the same number of parameters.
● The syntax on the form statement alone determines the method by which
a parameter is passed.
● The 'VALUE()' addition cannot be used on the perform statement, only
be used in form.
● 'USING' must come before changing.
● The addition 'USING' can only occur once in a statement. The same rule
applies to changing.

Passage Method Description Advantages

By reference Passes a pointer to the original memory Very efficient


location.

By value Allocates a new memory location for use Prevents changes to


within the subroutine. The memory is freed passed variable
when the subroutine ends.

By value and result Similar to pass by value, but the contents of Allows changes and
the new memory is copied back into the allows a rollback
original memory before returning.
HOW TO REUSE SUBROUTINES IN SAP ABAP ?

In the below lesson you will be able to learn using tables in performs and re-
usability of performs in different ABAP programs.

Step 1: Create a program ZSAPN_SUBROUTINE_REUSE1 and create a


perform to get material details for a material type.

Example program 1:
REPORT ZSAPN_SUBROTINE_REUSE1.
DATA : IT_MARA TYPE TABLE OF MARA.
DATA : WA_MARA TYPE MARA.
CONSTANTS: C_MTART TYPE MARA-MTART VALUE 'FERT'. "Constant declaration

PERFORM GET_MATDATA TABLES IT_MARA USING C_MTART. "Perform to get material


data by passing material type

LOOP AT IT_MARA INTO WA_MARA . "Display material


WRITE: / WA_MARA-MATNR, WA_MARA-MTART.
ENDLOOP.
FORM GET_MATDATA TABLES P_IT_MARA TYPE STANDARD TABLE "perform
implementation
"Insert correct name for <...>
USING P_C_MTART.
SELECT * FROM MARA INTO TABLE P_IT_MARA UP TO 50 ROWS WHERE MTART =
P_C_MTART.
ENDFORM. " GET_MATDATA
Step 2: Create another program ZSAPN_SUBROUTINE_REUSE2 and call
perform external program ZSAPN_SUBROUTINE_REUSE1.

The syntax for calling external subroutine is:


PERFORM <PERFORM NAME>(<PROGRAM NAME>) TABLES <TABLES> USING
<PARAMS> CHANGING <PARAMS>.

Example program 2:
REPORT ZSAPN_SUBROTINE_REUSE2.

DATA : IT_MARA TYPE TABLE OF MARA, "material internal table


WA_MARA TYPE MARA. "Work area

CONSTANTS: C_MTART TYPE MARA-MTART VALUE 'HALB'. "Constant

PERFORM GET_MATDATA(ZSAPN_SUBROTINE_REUSE1) TABLES IT_MARA USING


C_MTART. "Calling external subroutine

LOOP AT IT_MARA INTO WA_MARA. "Display data


WRITE:/ WA_MARA-MATNR, WA_MARA-MTART, WA_MARA-MEINS.
ENDLOOP.
DIFFERENCE BETWEEN CHANGING AND TABLE PARAMETERS
IN FUNCTION MODULES SAP ABAP

Changing Parameters Table Parameters

Change parameter can be used for one Tables can only be used for multiple
record(work area) as well as multiple records(internal tables).
records(internal tables).

Can be used for input and output. Can be used for input and output.

Same instance will be changed ex: If Instance will be copied and make any
you pass 30 as parameter, if you changes to that, original will not change
change that inside function, the
original variable will be changed
DEBUGGING IN SAP ABAP PROGRAMMING

Debugging is one of the important part in trouble shooting of an ABAP


application, we can debug ABAP code by using breakpoints.

In SAP Programming there are two kinds of breakpoints.

Static Breakpoints: These can be set by using statement BREAK-POINT in


ABAP code, these breakpoints are not user specific, these will trigger for every
user. We need to delete these breakpoints manually.

Dynamic Breakpoints: These breakpoints are user specific, these will trigger
for specific user only. These breakpoints will be deleted automatically when
you log-off from SAP. These can be set in ABAP editor. Dynamic breakpoints
can be set in active (activated) source code only.

Dynamic breakpoints are of two types. External break-point: These is a type of


break-point, which will get activated even for Non SAP Applications, these
breakpoints will be triggered from SAP or from Non-SAP example from portal
screen.

Set it through Utilities - Setting - breakpoints.

Session break-point: This break-point will be activated for call only within
SAP system and its active till the User session is on.
E.g. SE38

These breakpoints have different behaviors in different types of coding blocks


ex: Function Modules, Sub-routines etc.

In this lesson we will discuss the behavior of breakpoints in each.

When we put break-point in some ABAP code, control will stop at the specific
place when executing the ABAP program, then it is debugging mode. We can
control debugging using function keys F5, F6, F7 and F8 or using toolbar
buttons in debugging screen.

1. Working with static break point.


Go to SE38, create a program ZSAPN_DEBUGGING, add below code and
activate.

REPORT ZSAPN_DEBUGGING.

SKIP.

BREAK-POINT. "Static break-point

Save, activate and execute the program.

2. Working with Dynamic Breakpoint.


Go to SE38, create a program ZSAPN_DEBUG, and add below code

REPORT ZSAPN_DEBUG.

DATA : IT_MARA TYPE TABLE OF MARA,


WA_MARA TYPE MARA.

PARAMETERS: P_MTART TYPE MARA-MTART.

SELECT * FROM MARA INTO TABLE IT_MARA UP TO 50 ROWS


WHERE MTART = P_MTART.

LOOP AT IT_MARA INTO WA_MARA.


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

Go to program source code, put cursor where you wants to set break-point and
click on set/delete external break-point icon (see image below).
Now execute the program, provide input ex: FERT and execute (F8), breakpoint
will trigger.

Now go to 'Desktop 3', which is friendly to see run-time variables and data,
double click on any variable to see respected value and data in right pane.
Techniques of debugging
Now, we will learn and understand the real techniques of debugging.
What are uses of F5, F6, F7 and F8 in debugging ?

These are function keys, which are used to control debugging ex: go to next
break point, execute perform/function module which out going into it etc.
F5 - When you press F5 in debugging, you will go to next step means you
program control goes to next line.

F6 - When you press F6 in debugging, it will execute the module without going
into it.F6 works for performs (subroutines), Function modules, Class methods
etc.

Ex for F6: we have a program, we have some function modules in the program,
when we click F5 in debugging control will go into function module source
code, we don`t want to go into function module, in that case we use F6, it will
not go into function module instead it will execute it in one step.

F7 - When you press F7 in debugging, it will completes the current


module/program in a single step.

Ex for F7: We have a program, we have a function module in the program, we


have put break point, when we press F7 it will completes the program if the
control is in program, when we press F7 it will complete the module( FM) when
the control is in function module.

F8 - When you press F8 in debugging, control will go to next break point if any
or completes the program execution.

When do we use external break-point?


Whenever we wants to debug a program, function module, Web Dynpro
application or any other object from external interface (other than GUI) like
portal, web dynpro etc, we need to use external break-point. We can use
external break point to debug from GUI also.

Вам также может понравиться