abap create functionexample(scribd)

Upload: wyfwong

Post on 14-Apr-2018

213 views

Category:

Documents


0 download

TRANSCRIPT

  • 7/29/2019 ABAP Create FunctionExample(Scribd)

    1/6

    Page 1 of6

    ABAP Create function module Example

    Function modules form a very important part in Modularization of the code. They are used to

    reduce the redundancy of code , improves debugging and readability of the program. Thefollowing tutorial explains in depth how to create your own custom function module(FM) and

    using it.

    Go to SE37.

    First lets create a function group to hold all our function modules.

    In SE37, navigate to Goto>> Function Groups >> Create Group.

    Give the name of the function group

    Thats it. Now we will create a function module. Same SE37 transaction create a FM

    zau_getwerks.

    Give function group name we just created and give a short text for the FM

    http://images.newtosap.info/se37_create.pnghttp://images.newtosap.info/create_fun_group2.pnghttp://images.newtosap.info/create_fun_group.png
  • 7/29/2019 ABAP Create FunctionExample(Scribd)

    2/6

    Page 2 of6

    We are creating this FM which accepts a material number and gives the plant details for that

    material.

    Create an import parameter lv_matnr of type matnr. We can make it pass by value also bychecking Pass by value. By default it is pass by reference only. If optional is checked then

    passing this parameter is not mandatory in the program.

    Import Parameters - FM

    Here I will create a output table named output which is having a structure of zty_material.Create a zty_material structure from SE11 . Data type >> Create structure and add matnr and

    werks component to that structure.

    Tables in FMWe will add exceptions now. Exceptions provide a easy way for error handling in FMs. We will

    create 2 FMs

    One FM when we enter invalid material and another when data is not present related to plant.(No

    data in marc table)

    Exceptions in FM

    Now the main coding/logic part of the program. Its a simple select statement. And we are

    checking sy-subrc to check whether we obtained data or not. If no data is fetched then we raise

    appropriate exceptions by using RAISE command.

    http://images.newtosap.info/exceptions_fm.pnghttp://images.newtosap.info/tables_fm.pnghttp://images.newtosap.info/import_parameters_fm.pnghttp://images.newtosap.info/new_fm.png
  • 7/29/2019 ABAP Create FunctionExample(Scribd)

    3/6

    Page 3 of6

    Source Code FM

    FUNCTION zau_getwerks.

    *"----------------------------------------------------------------------

    *"*"Local Interface:

    *" IMPORTING*" REFERENCE(LV_MATNR) TYPE MATNR

    *" TABLES

    *" OUTPUT STRUCTURE ZTY_MATERIAL

    *" EXCEPTIONS

    *" NOMAT

    *" NODATA

    *"----------------------------------------------------------------------

    DATA: v_matnr TYPE matnr.

    SELECT matnr werks FROM marc

    INTOTABLEoutput

    WHERE matnr EQ lv_matnr. IF sy-subrc NE0.

    SELECTsingle matnr FROM mara INTO v_matnr WHERE matnr EQ lv_matnr.

    IF sy-subrc NE0.

    RAISE nomat.

    ELSE.

    RAISE nodata.

    ENDIF.

    ENDIF.

    ENDFUNCTION.

    Activate the FM.

    Now activate the FM and test the FM by clicking executes. Pass a material number and see theoutput

    http://images.newtosap.info/source_code_fm.png
  • 7/29/2019 ABAP Create FunctionExample(Scribd)

    4/6

    Page 4 of6

    Test FM

    FM Output

    Now lets create a Z program which uses the FM we created.

    *&---------------------------------------------------------------------*

    *& Report ZAU_FUNCTIONTEST

    *&

    *&---------------------------------------------------------------------*

    *&

    *& New To SAP : http://www.newtosap.info

    *&---------------------------------------------------------------------*

    REPORT zau_functiontest.TYPES: BEGINOF ty_material,

    matnr TYPE matnr,

    werks TYPE werks_d,

    ENDOF ty_material.PARAMETERS: p_matnr TYPE mara-matnr.

    DATA: it_marc TYPESTANDARDTABLEOF ty_material,

    wa_marc TYPE ty_material.

    http://images.newtosap.info/fm_test2.pnghttp://images.newtosap.info/fm_test.png
  • 7/29/2019 ABAP Create FunctionExample(Scribd)

    5/6

    Page 5 of6

    CALLFUNCTION'ZAU_GETWERKS'

    EXPORTING

    lv_matnr = p_matnr

    TABLES

    output = it_marc

    EXCEPTIONS

    nomat = 1

    nodata = 2

    OTHERS = 3.IF sy-subrc EQ0.

    LOOPAT it_marc INTO wa_marc.

    WRITE:/5 wa_marc-matnr, wa_marc-werks.

    ENDLOOP.ELSEIF sy-subrc EQ1.

    MESSAGE'Invalid material'TYPE'S' DISPLAY LIKE'E'.

    ELSEIF sy-subrc EQ2. MESSAGE'No data present for plant'TYPE'S' DISPLAY LIKE'E'.ENDIF.

    Thats it. Its as simple as that. Every exception is given numbers by default and we can checksy-subrc for these numbers to identify the exception.

    Running Report

    Report Output

    http://images.newtosap.info/report_out_fm.pnghttp://images.newtosap.info/run_report_fm.png
  • 7/29/2019 ABAP Create FunctionExample(Scribd)

    6/6

    Page 6 of6