Enhancing ABAP Programs: Leveraging Class Methods for Sales Order Display and Utilizing Multiple Tables

 

1. Calling Our Usual ABAP Class for Sales Order Display in Program :-

Requirement :-

  • We have created this a class in our previous part to display the Sales Order details.



  • Now, The Customer wants me to use this method from the program.


Solution :-

  • Step 1 :- Go to ABAP Editor ( SE38 ) and create a executable program.



  • Step 2 :- provide a parameter for input number.



  • Step 3 :- Define variable for the output purpose.



  • Step 4 :- Define object for our usual ABAP class and create the object.



  • Step 5 :- Call the method and provide the above defined output parameters and use write statement to display the output.




Code :-

*&---------------------------------------------------------------------*
*& Report ZAR_USUAL_ABAP_CLASS
*&---------------------------------------------------------------------*
*&
*&---------------------------------------------------------------------*
REPORT ZAR_USUAL_ABAP_CLASS.

DATA : perdat type erdat,
       perzet type erzet,
       pernam type ernam,
       pvbtyp type vbtypl.

PARAMETERS : p_vbeln type vbeln_va.

START-OF-SELECTION.

*&----------------------------------------------
*Creating Object for Usual ABAP Class
DATA(lo_object) = new ZAR_USUAL_ABAP_CLASS( ).

CALL METHOD zar_usual_abap_class=>display_sales_order_details
  EXPORTING
    p_vbeln     = p_vbeln
  IMPORTING
    p_erdat     = perdat
    p_erzet     = perzet
    p_ernam     = pernam
    p_vbtyp     = pvbtyp
  EXCEPTIONS
    wrong_input = 1
    others      = 2
        .

WRITE : perdat, perzet, pernam, pvbtyp.

*&-------------------------------------------------------------
*&End of Program
*&----------------------------------------------------------------------------

Execute the Program :-



  • Press F8 to execute the program.




2. Using Multiple Tables (VBAK, VBAP ) to display Sales Order Details :-

Requirement :-

  • Take Single Sales Document Number as input.
  • Write a method in the above used class.
  • Use multiple tables VBAK, VBAP to fetch the Sales Order details.

Solution :-

  • Step 1 :- Go to SE24 and open our class in change mode.



  • Step 2 :- Write the method name and provide the details.



  • Step 3 :- Provide the parameter one as input.



  • Step 4 :- Since, we are require to store data from two tables, therefore we will have to pass a table type as exporting parameter.

    • Create a type structure in SE11 for 4 fields from VBAK and 2 fields from VBAP.


    • now create the corresponding table type for the same.


  • Step 5 :- Now we can pass this as a parameter.



  • Step 6 :- Now write the corresponding logic in source code.

    • Create a type structure for both VBAK and VBAP table and corresponding internal table and work area.



    • Write the select query to fetch the corresponding data.



    • now define a work area for for the same structure which we used in parameters and fill that work area with the data stored in internal tables of header and item tables.




Code :-

  method GET_DATA.
    types : begin of ty_vbak,
      vbeln type vbeln_va,
      erdat type erdat,
      erzet type erzet,
      ernam type ernam,
      vbtyp type vbtypl,
     end of ty_vbak.

     types : begin of ty_vbap,
       vbeln type vbeln_va,
       posnr type posnr_va,
       matnr type matnr,
     end of ty_vbap.

     DATA : lt_vbak type table of ty_vbak,
            ls_vbak type ty_vbak,
            lt_vbap type table of ty_vbap,
            ls_vbap type ty_vbap,
            ls_final type ZAR_SALES_ORDER_DETAILS.

     select vbeln erdat erzet ernam vbtyp
       from vbak into table lt_vbak
       where vbeln = p_vbeln.

      if lt_vbak is not INITIAL.
        select vbeln posnr matnr
          from vbap
          into table lt_vbap
          FOR ALL ENTRIES IN lt_vbak
          where vbeln = lt_vbak-vbeln.

          loop at lt_vbap into ls_vbap.
            read table lt_vbak into ls_vbak with key vbeln = ls_vbap-vbeln.
            if sy-subrc eq 0.
              ls_final-vbeln = ls_vbak-vbeln.
              ls_final-erdat = ls_vbak-erdat.
              ls_final-erzet = ls_vbak-erzet.
              ls_final-ernam = ls_vbak-ernam.
              ls_final-vbtyp = ls_vbak-vbtyp.
            endif.

              ls_final-posnr = ls_vbap-posnr.
              ls_final-matnr = ls_vbap-matnr.

              append ls_final to lt_data.
          endloop.

      endif.
  endmethod.

Calling the Above Created Method in ABAP Editor :-

  • Step 1 :- Create a executable program in ABAP Editor.
  • Step 2 :- create a internal table of the above created table type.
  • Step 3 :- Create a parameter for the input.
  • Step 4 :- Create the object of the class.
  • Step 5 :- call the method of the class.
  • Step 6 :- Use factory method of CL_SALV_TABLE class to display the data.

Program :-

*&---------------------------------------------------------------------*
*& Report ZAR_USUAL_ABAP_CLASS
*&---------------------------------------------------------------------*
*&
*&---------------------------------------------------------------------*
REPORT ZAR_USUAL_ABAP_CLASS.

data : lt_final type table of ZAR_SALES_ORDER_DETAILS.

PARAMETERS : p_vbeln type vbeln_va.

START-OF-SELECTION.

*&----------------------------------------------
*Creating Object for Usual ABAP Class
DATA(lo_object) = new ZAR_USUAL_ABAP_CLASS( ).

CALL METHOD lo_object->get_data
  EXPORTING
    p_vbeln = p_vbeln
  IMPORTING
    lt_data = lt_final
    .

TRY.
CALL METHOD cl_salv_table=>factory
  EXPORTING
    list_display   = IF_SALV_C_BOOL_SAP=>FALSE
  IMPORTING
    r_salv_table   = data(lo_disp)
  CHANGING
    t_table        = lt_final
    .
  CATCH cx_salv_msg.
ENDTRY.

lo_disp->display( ).

*&-------------------------------------------------------------
*&End of Program
*&----------------------------------------------------------------------------

Execute :-



  • Execute the code


Comments

Popular posts from this blog

Understanding Different Types of SAP Function Modules: Normal, RFC, and Update

Mastering ABAP: A Step-by-Step Guide to Function Modules and Groups for Modularized Programming

Introduction to SAP