Skip to Content
Personal Insights
Author's profile photo Raghavan Kathamuthu

ABAP Sample Report program with join condition and using cl_salv_table class

Introduction:

Hope everyone is well. I am new to technical. In ABAP for self learning we need more sample program to get clarity and clarification to understand things better. In SAP blog the availability of sample program is less. So I am writing this.

Main Part

This program will fetch the material details and production order details.

Below is the program.

REPORT zprod.

TYPES: BEGIN OF ty_final,
  matnr TYPE mara-matnr,
  werks TYPE marc-werks,
  maktx TYPE makt-maktx,
  plnum TYPE plaf-plnum,
  gsmng TYPE plaf-gsmng,
  pedtr TYPE plaf-pedtr,
  aufnr TYPE afpo-aufnr,
  DGLTS TYPE afpo-DGLTS,
  psmng TYPE afpo-psmng,
  meins TYPE afpo-meins,
  bwart TYPE mseg-bwart,
  menge TYPE mseg-menge,
  budat_mkpf TYPE mseg-budat_mkpf,
  END OF ty_final.

data: it_final type STANDARD TABLE OF ty_final,
      wa_final type ty_final.
  DATA: smatnr TYPE mara-matnr,
        swerks TYPE marc-werks.

  SELECTION-SCREEN BEGIN OF BLOCK a.
    SELECT-OPTIONS: s_matnr FOR smatnr,
    s_werks FOR swerks.

  SELECTION-SCREEN END OF BLOCK a.

  START-OF-SELECTION.
  select a~matnr b~werks c~maktx d~plnum d~gsmng d~pedtr e~aufnr e~DGLTS
    e~psmng e~meins f~bwart f~menge f~meins f~budat_mkpf 
    into CORRESPONDING FIELDS OF table it_final
    from mara as a
    INNER JOIN marc as b on a~matnr = b~matnr
    INNER JOIN makt as c on a~matnr = c~matnr
    INNER JOIN plaf as d on a~matnr = d~matnr and b~werks = d~plwrk
    INNER JOIN afpo as e on a~matnr = e~matnr and b~werks = e~pwerk
    INNER JOIN mseg as f on a~matnr = f~matnr and b~werks = f~werks and bwart = '101'
    where a~matnr in s_matnr
    and b~werks in s_werks.
  END-OF-SELECTION.

  sort it_final by matnr plnum ASCENDING.
  delete ADJACENT DUPLICATES FROM it_final COMPARING plnum.

  DATA: lr_alv          TYPE REF TO cl_salv_table.
    TRY.
        CALL METHOD cl_salv_table=>factory
          EXPORTING
            list_display = if_salv_c_bool_sap=>false
          IMPORTING
            r_salv_table = lr_alv
          CHANGING
            t_table      = it_final.
        ##NO_HANDLER.
      CATCH cx_salv_msg .
    ENDTRY.
    CALL METHOD lr_alv->display.

 

Output is below.

Conclusion:

Hope this may help for beginners.

Please refer the inks for more https://blogs.sap.com/2020/12/13/reports-with-cl_salv_table/

Assigned Tags

      5 Comments
      You must be Logged on to comment or reply to a post.
      Author's profile photo Ron Mitton
      Ron Mitton

      Hi Raghavan,

      Thanks for doing this, it is a good idea.  Personally, I hate one character table aliases so I rewrote your code without them in an attempt to stop this practice.

      REPORT zprod.
      TABLES: mara,marc.
      
      TYPES:
        BEGIN OF production_order_line_t,
          material_number                TYPE mara-matnr,
          plant                          TYPE marc-werks,
          material_description           TYPE makt-maktx,
          planned_order_number           TYPE plaf-plnum,
          total_planned_order_quantity   TYPE plaf-gsmng,
          planned_order_finish_date      TYPE plaf-pedtr,
          production_order_number        TYPE afpo-aufnr,
          production_order_sched_finish  TYPE afpo-dglts,
          production_order_item_quantity TYPE afpo-psmng,
          prod_order_unit_of_measure     TYPE afpo-meins,
          movement_type                  TYPE mseg-bwart,
          document_quantity              TYPE mseg-menge,
          document_unit_of_measure       TYPE mseg-meins,
          document_posting_date          TYPE mseg-budat_mkpf,
        END OF production_order_line_t.
      
      DATA: production_orders TYPE STANDARD TABLE OF production_order_line_t.
      
      SELECTION-SCREEN BEGIN OF BLOCK a.
        SELECT-OPTIONS s_matnr FOR mara-matnr.
        SELECT-OPTIONS s_werks FOR marc-werks.
      SELECTION-SCREEN END OF BLOCK a.
      
      START-OF-SELECTION.
        SELECT
          FROM mara
          JOIN marc
            ON marc~matnr = mara~matnr
          LEFT OUTER JOIN makt
            ON makt~matnr = mara~matnr
           AND makt~spras = @sy-langu
          JOIN plaf
            ON plaf~matnr = mara~matnr
           AND plaf~plwrk = marc~werks
          JOIN afpo
            ON afpo~matnr = marc~matnr
           AND afpo~pwerk = marc~werks
          JOIN mseg
            ON mseg~matnr = marc~matnr
           AND mseg~werks = marc~werks
           AND mseg~bwart = '101'
        FIELDS mara~matnr, marc~werks, makt~maktx,
               plaf~plnum, plaf~gsmng, plaf~pedtr,
               afpo~aufnr, afpo~dglts, afpo~psmng, afpo~meins,
               mseg~bwart, mseg~menge, mseg~meins, mseg~budat_mkpf
          WHERE mara~matnr IN @s_matnr
            AND marc~werks IN @s_werks
          INTO TABLE @production_orders.
      
      END-OF-SELECTION.
      
        SORT production_orders BY material_number planned_order_number ASCENDING.
        DELETE ADJACENT DUPLICATES FROM production_orders COMPARING planned_order_number.
      
        DATA alv_table TYPE REF TO cl_salv_table.
        TRY.
            CALL METHOD cl_salv_table=>factory
              EXPORTING
                list_display = if_salv_c_bool_sap=>false
              IMPORTING
                r_salv_table = alv_table
              CHANGING
                t_table      = production_orders.
            ##NO_HANDLER.
          CATCH cx_salv_msg .
        ENDTRY.
        CALL METHOD alv_table->display( ).

       

       

      Author's profile photo Matthew Billingham
      Matthew Billingham

      Yep. Using the full name of the table is much clearer. I have sometimes done things like a for mara and c for marc. But just alphabetically assigning aliases detracts from comprehension.

      Author's profile photo Raghavan Kathamuthu
      Raghavan Kathamuthu
      Blog Post Author

      Thanks a lot for sharing your knowledge. Much Helpfull

      Author's profile photo Matthew Billingham
      Matthew Billingham

      We're all here to learn. Glad to pass some of our experience.

      Here's something to think about. You defined

      lr_alv

      But you've used an l prefix. And it isn't local. It's a global variable within your program. If you read the style guides and the clean code git repository, you'll also find that lr_ isn't necessary either.

      Author's profile photo Ulrich Schmidt
      Ulrich Schmidt

      Hi Rhagavan,

      I think that the tag "ABAP Connectivity" is incorrectly assigned here. ABAP Connectivity is about HTTP and RFC communication, mainly with programs written in a non-ABAP language (like C/C++, Java, .NET, but also between two ABAP systems). I see none of that being used here. So should we remove it?

      Best Regards, Ulrich