Tuesday, March 19, 2013

Internal Table as Parameter of Method

We can have an internal table as a parameter of a method. This case we have to Export the internal table at the declaration time of the method. And we shall import the internal table at the time of calling of the method.

In the following program we are showing item wise header information of Purchase order in two different displays. The output will contain the header information at top and item information at below of the same screen. We are getting the Purchase Order number from the input parameter.

REPORT  zsr_test NO STANDARD PAGE HEADING.

TABLES: ekko.
TYPES: BEGIN OF ty_ekko,
        ebeln TYPE ekko-ebeln,
        lifnr TYPE ekko-lifnr,
       END OF ty_ekko,

       BEGIN OF ty_ekpo,
         ebeln TYPE ekpo-ebeln,
         ebelp TYPE ekpo-ebelp,
         matnr TYPE ekpo-matnr,
         werks TYPE ekpo-werks,
         lgort TYPE ekpo-lgort,
         mtart TYPE ekpo-mtart,
       END OF ty_ekpo.

DATA: wa_ekko TYPE ty_ekko,
      wa_ekpo TYPE ty_ekpo,
      it_ekko TYPE STANDARD TABLE OF ty_ekko,
      it_ekpo TYPE STANDARD TABLE OF ty_ekpo.

INITIALIZATION.
  PARAMETERS p_ebeln TYPE ekko-ebeln.

*----------------------------------------------------------------------*
*       CLASS cls
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS cls DEFINITION.

  PUBLIC SECTION.
    METHODS: m_ekko IMPORTING po TYPE ekko-ebeln
                    EXPORTING lt TYPE ANY TABLE,
             m_ekpo.

ENDCLASS.                    "cls

*----------------------------------------------------------------------*
*       CLASS cls IMPLEMENTATION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS cls IMPLEMENTATION.

  METHOD m_ekko.
    SELECT ebeln lifnr
      FROM ekko INTO TABLE lt
      WHERE ebeln = po.

    IF sy-subrc = 0.
      LOOP AT lt INTO wa_ekko.
        AT FIRST.
          WRITE: /  'Purchase Order Header:'.
          WRITE: /  'PO No.',
                 12 'Vendor',
                 / '-----------------'.
          SKIP.
        ENDAT.

        WRITE: /  wa_ekko-ebeln,
               12 wa_ekko-lifnr.
        CLEAR wa_ekko.
      ENDLOOP.
      WRITE: / '-----------------'.
      SKIP 2.
    ENDIF.
  ENDMETHOD.                    "m_ekko

  METHOD m_ekpo.
    IF it_ekko IS NOT INITIAL.
      SELECT ebeln ebelp matnr werks lgort mtart
        FROM ekpo INTO TABLE it_ekpo
        FOR ALL ENTRIES IN it_ekko
        WHERE ebeln = it_ekko-ebeln.

      IF sy-subrc = 0.
        LOOP AT it_ekpo INTO wa_ekpo.
          AT FIRST.
            WRITE: /  'Purchase Order Item:'.
            WRITE: /  'PO No.',
                   12 'Item',
                   20 'Material',
                   40 'Plant',
                   48 'Storage',
                   57 'Type'.
            ULINE.
            SKIP.
          ENDAT.

          WRITE: /  wa_ekpo-ebeln,
                 12 wa_ekpo-ebelp,
                 20 wa_ekpo-matnr,
                 40 wa_ekpo-werks,
                 48 wa_ekpo-lgort,
                 57 wa_ekpo-mtart.
          CLEAR wa_ekpo.
        ENDLOOP.
        ULINE.
      ENDIF.
    ENDIF.
  ENDMETHOD.                    "m_ekpo

ENDCLASS.                    "cls IMPLEMENTATION

START-OF-SELECTION.

  DATA obj TYPE REF TO cls.
  CREATE OBJECT obj.
  CALL METHOD: obj->m_ekko EXPORTING po = p_ebeln
                           IMPORTING lt = it_ekko,
               obj->m_ekpo.

The selection is based on the parameter of a purchase order.


The output is as follows.


Now we can achieve the same thing by using Selection range (select option). In the following program we have illustrated this.

REPORT  zsr_test NO STANDARD PAGE HEADING.

TABLES: ekko.
TYPES: BEGIN OF ty_ekko,
        ebeln TYPE ekko-ebeln,
        lifnr TYPE ekko-lifnr,
       END OF ty_ekko,

       BEGIN OF ty_ekpo,
         ebeln TYPE ekpo-ebeln,
         ebelp TYPE ekpo-ebelp,
         matnr TYPE ekpo-matnr,
         werks TYPE ekpo-werks,
         lgort TYPE ekpo-lgort,
         mtart TYPE ekpo-mtart,
       END OF ty_ekpo.

DATA: wa_ekko TYPE ty_ekko,
      wa_ekpo TYPE ty_ekpo,
      it_ekko TYPE STANDARD TABLE OF ty_ekko,
      it_ekpo TYPE STANDARD TABLE OF ty_ekpo.

INITIALIZATION.
  SELECT-OPTIONS s_ebeln FOR ekko-ebeln.

*----------------------------------------------------------------------*
*       CLASS cls
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS cls DEFINITION.

  PUBLIC SECTION.
    METHODS: m_ekko IMPORTING po LIKE s_ebeln[]
                    EXPORTING lt TYPE ANY TABLE,
             m_ekpo.

ENDCLASS.                    "cls

*----------------------------------------------------------------------*
*       CLASS cls IMPLEMENTATION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS cls IMPLEMENTATION.

  METHOD m_ekko.
    SELECT ebeln lifnr
      FROM ekko INTO TABLE lt
      WHERE ebeln IN po.

    IF sy-subrc = 0.
      LOOP AT lt INTO wa_ekko.
        AT FIRST.
          WRITE: /  'Purchase Order Header:'.
          WRITE: /  'PO No.',
                 12 'Vendor',
                 / '-----------------'.
          SKIP.
        ENDAT.

        WRITE: /  wa_ekko-ebeln,
               12 wa_ekko-lifnr.
        CLEAR wa_ekko.
      ENDLOOP.
      WRITE: / '-----------------'.
      SKIP 2.
    ENDIF.
  ENDMETHOD.                    "m_ekko

  METHOD m_ekpo.
    IF it_ekko IS NOT INITIAL.
      SELECT ebeln ebelp matnr werks lgort mtart
        FROM ekpo INTO TABLE it_ekpo
        FOR ALL ENTRIES IN it_ekko
        WHERE ebeln = it_ekko-ebeln.

      IF sy-subrc = 0.
        LOOP AT it_ekpo INTO wa_ekpo.
          AT FIRST.
            WRITE: /  'Purchase Order Item:'.
            WRITE: /  'PO No.',
                   12 'Item',
                   20 'Material',
                   40 'Plant',
                   48 'Storage',
                   57 'Type'.
            ULINE.
            SKIP.
          ENDAT.

          WRITE: /  wa_ekpo-ebeln,
                 12 wa_ekpo-ebelp,
                 20 wa_ekpo-matnr,
                 40 wa_ekpo-werks,
                 48 wa_ekpo-lgort,
                 57 wa_ekpo-mtart.
          CLEAR wa_ekpo.
        ENDLOOP.
        ULINE.
      ENDIF.
    ENDIF.
  ENDMETHOD.                    "m_ekpo

ENDCLASS.                    "cls IMPLEMENTATION

START-OF-SELECTION.

  DATA obj TYPE REF TO cls.
  CREATE OBJECT obj.
  CALL METHOD: obj->m_ekko EXPORTING po = s_ebeln[]
                           IMPORTING lt = it_ekko,
               obj->m_ekpo.

Here we are using the selection screen with selection range.


The output is as follows.

7 comments:

Unknown said...

Great! keep on posting ......


















Antonio said...

Great post; do you mentor jr abapers like myself? Thanks.

Anonymous said...

How to do the same with select-options as it is an table
but as i tried using type any table for select options but you can specify type any table only once for an method as importing parameter, as per my requirement i have 6 select-options...

Sandip Roy said...

Please check this page now. I have added the Select Option's coding for you. Since select option is an internal table with header line, any table type will not work here.

Now in this way you have to make the logic as per your requirement of 6 select options. Hope it will help you.

Anonymous said...

SAP Success Factors Real Time Hands on Training in Chennai...

Don't always Depend on Training Institute Alone and so please aware of Best Trainers too..

http://thecreatingexperts.com/sap-successfactors-training-in-chennai/

If You need a Best Trainer over SAP Success Factors Means??? Please ready for an DEMO From the Trainer MR.Karthick
CONTACT:8122241286

Both Classroom/Online Training is Available!!!!!!

Unknown said...



Best SAP Success Factors Training Institute in Chennai
http://thecreatingexperts.com/sap-successfactors-training-in-chennai/
Best SAP MM Training in Chennai
http://thecreatingexperts.com/sap-mm-training-in-chennai/
Best SAP SD Training in Chennai
http://thecreatingexperts.com/sap-sd-training-in-chennai/
http://thecreatingexperts.com/sap-hr-training-in-chennai/
Best SAP FICO Training in Chennai
http://thecreatingexperts.com/sap-fico-training-in-chennai/
Best SAP ABAP Training in Chennai
http://thecreatingexperts.com/sap-abap-training-in-chennai/
Best SAP BASIS Training in Chennai
http://thecreatingexperts.com/sap-basis-training-in-chennai/

If You need a Best Trainer in SAP Success Factors??? Then be ready for a DEMO From the Trainer MR.Karthick
CONTACT:8122241286
http://thecreatingexperts.com/sap-mm-training-in-chennai/

Both Classroom/Online Training is Available!!!!!!

Unknown said...

Really is very interesting, I saw your website and get more details..Nice work. Thanks regards, sap hr abap training