sap f11labz

Object Oriented Classic for Multiple Tables

Here is a program by which we can fetch the data from MARA table based on the Material Number selected in the Selection Screen. Then we can find out the Storage Location details from MARD table based on the Plant details from MARC table. These MARC and MARD table data will be selected FOR ALL ENTRIES IN selected MARA table.

*&---------------------------------------------------------------------*
*& Report  ZSR_TEST
*&
*&---------------------------------------------------------------------*
*&
*&
*&---------------------------------------------------------------------*

REPORT  zsr_test.

TABLES: mara, marc, mard.

INITIALIZATION.

SELECTION-SCREEN BEGIN OF BLOCK b1 WITH FRAME TITLE text-001.
SELECT-OPTIONS  s_matnr FOR mara-matnr OBLIGATORY.
SELECTION-SCREEN END OF BLOCK b1.
*----------------------------------------------------------------------*
*       CLASS cls1 DEFINITION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS cls1 DEFINITION.
PUBLIC SECTION.

TYPES: BEGIN OF ty_mara,
matnr TYPE mara-matnr,
ernam TYPE mara-ernam,
mtart TYPE mara-mtart,
END OF ty_mara,

BEGIN OF ty_marc,
matnr TYPE marc-matnr,
werks TYPE marc-werks,
END OF ty_marc,

BEGIN OF ty_mard,
matnr TYPE mard-matnr,
werks TYPE mard-werks,
lgort TYPE mard-lgort,
END OF ty_mard,

BEGIN OF ty_out1,
matnr TYPE mara-matnr,
ernam TYPE mara-ernam,
mtart TYPE mara-mtart,
werks TYPE mard-werks,
lgort TYPE mard-lgort,
END OF ty_out1.

DATA: wa_mara TYPE ty_mara,
it_mara TYPE STANDARD TABLE OF ty_mara,
wa_marc TYPE ty_marc,
it_marc TYPE STANDARD TABLE OF ty_marc,
wa_mard TYPE ty_mard,
it_mard TYPE STANDARD TABLE OF ty_mard,
wa_out1 TYPE ty_out1,
it_out1 TYPE STANDARD TABLE OF ty_out1.

METHODS: m_mara, m_marc, m_mard,
m_out1, m_display1.

PROTECTED SECTION.
PRIVATE SECTION.
ENDCLASS.                    "cls1 DEFINITION

*----------------------------------------------------------------------*
*       CLASS cls1 IMPLEMENTATION
*----------------------------------------------------------------------*
*
*----------------------------------------------------------------------*
CLASS cls1 IMPLEMENTATION.

METHOD m_mara.

SELECT matnr ernam mtart
FROM mara INTO TABLE it_mara
WHERE matnr IN s_matnr.

IF sy-subrc = 0.
SORT it_mara BY matnr.
ELSE.
MESSAGE 'No data found' TYPE 'I'.
ENDIF.

ENDMETHOD.                    "m_mara

METHOD m_marc.

IF it_mara IS NOT INITIAL.
SELECT matnr werks FROM marc
INTO TABLE it_marc
FOR ALL ENTRIES IN it_mara
WHERE matnr = it_mara-matnr.

IF sy-subrc = 0.
SORT it_marc BY matnr.
ENDIF.
ENDIF.

ENDMETHOD.                    "m_marc

METHOD m_mard.

IF it_mara IS NOT INITIAL.
SELECT matnr werks lgort
FROM mard INTO TABLE it_mard
FOR ALL ENTRIES IN it_mara
WHERE matnr = it_mara-matnr.

IF sy-subrc = 0.
SORT it_mard BY matnr.
ENDIF.
ENDIF.

ENDMETHOD.                    "m_mard

METHOD m_out1.

IF it_mara IS NOT INITIAL.
LOOP AT it_mara INTO wa_mara.
wa_out1-matnr = wa_mara-matnr.
wa_out1-ernam = wa_mara-ernam.
wa_out1-mtart = wa_mara-mtart.
READ TABLE it_marc INTO wa_marc
WITH KEY matnr = wa_mara-matnr BINARY SEARCH.
IF sy-subrc = 0.
wa_out1-werks = wa_marc-werks.
ENDIF.
READ TABLE it_mard INTO wa_mard
WITH KEY matnr = wa_mara-matnr BINARY SEARCH.
IF sy-subrc = 0.
wa_out1-lgort = wa_mard-lgort.
ENDIF.
APPEND wa_out1 TO it_out1.
CLEAR: wa_out1, wa_mara, wa_marc, wa_mard.
ENDLOOP.
ENDIF.

ENDMETHOD.                                                "m_out1

METHOD m_display1.

IF it_out1 IS NOT INITIAL.
LOOP AT it_out1 INTO wa_out1.
AT FIRST.
WRITE: /3 'MATERIAL',
21 'NAME',
36 'TYPE',
48 'PLANT',
56 'STORAGE LOCATION'.
ULINE.
SKIP.
ENDAT.
WRITE: /3 wa_out1-matnr,
21 wa_out1-ernam,
36 wa_out1-mtart,
48 wa_out1-werks,
56 wa_out1-lgort.
ENDLOOP.
CLEAR wa_out1.
ENDIF.

ENDMETHOD.                                                "m_display1

ENDCLASS.                    "cls1 IMPLEMENTATION

START-OF-SELECTION.
DATA: obj1 TYPE REF TO cls1.

CREATE OBJECT: obj1.

CALL METHOD: obj1->m_mara,
obj1->m_marc,
obj1->m_mard,
obj1->m_out1,
obj1->m_display1.

The Selection Screen:

The Output is:

Reproduced from Sandip Roy’s Blog
Sandip Roy

Sandip Roy

Sandip is SAP ABAP consultant at Ericsson. When He is not working he can be found taking snaps @ the Himlayas

More Posts - Website

Add a Comment

HTML Snippets Powered By : XYZScripts.com