How to Create and Use an SAP Function Module for Order Details Retrieval in ABAP

 Let’s take real life scenario to develop a function module.

Requirement :-

  • Create a function module that takes Single Order Number as input.
  • Displays details of Order Number (Order Header + Order Item ).
  • Suppose below is my header and item table.
    • Header.



    • Item Table



Solution :-

  • Step 1 :- Go to SE37.

  • Step 2 :- Give a name and click on create button.



  • Step 3 :- Provide the function group and short description and click on save button.



  • Step 4 :- In import tab we will declare single input as a parameter.



  • Step 5 :- Let’s create a type structure which contains fields from both header and item table which will be given in export section.



  • Step 6 :- Create a table type for same structure.



  • Step 6 :- Pass the type structure and table type in export section of function module to create a internal table and work area.



  • Step 7 :- Open Source Code, Create a type structure for order header and order item table.



  • Step 8 :- Write select query to fetch data from header and item table.



  • Step 9 :- Fill data in the internal table which is mentioned in the export parameter.



  • Step 10 :- Activate the Function Module.

Source Code :-

***********************************
*Creating Type Strcuture
  TYPES : BEGIN OF ty_header,
            order_number TYPE zar_order_number,
            order_date   TYPE zar_order_date,
            payment_mode TYPE zar_payment_mode,
            currency     TYPE zar_curency,
          END OF ty_header.
  TYPES : BEGIN OF ty_item,
            order_number      TYPE zar_order_number,
            order_item_number TYPE zar_order_item_number,
            item_cost         TYPE zar_total_amount,
          END OF ty_item.
  DATA : lt_header TYPE TABLE OF ty_header,
         ls_header TYPE ty_header,
         lt_item   TYPE TABLE OF ty_item,
         ls_item   TYPE ty_item.
  DATA: ls_final TYPE zar_order.
***********************************************
*Writing Select Queries
  SELECT order_number order_date payment_mode
    currency FROM zar_header INTO TABLE
    lt_header WHERE order_number = p_order_number.
  IF lt_header IS NOT INITIAL.
    SELECT order_number order_item_number item_cost
      FROM zar_item INTO TABLE lt_item
      FOR ALL ENTRIES IN lt_header WHERE
      order_number = lt_header-order_number.

******************************
*Filing data into parameter of export section.
    LOOP AT lt_item INTO ls_item.
      ls_final-order_item_number = ls_item-order_item_number.
      ls_final-item_cost = ls_item-item_cost.

      READ TABLE lt_header INTO ls_header WITH KEY order_number = ls_item-order_number.
      IF sy-subrc EQ 0.
        ls_final-order_number = ls_header-order_number.
        ls_final-order_date = ls_header-order_date.
        ls_final-payment_mode = ls_header-payment_mode.
        ls_final-currency = ls_header-currency.
      ENDIF.
      APPEND ls_final TO lt_final.
      CLEAR : ls_final, ls_header, ls_item.
    ENDLOOP.
  ENDIF.

Execute the function module :-



  • let’s suppose, I give order number 1 as input.

  • Press F8.



  • We can see we got 3 inputs in LT_FINAL. Click on the entries to get the order details.




Calling the Above Function Module in Program :-

  • Step 1 :- Create a executable in program in ABAP Editor.

  • Step 2 :- Give a parameter to give order number as input.

  • Step 3 :- Declare internal table and work area for final table.



  • Step 4 :- Call the Function Module.



  • Step 5 :- Display the data.



    Source Code :-

    **********************************************
    *Start of Program
    ****************
    DATA : lt_final type ZAR_T_ORDER,
           ls_final TYPE ZAR_ORDER.
    PARAMETERS : p_ono type zar_order_number.
    *****************************************
    *Calling the Function Module
    
    CALL FUNCTION 'ZAR_ORDER_DETAILS'
      EXPORTING
        p_order_number       = p_ono
     IMPORTING
       LT_FINAL             = lt_final
              .
    
    *****************************************
    *Displaying the Output.
    WRITE : 'Order Details :'.
    LOOP at lt_final INTO ls_final.
      WRITE :/ ls_final-order_number,
               ls_final-order_date,
               ls_final-payment_mode,
               ls_final-currency,
               ls_final-order_item_number,
               ls_final-item_cost.
    ENDLOOP.
    
    *********
    *End of Program
    **************************************************
    

    Execute the program :-



    • Press F8.




Comments

Popular posts from this blog

Introduction to OData | Ui5 | Fiori

Introduction to SAP

ODATA topics necessary for SAP Ui5 / FIORI