8 modularization techniques

34
Modularization techniques in SAP ABAP using Function Modules, sub-routines, includes and classes. 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. Sub-routines. 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 dosen`t 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,

Upload: kiran

Post on 14-Feb-2016

36 views

Category:

Documents


2 download

DESCRIPTION

8 Modularization Techniques

TRANSCRIPT

Page 1: 8 Modularization Techniques

Modularization techniques in SAP ABAPusing Function Modules, sub-routines, includes and classes.

Modularization TechniquesThe 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.

Sub-routines.

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 dosen`t 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 .

Page 2: 8 Modularization Techniques

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>

Page 3: 8 Modularization Techniques

TABLES <TABLE1> <TABLE2>.

ENDFORM.

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

Using Classes in SAP ABAP

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

Page 4: 8 Modularization Techniques

Creating and using of Function Modules in SAP ABAP calling function modules in SAP ABAP programs

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 acts 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 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.

Page 5: 8 Modularization Techniques

 A Popup will open Click on 'Yes' and provide short text, click on save .

 Select Local Object .

Page 6: 8 Modularization Techniques

 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 .

Page 7: 8 Modularization Techniques

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

Page 8: 8 Modularization Techniques

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

Page 9: 8 Modularization Techniques

 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 .

Page 10: 8 Modularization Techniques

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.

Page 11: 8 Modularization Techniques

 Provide title, select Executable Program from drop-down and Save as local object.

 Source Code

Declare work area and a input field (parameter) .

Page 12: 8 Modularization Techniques

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).

Page 13: 8 Modularization Techniques

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.

Page 14: 8 Modularization Techniques

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

Page 15: 8 Modularization Techniques

Using exceptions in Function Modules in SAPExceptions: Exceptions are used to catch certain type of errors in Function Modules.

When ever 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 .

Page 16: 8 Modularization Techniques

Save, Activate and execute

A popup will open, just click on save without giving any value.

Page 17: 8 Modularization Techniques

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 .

Page 18: 8 Modularization Techniques

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

Page 19: 8 Modularization Techniques

MESSAGE 'Please enter a material no' TYPE 'E'.

ENDIF.

WRITE : WA_MARA-MATNR, WA_MARA-MTART, WA_MARA-MEINS, WA_MARA-MBRSH . "Display result

Excute the program without giving any input, we will get an error message.

Page 20: 8 Modularization Techniques

Using changing and table parameters in Function ModulesRequirement: Develop a Function Module to get 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, insted 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 parameters 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), short text and enter.

Go to import tab, provide parameter name as IM_MTART -> TYPE -> MARA-MTART, enter.

Page 21: 8 Modularization Techniques

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.

Page 22: 8 Modularization Techniques
Page 23: 8 Modularization Techniques

Sub-routines in SAP ABAPWhat are sub-routines in SAP ABAP ? Using sub-routines in SAP ABAP, differences between sub-routines and Function Modules

These are also sub programs, which contains set of re-usable statements for doing some specific task.These are mainly used for re-usability with in the same program, how ever we can re-use these sub-routines in other programs too.Sub-routines contains importing and exporting parameters.

Function Modules V/S Sub-routines

The main difference between Function Modules and sub-routines is sub-routines dosen`t contain any exceptions and sub-routines 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

Page 24: 8 Modularization Techniques

Sub-routine types and passing values to sub-routines in SAP ABAPBasically there are two types of sub-routines in SAP ABAP programming.

1. Local sub-routines.2. External sub-routines.

Local sub-routines

These are sub-routines in which definition (perform) and implementation (from) are available in the same program.

External sub-routines

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

Passing values with sub-routines

There are three ways to pass the values with sub-routines.

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.

Page 25: 8 Modularization Techniques

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.

Page 26: 8 Modularization Techniques

Sub-routines reusability, External Sub-routinesusing tables in performs and reusability of performs in different ABAP programs.

Step1: Create a program ZSAPN_SUBROUTINE_REUSE1 and create a perform to get material details for a material type.

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 decleration

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 implementatio

"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

Step2: Create another program ZSAPN_SUBROUTINE_REUSE2 and call perform external program ZSAPN_SUBROUTINE_REUSE1.

Syntax for calling external subroutine is:

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

Example program2:

REPORT ZSAPN_SUBROTINE_REUSE2.

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

WA_MARA TYPE MARA. "work area

Page 27: 8 Modularization Techniques

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.

Page 28: 8 Modularization Techniques

Difference between changing and table parameters in Function Modules SAP ABAPChanging Parameters Table Parameters

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

Tables can only be used for multiple records(internal tables).

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

Page 29: 8 Modularization Techniques

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 break points.

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. <p.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.

Page 30: 8 Modularization Techniques

BREAK-POINT. "Static break-point

Save, activate and execute the program.

2. Working with Dynamic Break point.

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).

Page 31: 8 Modularization Techniques

Now execute the program, provide input ex: FERT and execute (F8), break-point 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.

Page 32: 8 Modularization Techniques

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.

<p.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.

<p.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.

Page 33: 8 Modularization Techniques

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.

</p.</p.</p.

Page 34: 8 Modularization Techniques