You are currently viewing SAP – ABAP Calling Function Module

SAP – ABAP Calling Function Module

In this article we will discuss about how to call a function module in ABAP Program and also learn about other way to make a ALV Grid i.e. different from previous in which we have to write code to pass Internal Table Name, Field Name, Field Text, Field Color to make Field Catalouge again and again but in this Article we will see how to call Function Module and  Writing code only once of making field catalogue how can we make ALV Grid.

 

Syntax to call Function Module: –

CALL FUNCTION 'FUNCTION MODULE NAME'
EXPORTING
IMPORT_PARAMETER = 'Value’ OR  VARIABLE1
IMPORTING
EXPORT_PARAMETER = VARIABLE2
TABLES
INTERNAL_TABLE   =  INTERNAL_TABLE
EXCEPTIONS
EXCEPTIOPN_PARAMETER = VARIABLE3.

Exporting means you are exporting Input value to Import parameter of Function Module.

Importing means ABAP Program Importing the Output Value from Export Parameter of Function Module.

Tables works as both Importing and Exporting the Internal Table.

Exceptions are defined in the Function Module which your ABAP Program may get if any error occurs.

 

For Example: –

Let’s make a ALV Grid report of Bill Details.

Source Code: –

*&---------------------------------------------------------------------*
*& Report  ZBILL_ALV_GRID_REPORT
*&
*&---------------------------------------------------------------------*
*&
*&
*&---------------------------------------------------------------------*
REPORT  ZBILL_ALV_GRID_REPORT.
*---Calling the type pool SLIS to inherit all of its fields-----
TYPE-POOLS SLIS.
" ---- WORK-AREA AND INTERNAL TABLE DECLARATION ----
DATA: WA_BILL TYPE ZBILL,
IT_BILL TYPE ZBILL OCCURS 0.
DATA: WA_FCAT   TYPE SLIS_FIELDCAT_ALV,
IT_FCAT   TYPE SLIS_T_FIELDCAT_ALV,

WA_LAYOUT TYPE SLIS_LAYOUT_ALV,

WA_TOP    TYPE SLIS_LISTHEADER,
IT_TOP    TYPE SLIS_T_LISTHEADER.
DATA : P_BILLNO TYPE ZBILL-BILNO.
SELECTION-SCREEN BEGIN OF BLOCK A WITH FRAME TITLE TEXT-001.
PARAMETERS: P_DOCNO TYPE  ZBILL-DOC_ID.
SELECTION-SCREEN END OF BLOCK A.
START-OF-SELECTION. " for selection of data from database.
PERFORM GET_BILLS.
END-OF-SELECTION.
PERFORM BUILD_FIELD_CATALOG USING 'IT_BILL'. "Passing Internal Table Name.
PERFORM ALV_LAYOUT.
PERFORM ALV_GRID_DISPLAY.
TOP-OF-PAGE.
PERFORM TOP_OF_BILL.

*&---------------------------------------------------------------------*
*&      Form  GET_BILLS
*&---------------------------------------------------------------------*
*       Selection of BILL table data
*----------------------------------------------------------------------*
FORM GET_BILLS.
CALL FUNCTION 'ZFM_BILL_DETAIL'
EXPORTING
BILLDOCNO = P_DOCNO
IMPORTING
BILLNO    = P_BILLNO
TABLES
IBILL    =  IT_BILL.
ENDFORM.

FORM BUILD_FIELD_CATALOG  USING    VALUE(P_TAB).
PERFORM ALV_FIELD_CATALOG USING P_TAB 'DOC_ID' 'Document ID' 'C10'.
PERFORM ALV_FIELD_CATALOG USING P_TAB 'BILNO' 'Bill Number' 'C21'.
PERFORM ALV_FIELD_CATALOG USING P_TAB 'BILDESC' 'Description' 'C21'.
PERFORM ALV_FIELD_CATALOG USING P_TAB 'QUANTITY' 'Total Quantity' 'C1'.
PERFORM ALV_FIELD_CATALOG USING P_TAB 'AMOUNT' 'Amount' 'C21'.              "Passing Internal Table Name, Field Name, Field Text and Color Code.
ENDFORM.                    " BUILD_FIELD_CATALOG
*&---------------------------------------------------------------------*
*&      Form  ALV_FIELD_CATALOG
*&---------------------------------------------------------------------*
*       Preparing ALV field catalog
*----------------------------------------------------------------------*
FORM ALV_FIELD_CATALOG using P_IT_BILL VALUE(P_FIELDNAME) VALUE(P_FIELDTEXT)
VALUE(P_COLOR).
DATA LV_COL TYPE I VALUE 0.
IF NOT IT_BILL IS INITIAL.
LV_COL            = 1 + LV_COL.      "INCREMENTING POSITION OF COLUMN
WA_FCAT-COL_POS   = LV_COL.         "COLUMN POSITION
WA_FCAT-FIELDNAME = P_FIELDNAME.       "TECHNICAL FIELD NAME
WA_FCAT-TABNAME   = P_IT_BILL.     "OUTPUT TABLE NAME
WA_FCAT-SELTEXT_L = P_FIELDTEXT. "FIELD TEXT
WA_FCAT-EMPHASIZE = P_COLOR.          "FOR COLUMN COLOR
APPEND WA_FCAT TO IT_FCAT.          "PREPARING THE FIELDCAT TABLE
CLEAR WA_FCAT.
ELSE.
MESSAGE W890(ZWALIA) WITH 'No Bill Detail Found'.
ENDIF.
ENDFORM.  " end alv_field_catalog

&---------------------------------------------------------------------*
*&      Form  ALV_LAYOUT
*&---------------------------------------------------------------------*
*       Preparing the ALV Layout
*----------------------------------------------------------------------*
FORM ALV_LAYOUT.
WA_LAYOUT-ZEBRA = 'X'.             "CALLING THE ZEBRA LAYOUT
WA_LAYOUT-COLWIDTH_OPTIMIZE = 'X'.
ENDFORM.
FORM TOP_OF_BILL.
REFRESH IT_TOP.
WA_TOP-TYP  = 'H'.            "HEADER TYPE
WA_TOP-KEY = 'Bill Details'.
*    WA_TOP-INFO = 'Bill Details'. "HEADER TEXT
APPEND WA_TOP TO IT_TOP.
CLEAR WA_TOP.
WA_TOP-TYP  = 'S'.            "NORMAL LINE TYPE
WA_TOP-KEY = 'Report:'.     "NORMAL LINE TEXT
WA_TOP-INFO = SY-REPID.
*    CONCATENATE WA_TOP-INFO SY-REPID INTO WA_TOP-INFO.
"CONCATENATING THE TEXT INFO WITH PROGRAM NAME
APPEND WA_TOP TO IT_TOP.
CLEAR WA_TOP.
WA_TOP-TYP  = 'S'.
WA_TOP-KEY = 'User: '.
WA_TOP-INFO = SY-UNAME.
APPEND WA_TOP TO IT_TOP.
CLEAR WA_TOP.
WA_TOP-TYP  = 'S'.
WA_TOP-KEY = 'Date:'.
WA_TOP-INFO = SY-DATUM.
    APPEND WA_TOP TO IT_TOP.
CLEAR WA_TOP.
WA_TOP-TYP  = 'S'.
WA_TOP-KEY = 'Time:'.
WA_TOP-INFO = SY-UZEIT.
APPEND WA_TOP TO IT_TOP.
CLEAR WA_TOP.
if P_BILLNO is not initial.
WA_TOP-TYP  = 'S'.
WA_TOP-KEY = 'Bill No.:'.
WA_TOP-INFO = P_BILLNO.
APPEND WA_TOP TO IT_TOP.
CLEAR WA_TOP.
"-CALLING FUNCTION MODULE FOR DISPLAYING TOP OF PAGE-------------------*
CALL FUNCTION 'REUSE_ALV_COMMENTARY_WRITE'
EXPORTING
IT_LIST_COMMENTARY       = IT_TOP "PASSING THE INTERNAL TABLE
*             I_LOGO                   =
*     I_END_OF_LIST_GRID       =
*     I_ALV_FORM               =.
ENDFORM.

*&---------------------------------------------------------------------*
*&      Form  ALV_GRID_DISPLAY
*&---------------------------------------------------------------------*
*       Preparing the final output by using Grid Display
*----------------------------------------------------------------------*
FORM ALV_GRID_DISPLAY.
IF NOT IT_BILL IS INITIAL AND NOT IT_FCAT IS INITIAL.
CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
EXPORTING
I_CALLBACK_PROGRAM                = SY-REPID
I_CALLBACK_TOP_OF_PAGE            = 'TOP_OF_BILL'
IS_LAYOUT                         = WA_LAYOUT
IT_FIELDCAT                       = IT_FCAT
TABLES
T_OUTTAB                          = IT_BILL "Final output table
EXCEPTIONS
PROGRAM_ERROR                     = 1
OTHERS                            = 2.
IF sy-subrc <> 0.
MESSAGE 'Report not Generated' TYPE 'I'.
ENDIF.
ENDIF.
ENDFORM.

 

Output: –

Enter the Bill Document No. = 19.

Now, Press F8 key We are getting the following result:-

 

Now Go Back, Clear Bill Document No.

Press F8 again. We are getting the following result:-

 

Dinesh Kumar Bansal

Dinesh Kumar Bansal is an Indian Software Developer, who is working on ASP.Net, MS-SQL, SAP-ABAP technologies from last one year. His Basic Principle of developing software is, “You have to clear about, what do you want to do, how can it be done”. He always says that development can be done with a cool & fresh mind.

Leave a Reply