function groups in abap

Upload: latha-jujari

Post on 14-Apr-2018

230 views

Category:

Documents


0 download

TRANSCRIPT

  • 7/27/2019 Function groups in ABAP

    1/12

    ABAP Programming Language Rev. 09/10/2008 1SAPtips Sample ABAP Programming Overview of Function Groups and Function Modules

    This segment from our 415 page ABAP Programming manual is being made

    available as a sample of our training manuals. Please contact

    [email protected] you have any SAP training needs, or visitwww.ERPtips.comfor more information.

    Written by Rehan Zaidi and John Verbestel

    Published by Klee Associates, Inc.

    Copyright Klee Associates, Inc., 2008, 2009

    Unit 3 Function Groups and Function Modules

    Overview of Function Groups and Function Modules

    The following figure describes the basic construct of a Function Group, which is a

    specialized program.

    Although created and referenced as the function group ZMAT, the underlyingprogram is named SAPLZMAT. The SAPL prefix defines this program as a

    mailto:[email protected]:[email protected]://www.erptips.com/http://www.erptips.com/http://www.erptips.com/mailto:[email protected]
  • 7/27/2019 Function groups in ABAP

    2/12

    Unit 3 Function Groups and Function Modules Klee Associates, Inc.

    2 Rev. 09/10/2008 ABAP Programming Language

    function group, and is created automatically by the Function Builder, SAPTransaction SE37.

    The program is organized by a series of Include programs for handling globaldata, and each individual function module.

    Function Modules are created separately, and although unique to the SAPsystem, they must be assigned to a Function Group.

    Creating a Function Group

    Function groups are created using transaction SE37 (function builder). AFunction Group is realized in the SAP Software system as a program, containinga logically related set of function modules.

    When creating function groups, realize that when a program (client) calls afunction module, the function group and all its global data are instantiatedand consume runtime memory until the end of the calling program (clientlifetime). Do not overload your function group as it may result in effecting theperformance of the calling program.

    Create the function group by selecting the menu path Goto Function Groups Create Group.

    The following popup will request the details of the function group. Customer

    function groups should begin with a Z or a Y.

  • 7/27/2019 Function groups in ABAP

    3/12

    Klee Associates, Inc. Unit 3 Function Groups and Function Modules

    ABAP Programming Language Rev. 09/10/2008 3

    After saving, the function group will be requested to be assigned to a package,then a change request for transport and control requirements.

    Creating a Function Module

    Function modules are created using transaction SE37 (function builder). The

    standard SAP system has a number of predefined function modules. Functionmodules are best used for carrying out database updates and communicatingwith different SAP systems.

    In contrast to subroutines, function modules do not need to be defined in thesource of your ABAP program. Function modules may have both input andoutput parameters. The input parameters can be mandatory or optional.

    You can also assign default values to parameters. Function modules alsoprovide the option of exception handling, which catches errors when the functionmodule executes. You may also test function modules from transaction SE37before including them in your programs.

    Function Modules must begin with a Z_ or a Y_, and can be created as follows:

    1. Enter the new function module name and click the Create button.

    2. Assign the function group.

  • 7/27/2019 Function groups in ABAP

    4/12

    Unit 3 Function Groups and Function Modules Klee Associates, Inc.

    4 Rev. 09/10/2008 ABAP Programming Language

    3. Define the interface to the function module.

    The Attributes of the function module should be reviewed first, as it controls thebehavior of the overall function module.

    The processing type controls the behavior of the function module.

    Normal Function Module

    Used for modularizing code and is not externally visible to othersystems, but globally visible within the same SAP software system.

    Remote Function Module

    Visible to external systems, and thus can be called via the RFCgateway.

    Update Function Module

    Denotes the use of the function module for Asynchronous updatepurposes.

    The three sub-selections pertain to the database logical unit of workand when the function module is executed in the context of theupdate task. More information can be found under AsynchronousUpdate Techniques.

  • 7/27/2019 Function groups in ABAP

    5/12

    Klee Associates, Inc. Unit 3 Function Groups and Function Modules

    ABAP Programming Language Rev. 09/10/2008 5

    The interface is based on the following:

    Inbound

    Data to be received from the calling program

    Note the commonality with subroutines, where you can pass byreference (Default) or pass by value.

    Optional parameters can be assigned a default value, and do not need tobe provided by the calling program.

    Outbound

    Data to be returned to the calling program

    Changing

    Data to be received and returned using the same reference variable

    Tables

    Obsolete interface for passing arrays of information. Use globaltable types going forward, in combination with the Changinginterface. However, many delivered function modules still use theTables interface.

  • 7/27/2019 Function groups in ABAP

    6/12

    Unit 3 Function Groups and Function Modules Klee Associates, Inc.

    6 Rev. 09/10/2008 ABAP Programming Language

    Exceptions

    Exceptions, resulting in non-zero values of the system variable SY-SUBRC.

    Exceptions are raised using the RAISING command inthe source code of the function module.

    In the figure above, the exception NOCUSTOMERFOUND is declared.

    Function Modules now have the added capability to use exception classes.This will be described in the Introduction to Object Oriented ABAP unit.

    4. Enter the source code.

  • 7/27/2019 Function groups in ABAP

    7/12

    Klee Associates, Inc. Unit 3 Function Groups and Function Modules

    ABAP Programming Language Rev. 09/10/2008 7

    The source code will show the current interface as comment statements. Do nottry to change the comment statements as it will make no difference to theinterface.

    Note the use of the RAISE statement in the code above to trigger an exception.When the RAISE statement occurs, the program processing jumps to the

    ENDFUNCTION, and the control is passed back to the calling application.

    Testing the Function Module

    The Function Builder has its own test workbench, and can be accessed using theTEST icon, the same one used in the ABAP Editor.

    On the main screen, select the test icon as demonstrated below.

    The next screen will request the input parameters for the function module beingtested.

  • 7/27/2019 Function groups in ABAP

    8/12

    Unit 3 Function Groups and Function Modules Klee Associates, Inc.

    8 Rev. 09/10/2008 ABAP Programming Language

    Click Execute orExecute in Debugging once the input fields are entered. Theresults are shown in the next image.

    The above was a successful test, and all the return parameters can be vieweddirectly. It can be seen that three records were returned in the T_Salesinfo table,and that the export structure E_Cust_Detail has information contained within.

    If the test failed, a result would appear as the next screen demonstrates. Theexception is displayed as the main result, and all other export fields are empty.

  • 7/27/2019 Function groups in ABAP

    9/12

    Klee Associates, Inc. Unit 3 Function Groups and Function Modules

    ABAP Programming Language Rev. 09/10/2008 9

    Working with Function Groups in the ObjectNavigator

    Use the package or the function group view in the object navigator (SAPTransaction SE80 to show the function group and it's modules so that they can

    be edited directly from the Workbench.

    An example is presented below:

  • 7/27/2019 Function groups in ABAP

    10/12

    Unit 3 Function Groups and Function Modules Klee Associates, Inc.

    10 Rev. 09/10/2008 ABAP Programming Language

    Using Function Modules in ABAP Programs

    For including function modules that already exist in the system, simply click on

    the icon from the ABAP Editor while in the Change mode. The dialogbox (shown in the next figure) appears:

    By entering the name of the function module in the CALL FUNCTION field, and

    pressing Enter, the relevant function module call will be added directly in theprogram at the cursor.

  • 7/27/2019 Function groups in ABAP

    11/12

    Klee Associates, Inc. Unit 3 Function Groups and Function Modules

    ABAP Programming Language Rev. 09/10/2008 11

    The following can be noted, when using the Pattern button to insert the functionmodule call:

    All interface parameters and exceptions are imported.

    Optional parameters, Importing parameters, and exceptions areautomatically commented out.

    A basic if statement is added to handle any exceptions, and acommented out message statement.

    You may then pass appropriate values to the function module to get the desiredresult. You must define suitable data objects that have types compatible to theparameters of the function module in question.

  • 7/27/2019 Function groups in ABAP

    12/12

    Unit 3 Function Groups and Function Modules Klee Associates, Inc.

    12 Rev. 09/10/2008 ABAP Programming Language

    Legal Speak:

    Purchase of this book or acquisition at an ERPtips class, constitutes an impliedagreement by the purchaser to a limited, license granted to the purchaser byKlee Associates, Inc., specifically for purposes limited to improving your personalknowledge of SAP software which does not in any way compete with Klee

    Associates.

    This manual is protected by copyright law, and is sold or is part of an ERPtipsclass with the following conditions:

    The manual will be used for your personal professional development.

    The manual will not be physically copied for distribution to other individuals orconverted to an electronic copy for distribution to other individuals, unlesscopyright permissions for copying have been acquired from Klee Associates,Inc.

    The manual will not be used to conduct training classes by you or otherindividuals, without the written permission of Klee Associates, Inc.

    Inclusion of the conceptsin this book in any internal client training material isallowed if the source of the concepts is acknowledged as follows:

    Copyright 2008, 2009

    All rights reserved.

    Used with permission.

    Klee Associates, Inc.

    NO WARRANTY: This documentation is delivered as is, and Klee Associatesmakes no warranty as to its accuracy or use. Any use of this documentation is atthe risk of the user. Although we make every good faith effort to ensureaccuracy, this document may include technical or other inaccuracies ortypographical errors. Klee Associates reserves the right to make changeswithout prior notice.

    ERPtips is a valued resource for thousands of clients and consultants worldwide.Visit our web site for more information about upcoming training, books, andnewsletters.

    www.ERPtips.com

    ERPtips is a registered trademark of Klee Associates, Inc.

    No Affiliation: Klee Associates, Inc. and this publication are not affiliated with orendorsed by SAP AG. SAP AG software referred to on this site is furnishedunder license agreements between SAP AG and its customers and can be usedonly within the terms of such agreements. SAP AG is a registered trademark ofSAP AG. All other product names used herein are trademarks or registeredtrademarks of their respective owners.

    http://www.erptips.com/http://www.erptips.com/http://www.erptips.com/