SAP AND ABAP TIPS AND FACTS

Wednesday, March 19, 2008

Examining Function Modules in SAP ABAP

Examining Function Modules in SAP ABAP


Prior to creating a new function module, you need to create a function group. Some examples of function groups are given below.

















Add one more function module to the same Function Group, goto SE80 and see the result.








Function
Group
Description
V02D Customer Master: Read/Block
VBAK Business Object Sales
Document
ITOB_BAPI_EQ PM Equipment: BAPIs
STXD SAPscript program interface
1




Take time to go through each of the function module components. We will examine each of them in this post.

Required transactions are as folows.















Transaction Description
SE37 Function Builder Initial
Screen
SE80 Object Navigator


Run transaction SE37, and in the resulting screen click on the attributes TAB as shown below.



Now you can run transaction SE80, to check the Function Modules, Subroutines and Includes associated with this group. This way you can find out related function modules. For example if a particular function module does not satisfy your requirement, then you can search for a related function module in the same group.

See the figure below.



Now you know how to identify similar function modules in SAP ABAP.

Now Let us see the components that the SAP system created when a function group is created.Create a new function group from transaction SE80 and see all the components associated with it. See the figure below.




As you can see two includes have been created as follows.

LZTEMPTOP
LZTEMPUXX

In the TOP include, LZTEMPTOP you can place all the global data definations, these will be global to all the function modules in this group.

The second include program namely LZTEMPUXX should not be modified. The SAP R/3 system automatically places all the includes that you create in this include.
When you double click on this include just after creating the Function Group you see the following message in the left pane.

*****************************************************************
*   THIS FILE IS GENERATED BY THE FUNCTION LIBRARY.                           *
*   NEVER CHANGE IT MANUALLY, PLEASE!                                                *
*****************************************************************







Important:
Every time a function module is added to a Function Group, SAP R/3 will
add a new statement to the UXX. The include for the first function module
will be 01, the second 02 and so on




Create one more function module, add it to the same group, goto SE80 and see the result, you will notice that the system has generated one more Include named LZTEMPU02. In this case TEMP is specific to the names given for this example.

SAP ABAP Tutorial for creating an RFC (Remote enabled Function Module)

SAP ABAP Tutorial for creating an RFC (Remote enabled Function Module)

Task: To create an RFC  for creating a Sales order in SAP.

In the earlier posts we have already seen how to use a BAPI to create a SALES order in SAP ABAP. Today we will create an RFC to post a sales order.


Note: You can use the following BAPIs and Function modules to create a Sales Order in SAP.


BAPI_SALESORDER_CREATEFROMDAT1 Sales Order: Create Sales Order
BAPI_SALESORDER_CREATEFROMDAT1 Sales Order: Create Sales Order
SD_SALESDOCUMENT_CREATE

Other BAPIs related to Sales Order are as follows.

BAPISDORDER_GETDETAILEDLIST Sales Order: List of All Order Data
BAPI_ORDER_CHANGE_STATUS_GET Change status for order
BAPI_SALESORDER_CHANGE Sales Order: Change Sales Order
BAPI_SALESORDER_GETLIST Sales order: List of all orders for customer
BAPI_SALESORDER_GETSTATUS Sales order: Display status
BAPI_SALESORDER_SIMULATE Sales Order: Simulate Sales Ord


Transactions used in this Tutorial.

SE37            Function Builder
SE38            ABAP Editor
SE80            Object Navigator
SHDB           BDC Recorder


First we will do a BDC recording using transaction SHDB. To see details on how to do a BDC recording please click here.

In this case you need to use the transaction VA01 to do the recording.

We will do the recording for the following fields.

Order Type



Sold-to Party
Ship-to Party
Purch Ord No



Material
Quantity



In the function module that we create these will be the Import Parameters.

Note: you need to do the BDC recording for all the mandatory fields for creating a Sales Order.


Once the BDC recording is complete for Transaction VA01 as shown in the previous post (Refer this)

We need to create a program for this recording (Refer this).

Once the above two tasks are complete we need to do some clean up as we have to transfer this program to a function module. Please refer to the following program.

**********************************************************************************************************************************

report ZSALESORDER
       no standard page heading line-size 255.


DATA:   BDCDATA LIKE BDCDATA    OCCURS 0 WITH HEADER LINE,
        CTUMODE LIKE CTU_PARAMS-DISMODE VALUE 'A',
        CUPDATE LIKE CTU_PARAMS-UPDMODE VALUE 'L',
        MESSTAB LIKE BDCMSGCOLL OCCURS 0 WITH HEADER LINE,
        L_SUBRC LIKE SY-SUBRC,
        L_MSTRING(480),
        NODATA VALUE '/'.



*include bdcrecx.

start-of-selection.


perform bdc_dynpro      using 'SAPMV45A' '0101'.
perform bdc_field       using 'BDC_CURSOR'
                              'VBAK-SPART'.
perform bdc_field       using 'BDC_OKCODE'
                              '/00'.
perform bdc_field       using 'VBAK-AUART'
                              'OR'.
perform bdc_field       using 'VBAK-VKORG'
                              ''.
perform bdc_field       using 'VBAK-VTWEG'
                              ''.
perform bdc_field       using 'VBAK-SPART'
                              ''.
perform bdc_dynpro      using 'SAPMV45A' '4001'.
perform bdc_field       using 'BDC_OKCODE'
                              '/00'.
perform bdc_field       using 'VBKD-BSTKD'
                              '15493'.
perform bdc_field       using 'KUAGV-KUNNR'
                              '7777'.
perform bdc_field       using 'KUWEV-KUNNR'
                              '7777'.
perform bdc_field       using 'RV45A-KETDAT'
                              '04/01/2008'.
perform bdc_field       using 'RV45A-KPRGBZ'
                              'D'.
perform bdc_field       using 'VBKD-PRSDT'
                              '03/19/2008'.
perform bdc_field       using 'BDC_CURSOR'
                              'RV45A-KWMENG(01)'.
perform bdc_field       using 'RV45A-MABNR(01)'
                              'y-351'.
perform bdc_field       using 'RV45A-KWMENG(01)'
                              '                  1'.
perform bdc_dynpro      using 'SAPMV45A' '4001'.
perform bdc_field       using 'BDC_OKCODE'
                              '=SICH'.
perform bdc_field       using 'VBKD-BSTKD'
                              '15493'.
perform bdc_field       using 'KUAGV-KUNNR'
                              '7777'.
perform bdc_field       using 'KUWEV-KUNNR'
                              '7777'.
perform bdc_field       using 'RV45A-KETDAT'
                              '04/01/2008'.
perform bdc_field       using 'RV45A-KPRGBZ'
                              'D'.
perform bdc_field       using 'VBKD-PRSDT'
                              '03/19/2008'.
perform bdc_field       using 'VBKD-ZTERM'
                              'ZB01'.
perform bdc_field       using 'VBKD-INCO1'
                              'CIF'.
perform bdc_field       using 'VBKD-INCO2'
                              'Düsseldorf'.
perform bdc_field       using 'BDC_CURSOR'
                              'RV45A-MABNR(02)'.
perform bdc_dynpro      using 'SAPLSPO2' '0101'.
perform bdc_field       using 'BDC_OKCODE'
                              '=OPT1'.
perform bdc_transaction using 'VA01'.


Form bdc_transaction using tcode.

    CALL TRANSACTION TCODE USING BDCDATA
                     MODE   CTUMODE
                     UPDATE CUPDATE
                     MESSAGES INTO MESSTAB.
    L_SUBRC = SY-SUBRC.

      WRITE: / 'CALL_TRANSACTION',
               TCODE,
               'returncode:'(I05),
               L_SUBRC,
               'RECORD:',
               SY-INDEX.
      LOOP AT MESSTAB.

      ENDLOOP.

 endform.
*----------------------------------------------------------------------

*        Start new screen

*----------------------------------------------------------------------

FORM BDC_DYNPRO USING PROGRAM DYNPRO.
  CLEAR BDCDATA.
  BDCDATA-PROGRAM  = PROGRAM.
  BDCDATA-DYNPRO   = DYNPRO.
  BDCDATA-DYNBEGIN = 'X'.
  APPEND BDCDATA.
ENDFORM.

*----------------------------------------------------------------------

*        Insert field

*----------------------------------------------------------------------

FORM BDC_FIELD USING FNAM FVAL.
  IF FVAL <> NODATA.
    CLEAR BDCDATA.
    BDCDATA-FNAM = FNAM.
    BDCDATA-FVAL = FVAL.
    APPEND BDCDATA.
  ENDIF.
ENDFORM.

**********************************************************************************************************************************
Please make a note that the above program has been modified from the program that was generated using transaction SHDB. The include BDCRECX has been commented out and the code has been inserted in the main program. We will do further modifications to this program while inserting this code in the Remote Enabled Function Module RFC.

We will now create a Function Module and make it Remotely Enabled. How to create a function module see here.

Before creating a function module you need to create a function group see here.

Once the structure of the function module is created we need to create the Import Parameters. The import parameters we vary from system to system and will depend on the fields that you need to transfer to SAP R/3. Ideally there will be Import Parameters, Tables and Export Parameters.

Please see the figure below.

IMPORTANT: Note that the Pass Value filed has been selected. This filed has to be selected for all RFCs.




Once the Import Parameters are set you can create the export parameters.

Here we will be exporting the Sales Order Number.

IMPORTANT: Note that the Pass Value filed has been selected. This filed has to be selected for all RFCs.




Now we need to create an Include program as shown below.

Put the following code in a separate include program. This include program will be inserted in one of the includes of the function module. Namely LZSALESTOP. Please note that this name fill differ in your system. But the last three letters of the include will be TOP.

Also make a note of the following.

The include ending with UXX (in our case LZSALESUXX) should not be modified. SAP generates the following code in this include.

*****************************************************************
*   THIS FILE IS GENERATED BY THE FUNCTION LIBRARY.                           *
*   NEVER CHANGE IT MANUALLY, PLEASE!                                                *
*****************************************************************




Put the following code in a separate include program. This include program will be inserted in one of the includes of the function module. Namely LZSALESTOP. Please note that this name fill differ in your system. But the last three letters of the include will be TOP.

INCLUDE LZSALESU01.

***INCLUDE ZSALESCREATEI .

DATA:   BDCDATA LIKE BDCDATA    OCCURS 0 WITH HEADER LINE,
        CTUMODE LIKE CTU_PARAMS-DISMODE VALUE 'A',
        CUPDATE LIKE CTU_PARAMS-UPDMODE VALUE 'L',
        MESSTAB LIKE BDCMSGCOLL OCCURS 0 WITH HEADER LINE,
        L_SUBRC LIKE SY-SUBRC,
        L_MSTRING(480),
        NODATA VALUE '/'.



Form bdc_transaction using tcode.

    CALL TRANSACTION TCODE USING BDCDATA
                     MODE   CTUMODE
                     UPDATE CUPDATE
                     MESSAGES INTO MESSTAB.
    L_SUBRC = SY-SUBRC.

      WRITE: / 'CALL_TRANSACTION',
               TCODE,
               'returncode:'(I05),
               L_SUBRC,
               'RECORD:',
               SY-INDEX.

 endform.
*----------------------------------------------------------------------

*        Start new screen

*----------------------------------------------------------------------

FORM BDC_DYNPRO USING PROGRAM DYNPRO.
  CLEAR BDCDATA.
  BDCDATA-PROGRAM  = PROGRAM.
  BDCDATA-DYNPRO   = DYNPRO.
  BDCDATA-DYNBEGIN = 'X'.
  APPEND BDCDATA.
ENDFORM.

*----------------------------------------------------------------------

*        Insert field

*----------------------------------------------------------------------

FORM BDC_FIELD USING FNAM FVAL.
  IF FVAL <> NODATA.
    CLEAR BDCDATA.
    BDCDATA-FNAM = FNAM.
    BDCDATA-FVAL = FVAL.
    APPEND BDCDATA.
  ENDIF.
ENDFORM.

Now the main code has to be inserted in the body of the function module. The code shown below needs to be inserted in the body of the function module.

Please note that in the code given below the hard coded values are replaced by IMPORT Parameters of the function module.


FUNCTION Z_SALES_CREATE.
*"----------------------------------------------------------------------
*"*"Local interface:
*"  IMPORTING
*"     VALUE(ORDTYP) LIKE  VBAK-AUART
*"     VALUE(SOLDTO) LIKE  KNA1-KUNNR
*"     VALUE(SHIPTTO) LIKE  KNA1-KUNNR
*"     VALUE(PURCHORDNO) LIKE  VBKD-BSTKD
*"     VALUE(MATNO) LIKE  MARA-MATNR
*"     VALUE(QTY) LIKE  RV45A-KWMENG
*"  EXPORTING
*"     VALUE(SALESORDNO) LIKE  VBAK-VBELN
*"----------------------------------------------------------------------

perform bdc_dynpro      using 'SAPMV45A' '0101'.
perform bdc_field       using 'BDC_CURSOR'
                              'VBAK-SPART'.
perform bdc_field       using 'BDC_OKCODE'
                              '/00'.
perform bdc_field       using 'VBAK-AUART'
                              ORDTYP.
perform bdc_field       using 'VBAK-VKORG'
                              ''.
perform bdc_field       using 'VBAK-VTWEG'
                              ''.
perform bdc_field       using 'VBAK-SPART'
                              ''.
perform bdc_dynpro      using 'SAPMV45A' '4001'.
perform bdc_field       using 'BDC_OKCODE'
                              '/00'.
perform bdc_field       using 'VBKD-BSTKD'
                              PURCHORDNO.
perform bdc_field       using 'KUAGV-KUNNR'
                              SOLDTO.
perform bdc_field       using 'KUWEV-KUNNR'
                              SHIPTTO.
perform bdc_field       using 'RV45A-KETDAT'
                              '04/01/2008'.
perform bdc_field       using 'RV45A-KPRGBZ'
                              'D'.
perform bdc_field       using 'VBKD-PRSDT'
                              '03/19/2008'.
perform bdc_field       using 'BDC_CURSOR'
                              'RV45A-KWMENG(01)'.
perform bdc_field       using 'RV45A-MABNR(01)'
                              MATNO.
perform bdc_field       using 'RV45A-KWMENG(01)'
                              QTY.
perform bdc_dynpro      using 'SAPMV45A' '4001'.
perform bdc_field       using 'BDC_OKCODE'
                              '=SICH'.
perform bdc_transaction using 'VA01'.

GET PARAMETER ID 'AUN' FIELD SALESORDNO.
ENDFUNCTION.


Note that the remotely enabled and Start Immediately radio buttons are selected as shown below.



Please add the following line once the Call Transaction is completed.

GET PARAMETER ID 'AUN' FIELD SALESORDNO.

The above command will get the newly created Sales Order in the filed SALESORDNO. Alternatively you can follow the procedure given below using the USEREXIT.

GET PARAMETER ID 'AUN' FIELD SALESORDNO. Is a simpler way of getting the latest sales order number.

For more details on GET PARAMETER ID and SET PARAMETER ID please see the following.

SAP SPA/GPA Parameters


Important. The Sales Order number should be obtained in the EXPORT parameters of the function module. This is not handled in the code given below. But I will mention the user-exit from where the Sales Order number needs to be exported to this function module. We will discuss IMPORTING and EXPORTING from SAP Memory in the next Post.

User Exit Program Name: MV45AFZZ

        FORM USEREXIT_SAVE_DOCUMENT.                                          
                                                                              
        * Example:                                                            
        * CALL FUNCTION 'ZZ_EXAMPLE'                                          
        *      IN UPDATE TASK                                                 
        *      EXPORTING                                                      
        *           ZZTAB = ZZTAB.                                            
        LOOP AT XVBEP WHERE UPDKZ NE UPDKZ_DELETE                             
                        AND NOT AUFNR IS INITIAL.                             
          SET PARAMETER ID 'ANR' FIELD XVBEP-AUFNR.                           
        ENDLOOP.                                                              
                                                                                                                                                     
                                                                              
        ENDFORM.  

The Sales order number will be generated at the above point and can be exported to SAP Memory which can then be imported back into the function module.

ABAP TIPS

PREVIOUS                  NEXT                    RANDOM

 ABAP TIPS


Since your web browser does not support JavaScript, here is a non-JavaScript version of the image slideshow:

 ABAP TIPS
Always specify your conditions in the Where-clause instead of checking them yourself with check statements. The database system can then use an index (if possible) and the network load is considerably less.


 ABAP TIPS
For all frequently used Select statements, try to use an index. You always use an index if you specify (a generic part of) the index fields concatenated with logical Ands in the Select statement's Where clause. Note that complex Where clauses are poison for the statement optimizer in any database system.


 ABAP TIPS
If there exists at least one row of a database table or view with a certain condition, use the Select Single statement instead of a Select-Endselect-loop. Select Single requires one communication with the database system, whereas Select-Endselect needs two.


 ABAP TIPS
It is always faster to use the Into Table version of a Select statement than to use Append statements.


 ABAP TIPS
To read data from several logically connected tables use a join instead of nested Select statements. Network load is considerably less.


 ABAP TIPS
If you want to find the maximum, minimum, sum and average value or the count of a database column, use a select list with aggregate functions instead of computing the aggregates yourself. Network load is considerably less.


 ABAP TIPS
If you process your data only once, use a Select-Endselect-loop instead of collecting data in an internal table with Select Into Table. Internal table handling takes up much more space.


 ABAP TIPS
Use a select list or a view instead of Select * , if you are only interested in specific columns of the table. Network load is considerably less.


 ABAP TIPS
For all frequently used, read-only tables, try to use SAP buffering. Network load is considerably less.


 ABAP TIPS
Whenever possible, use array operations instead of single-row operations to modify your database tables. Frequent communication between the application program and database system produces considerable overhead.


 ABAP TIPS
Whenever possible, use column updates instead of single-row updates to update your database tables. Network load is considerably less.


 ABAP TIPS
Instead of using nested Select loops or FOR ALL ENTRIES it is often possible to use subqueries. Network load is considerably less.


 ABAP TIPS
Use the special operators CO, CA, CS, instead of programming the operations yourself. If ABAP/4 statements are executed per character on long strings, CPU consumption can rise substantially.


 ABAP TIPS
Some function modules for string manipulation have become obsolete and should be replaced by ABAP/4 statements or functions: STRING_CONCATENATE... -> CONCATENATE, STRING_SPLIT... -> SPLIT, STRING_LENGTH -> strlen(), STRING_CENTER -> WRITE...TO...CENTERED, STRING_MOVE_RIGHT -> WRITE...TO...RIGHT-JUSTIFIED


 ABAP TIPS
Use the CONCATENATE statement instead of programming a string concatenation of your own.


 ABAP TIPS
If you want to delete the leading spaces in a string, use the ABAP/4 statement SHIFT...LEFT DELETING LEADING... .Other constructions (with CN and SHIFT...BY SY-FDPOS PLACES, with CONDENSE if possible, with CN and ASSIGN CLA+SY-FDPOS(LEN) ...) are not as fast. In any case, avoid using SHIFT inside a WHILE-loop!


 ABAP TIPS
Use the SPLIT statement instead of programming a string split yourself.


 ABAP TIPS
Use the strlen( ) function to restrict the DO loop to the relevant part of the field, e.g. when determinating a check-sum.


 ABAP TIPS
Use "CLEAR f WITH val" whenever you want to initialize a field with a value different from the field's type-specific initial value.


 ABAP TIPS
Try to keep the table ordered and use binary search or used a table of type SORTED TABLE. If TAB has n entries, linear search runs in O( n ) time, whereas binary search takes only O( log2( n ) ).


 ABAP TIPS
A dynamic key access is slower than a static one, since the key specification must be evaluated at runtime. However, for large tables the costs are dominated by number of comparison needed to locate the entry.


 ABAP TIPS
If you need to access an internal table with different keys repeatedly, keep your own secondary indices.With a secondary index, you can replace a linear search with a binary search plus an index access.


 ABAP TIPS
LOOP ... WHERE is faster than LOOP/CHECK because LOOP ... WHERE evaluates the specified condition internally. As with any logical expressions, the performance is better if the operands of a comparison share a common type. The performance can be further enhanced if LOOP ... WHERE is combined with FROM i1 and/or TO i2, if possible.


Always use Pretty Printer and Extended Program Check before releasing the code. Do not leave unused code in the program. Comment the code thoroughly. Align the comments and the Code. Follow the SAP Standards and SAP Best Practices guidelines. It’s a good practice to take a dump of the code on your local drive.

Ole Automation Part1
Ole Automation Part 2
Processing Blocks in ABAP
Simple ABAP Report
ALV Grid - Changing Colors
ALV Report Example
Creating Variants For ABAP Reports
Recording BDC using Transaction
Sales Document Flow in ABAP
User Exits in SAP SD
SAP ABAP Naming Standards
SAP SD Tables
SAP ABAP Data Dictionary Tables
MM Important Transaction Codes in SAP
Passing g Data From One ABAP Program to Another
ABAP Compute Add Collect and Append
SAP ABAP Determining Attributes of Data
SAP ABAP Editor Icons
BAPI for Displaying Material Data
BAPI to get customer bank details
EDI Outbound Process
SAP EDI Process Overview
Function Module for Vendor Bank details
SAP IDOC
Creating a Valid Password in SAP
SAP BADIs Introduction
SAP ABAP MACROS
POP UP function Module to Confirm and Save Data
Select Options
BAPI for availability check
String to Numerical
SAP Goods Movement Process
Getting a List of Plants for a Material
SAP R3 Clients Concept
ABAP Adobe Forms
Authorization Object Tables
SAP Industry Specific Solutions

Sap Scripts and SmartForms Bar Codes
Standard Reports and Sap Scripts
Important Standard Reports in SAP
Abap Tricks and Tips
Bapi Sales Order
BAPI Purchase Order
Creating Function Modules in SAP
Creating Tables in SAP
Finding User Exits in SAP
Function Module Create Text and Read Text
Important Transaction Codes in SAP
ABAP Function Module for Submitting a Program
ABAP Game Tic Tac Toe
ABAP Internal Table To Excel Sheet
ABAP Function Module to create Directory
Different Types of Menus in SAP
Function Modules in SAP to check Loged in Users
ABAP Function Module for Adding Days to Dates
Call a Transaction From a Remote System
SAP MM simple Procurement Cycle
BAPI Material EDIT
Finding Decimal Places in Currency
Getting negative sign before a number in ABAP
Program Editor Lock Unlock
Restricting Select Options
List of BAPIs in the system
SAP Function Module Scramble a String
LSMW
POP up table contents on the screen
SAP R3 Bookmarking Websites
Stock Requirements List Function Module
Retail Transaction Codes
ABAP Debugger Break Points
ABAP Debugger WatchPoints
Drill Down Reports Concept
Creating a HOT SPOT
Interactive Programs and Hide Technique
String Concatenate
Get Week of the Year
SAP ABAP to Add Days to a Date
Add Months to a Date
Get Month in the Year
Display Clock

ABAP Code For Progress BAR
ABAP Function Module For Caluclator
ABAP Function Module For Calender
Displaying Messages in ABAP
Function Module Pop Up To Confirm
Conversion Routines in SAP
SAP ABAP Authorization
SAP ABAP Module Pool Tutorial
SAP ABAP RFC

Finding Path to SAP Transaction in Menu
SAP Purchasing Documents
SAP and ABAP Shortcuts
Logical Databases
Advantages of Logical Databases
Copy to Clipboard
BAPI Create Material
Finding and Running Programs in ABAP
Program Syntax Check and Extended Syntax Check
Select Options upper lower case
BAPI Sales Order Simulate
Get PLANT and Description for a Material
MRP List Function Module
Production Planning and Controlling
Applications in SAP R3
Tool Based Reports
Important Transaction Codes in SAP
SAP Stock per Bin
Pop Up a Calender
Module to Read a File
Module to Reverse A String
Run an Executable Program with Parameters
Program for POP up Screen
Printing Selection Parameters for a Report
Uploading and DownLoading a Report
SAP ABAP Version Management
SAP ABAP Short Cuts
List of Important System Variables

ABAP MACROS
ABAP Calling a File Selector
Some Important Function Modules
ABAP String Operations

ABAP Function Module to Check Validity of Date
Transfer Internal Table Contents to a File

SAP ABAP Program Types
BAPI to Read Customer Data
Checking Validity of Date
Download to Application Server
ABAP Debugger Breakpoint and Watchpoint
BAPI to get Company Code Details
Creating Material Using BAPI part 2
Generating a Valid Password
Logical Databases Structure
Making Fields OBligatory in Selection Screen
ABAP Views
Getting a Company Code for a Plant
Importing contents of Clipboard in SAP
Getting a Plant for a Material
Plant Material and Storage Location
SAP Production Planning Standard Reports
NetWeaver Components
Supported Databases and Operating Systems