Abap Object Oriented Programming Example

This is an abap object oriented programming example for you reference.

By: Rakesh

/*
SPAN {
font-family: "Courier New";
font-size: 10pt;
color: #000000;
background: #FFFFFF;
}

.L1S31 {
font-style: italic;
color: #808080;
}

.L1S32 {
color: #3399FF;
}

.L1S33 {
color: #4DA619;
}

.L1S52 {
color: #0000FF;
}

*&---------------------------------------------------------------------*
*& Report  ZALV_Example_OOPS
*&---------------------------------------------------------------------*

report  zalv_example_oops.

* declaration of variables.
type-pools: slis.
tables: vbak,vbap.

*Global variables to be used for ALV Grid.

*--- ALV Grid instance reference
data gr_alvgrid type ref to cl_gui_alv_grid .

*--- Name of the custom control added on the screen
data gc_custom_control_name type scrfname value 'CUSTOM_CONTROL' .

*--- Custom container instance reference
data gr_ccontainer type ref to cl_gui_custom_container .

*--- Field catalog table
data gt_fieldcat type lvc_t_fcat .

*--- Layout structure
data gs_layout type lvc_s_layo .

*--- Flag to check validation - Initially set to S (Success)
data gv_valid_flag type c value 'S'.

*--- Flag to set the color of field VOREF - Initially set to F (False)
data gv_flag type c value 'F'.

*Internal Table declaration.
data: begin of t_vbak occurs 0,
    vbeln type vbak-vbeln,
    vbtyp type vbak-vbtyp,
    vkorg type vbak-vkorg,
    vtweg type vbak-vtweg,
    spart type vbak-spart,
    erdat type vbak-erdat,
    ernam type vbak-ernam,
end of t_vbak.

data: begin of t_vbap occurs 0,
      vbeln type vbap-vbeln,
      posnr type vbap-posnr,
      matnr type vbap-matnr,
      lgort type vbap-lgort,
      vstel type vbap-vstel,
      prctr type vbap-prctr,
      aufnr type vbap-aufnr,
      voref type vbap-voref,
      zmeng type vbap-zmeng,
      netpr type vbap-netpr,
end of t_vbap.

data: begin of t_output occurs 0 ,
      vbeln like vbak-vbeln,  "Sales Document number
      posnr like vbap-posnr,  "Sales Document Item
      matnr like vbap-matnr,  "Material njumber
      vbtyp like vbak-vbtyp,  "SD document category
      vkorg like vbak-vkorg,  "SAles Organisation
      vtweg like vbak-vtweg,  "Distribution Channel
      spart like vbak-spart,  "Division
      erdat like vbak-erdat,  "Record Creation date
      ernam like vbak-ernam,  "Person who created record
      lgort like vbap-lgort,  "Storage Location
      vstel like vbap-vstel,  "Shipping /Recieving point
      prctr like vbap-prctr,  "Profit center
      aufnr like vbap-aufnr,  "Order number
      voref like vbap-voref,  "Reference indicator
                              "(Color: Red if X else Green)
      zmeng like vbap-zmeng,  "Target quantity
      netpr like vbap-netpr,  "Net price
      comments(100) type c,   "Comments(Editable)
  end of t_output.

* Work Area
data wa_output like t_output.

*Selection Screen Display.
selection-screen : begin of block b1 with frame title text-h01.
select-options: s_vbeln for vbak-vbeln,
                s_erdat for vbak-erdat,
                s_ernam for vbak-ernam,
                s_vbtyp for vbak-vbtyp,
                s_vkorg for vbak-vkorg,
                s_vtweg for vbak-vtweg,
                s_spart for vbak-spart.

selection-screen : end of block b1.

*----------------------------------------------------------------------*
*       CLASS lcl_event_handler DEFINITION
*----------------------------------------------------------------------*

class lcl_event_handler definition .
  public section .
    class-methods:

*Double-click control
    handle_double_click
    for event double_click of cl_gui_alv_grid
    importing e_row e_column.
endclass.                    "lcl_event_handler DEFINITION

*----------------------------------------------------------------------*
*       CLASS lcl_event_handler IMPLEMENTATION
*----------------------------------------------------------------------*
class lcl_event_handler implementation .

*Handle Double Click
  method handle_double_click .
    read table t_output into wa_output index e_row-index.

* Display Sales Order on double click.
    if sy-subrc eq 0 and e_column-fieldname eq 'VBELN'.
      call transaction 'VA03' and skip first screen.
    endif.
  endmethod .                    "handle_double_click
endclass.                    "lcl_event_handler IMPLEMENTATION

* validation at Selection Screen.

at selection-screen.
 perform data_validation.

* Selecting data.
start-of-selection.
  if gv_valid_flag ne 'E'.
    select vbeln vbtyp vkorg vtweg spart erdat ernam
     from vbak
     into corresponding fields of table t_vbak
     where vbeln in s_vbeln and vkorg in s_vkorg
            and vtweg in s_vtweg
            and spart in s_spart and erdat in s_erdat
            and ernam in s_ernam and vbtyp in s_vbtyp.

    if not t_vbak[] is initial.
     select vbeln posnr matnr lgort vstel prctr aufnr voref zmeng netpr
        into corresponding fields of table t_vbap
        from vbap
        for all entries in t_vbak
        where vbeln = t_vbak-vbeln.
    endif.
  endif.

  t_output[] = t_vbak[].

  if not t_output[] is initial.
    loop at t_output.
      read table t_vbap with key vbeln = t_output-vbeln.
      move t_vbap-posnr to t_output-posnr.
      move t_vbap-matnr to t_output-matnr.
      move t_vbap-lgort to t_output-lgort.
      move t_vbap-vstel to t_output-vstel.
      move t_vbap-prctr to t_output-prctr.
      move t_vbap-aufnr to t_output-aufnr.
      move t_vbap-voref to t_output-voref.
      move t_vbap-zmeng to t_output-zmeng.
      move t_vbap-netpr to t_output-netpr.
      insert t_output index sy-tabix.
    endloop.
  endif.

   if not t_output[] is initial.
   call screen '2000'.
   else.
      message 'No data fetched' type 'I'.
   endif.

*  perform display_alv.
*&---------------------------------------------------------------------*
*       FORM display_alv                                              *
*---------------------------------------------------------------------*

form display_alv.

  if gr_alvgrid is initial .

*----Creating custom container instance
    create object gr_ccontainer
      exporting
        container_name = 'CUSTOM_CONTROL'
*      container_name = gc_custom_control_name
      exceptions
        cntl_error = 1
        cntl_system_error = 2
        create_error = 3
        lifetime_error = 4
        lifetime_dynpro_dynpro_link = 5
        others = 6 .

    if sy-subrc <> 0.
*--Exception handling
    endif.

*----Creating ALV Grid instance

    create object gr_alvgrid
      exporting
        i_parent = gr_ccontainer
      exceptions
        error_cntl_create = 1
        error_cntl_init = 2
        error_cntl_link = 3
        error_dp_create = 4
        others = 5 .

    if sy-subrc <> 0.
*--Exception handling
    endif.

*----Preparing layout structure
    perform prepare_layout changing gs_layout .

*----Preparing field catalog.
    perform prepare_field_catalog changing gt_fieldcat .

*----Here will be additional preparations

*--e.g. initial sorting criteria, initial filtering criteria, excluding

*--functions
    call method gr_alvgrid->set_table_for_first_display
      exporting
* I_BUFFER_ACTIVE =
* I_CONSISTENCY_CHECK =
* i_STRUCTURE_NAME =
* IS_VARIANT =
* I_SAVE =
* I_DEFAULT = 'X'
        is_layout = gs_layout
* IS_PRINT =
* IT_SPECIAL_GROUPS =
* IT_TOOLBAR_EXCLUDING =
* IT_HYPERLINK =
      changing
        it_outtab = t_output[]
        it_fieldcatalog = gt_fieldcat
* IT_SORT =
* IT_FILTER =
      exceptions
          invalid_parameter_combination = 1
          program_error = 2
          too_many_lines = 3
          others = 4 .

    if sy-subrc <> 0.
*--Exception handling
    endif.
  else .
    call method gr_alvgrid->refresh_table_display
* EXPORTING
* IS_STABLE =
* I_SOFT_REFRESH =
    exceptions
    finished = 1
    others = 2 .

    if sy-subrc <> 0.
*--Exception handling
    endif.
  endif .

*--Registering handler methods to handle ALV Grid events
  set handler lcl_event_handler=>handle_double_click for gr_alvgrid.
endform .                    "display_alv

*&---------------------------------------------------------------------*
*       FORM prepare_fieldcatalo
*---------------------------------------------------------------------*

form prepare_field_catalog  changing p_fcat type lvc_t_fcat .
data m_fieldcat type lvc_s_fcat .

*creating the field catalog manually
clear m_fieldcat .

m_fieldcat-col_pos = '1'.
m_fieldcat-fieldname = 'VBELN'.
m_fieldcat-tabname = 'T_SALES'.
m_fieldcat-seltext = 'Document No.'.
m_fieldcat-reptext = 'Document No.'.
append m_fieldcat to p_fcat.

clear m_fieldcat .
m_fieldcat-col_pos = '2'.
m_fieldcat-fieldname = 'POSNR'.
m_fieldcat-tabname = 'T_SALES'.
m_fieldcat-seltext = 'Sales Doc Item'.
m_fieldcat-reptext = 'Sales Doc Item'.
append m_fieldcat to p_fcat.

clear m_fieldcat .
m_fieldcat-col_pos = '3'.
m_fieldcat-fieldname = 'MATNR'.
m_fieldcat-tabname = 'T_SALES'.
m_fieldcat-seltext = 'Material Number'.
m_fieldcat-reptext = 'Material Number'.
append m_fieldcat to p_fcat.

clear m_fieldcat .
m_fieldcat-col_pos = '4'.
m_fieldcat-fieldname = 'VBTYP'.
m_fieldcat-tabname = 'T_SALES'.
m_fieldcat-seltext = 'SD Doc Category'.
m_fieldcat-reptext = 'SD Doc Category'.
append m_fieldcat to p_fcat.

clear m_fieldcat .
m_fieldcat-col_pos = '5'.
m_fieldcat-fieldname = 'VKORG'.
m_fieldcat-tabname = 'T_SALES'.
m_fieldcat-seltext = 'Sales Organisation'.
m_fieldcat-reptext = 'Sales Organisation'.
append m_fieldcat to p_fcat.

clear m_fieldcat .
m_fieldcat-col_pos = '6'.
m_fieldcat-fieldname = 'VTWEG'.
m_fieldcat-tabname = 'T_SALES'.
m_fieldcat-seltext = 'Distribution Channel'.
m_fieldcat-reptext = 'Distribution Channel'.
append m_fieldcat to p_fcat.

clear m_fieldcat .
m_fieldcat-col_pos = '7'.
m_fieldcat-fieldname = 'SPART'.
m_fieldcat-tabname = 'T_SALES'.
m_fieldcat-seltext = 'Division'.
m_fieldcat-reptext = 'Division'.
append m_fieldcat to p_fcat.

clear m_fieldcat .
m_fieldcat-col_pos = '8'.
m_fieldcat-fieldname = 'ERDAT'.
m_fieldcat-tabname = 'T_SALES'.
m_fieldcat-seltext = 'Record creation Date'.
m_fieldcat-reptext = 'Record creation Date'.
append m_fieldcat to p_fcat.

clear m_fieldcat .
m_fieldcat-col_pos = '9'.
m_fieldcat-fieldname = 'ERNAM'.
m_fieldcat-tabname = 'T_SALES'.
m_fieldcat-seltext = 'Record Created By'.
m_fieldcat-reptext = 'Record Created By'.
append m_fieldcat to p_fcat.

clear m_fieldcat .
m_fieldcat-col_pos = '10'.
m_fieldcat-fieldname = 'LGORT'.
m_fieldcat-tabname = 'T_SALES'.
m_fieldcat-seltext = 'Storage location'.
m_fieldcat-reptext = 'Storage location'.
append m_fieldcat to p_fcat.

clear m_fieldcat .
m_fieldcat-col_pos = '11'.
m_fieldcat-fieldname = 'VSTEL'.
m_fieldcat-tabname = 'T_SALES'.
m_fieldcat-seltext = 'Shipping/Recieving Point'.
m_fieldcat-reptext = 'Shipping/Recieving Point'.
append m_fieldcat to p_fcat.

clear m_fieldcat .
m_fieldcat-col_pos = '12'.
m_fieldcat-fieldname = 'PRCTR'.
m_fieldcat-tabname = 'T_SALES'.
m_fieldcat-seltext = 'Profit Center'.
m_fieldcat-reptext = 'Profit Center'.
append m_fieldcat to p_fcat.

clear m_fieldcat .
m_fieldcat-col_pos = '13'.
m_fieldcat-fieldname = 'AUFNR'.
m_fieldcat-tabname = 'T_SALES'.
m_fieldcat-seltext = 'Order Number'.
m_fieldcat-reptext = 'Order Number'.
append m_fieldcat to p_fcat.

clear m_fieldcat .
m_fieldcat-col_pos = '14'.
m_fieldcat-fieldname = 'VOREF'.
m_fieldcat-tabname = 'T_SALES'.
m_fieldcat-seltext = 'Reference Indicator'.
m_fieldcat-reptext = 'Reference Indicator'.

if gv_flag eq 'F'.
  m_fieldcat-emphasize = 'C500'.
else.
  m_fieldcat-emphasize = 'C600'.
endif.

append m_fieldcat to p_fcat.

clear m_fieldcat .
m_fieldcat-col_pos = '15'.
m_fieldcat-fieldname = 'ZMENG'.
m_fieldcat-tabname = 'T_SALES'.
m_fieldcat-seltext = 'Target Quantity'.
m_fieldcat-reptext = 'Target Quantity'.
append m_fieldcat to p_fcat.

clear m_fieldcat .
m_fieldcat-col_pos = '16'.
m_fieldcat-fieldname = 'NETPR'.
m_fieldcat-tabname = 'T_SALES'.
m_fieldcat-seltext = 'Net Price'.
m_fieldcat-reptext = 'Net Price'.
append m_fieldcat to p_fcat.

clear m_fieldcat .
m_fieldcat-col_pos = '17'.
m_fieldcat-fieldname = 'COMMENTS'.
m_fieldcat-tabname = 'T_SALES'.
m_fieldcat-seltext = 'COMMENTS'.
m_fieldcat-reptext = 'COMMENTS'.
m_fieldcat-edit = 'X'.
m_fieldcat-tooltip = text-011.
m_fieldcat-outputlen = 50.
append m_fieldcat to p_fcat.

endform.                    " prepare_field_catalog

*&---------------------------------------------------------------------*
*&      Form  prepare_layout
*&---------------------------------------------------------------------*
*      <--P_GS_LAYOUT  text
*----------------------------------------------------------------------*

form prepare_layout  changing p_gs_layout type lvc_s_layo.
  p_gs_layout-zebra = 'X' .
  p_gs_layout-grid_title = 'Sales Document Details' .
  p_gs_layout-smalltitle = 'X' .
  p_gs_layout-cwidth_opt = 'X' .

* If the field value of Reference Indicator(VOREF) is 'X' then the color
* in the output should be set as red, else green.
  loop at t_output into wa_output.

    if wa_output-voref eq 'X'.
      gv_flag = 'T'.
    endif.
  endloop.

endform.                    " prepare_layout
 

*&---------------------------------------------------------------------*
*&      Form  data_validation
*&---------------------------------------------------------------------*
*       validate the data on the selection screen
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*

form data_validation .
  data: v_vbeln like vbak-vbeln,
        v_erdat like vbak-erdat,
        v_ernam like vbak-ernam,
        v_vbtyp like vbak-vbtyp,
        v_vkorg like vbak-vkorg,
        v_vtweg like vbak-vtweg,
        v_spart like vbak-spart.

* validating the Doc number.
  if not s_vbeln is initial.
    select single vbeln from vbak into v_vbeln
      where vbeln eq s_vbeln-low.

    if sy-subrc ne 0.
      gv_valid_flag = 'E'.        " validation flag set to Error
      message text-012 type 'E'.  " Invalid Sales Doc Number
    endif.
  endif.

* validating the Record creation date.
  if not s_erdat is initial.
    select single erdat from vbak into v_erdat
      where erdat eq s_erdat-low.
    if sy-subrc ne 0.
      gv_valid_flag = 'E'.        " validation flag set to Error
      message text-013 type 'E'.  " Invalid Record creation date
    endif.
  endif.

* validating the Person who created the record.
  if not s_ernam is initial.
    select single ernam from vbak into v_ernam
      where ernam eq s_ernam-low.

    if sy-subrc ne 0.
      gv_valid_flag = 'E'.        " validation flag set to Error
      message text-014 type 'E'.  " Invalid Person creating record
    endif.
  endif.

* validating the Document category type.
  if not s_vbtyp is initial.
    select single vbtyp from vbak into v_vbtyp
      where vbtyp eq s_vbtyp-low.
    if sy-subrc ne 0.
      gv_valid_flag = 'E'.        " validation flag set to Error
      message text-015 type 'E'.  " Invalid doc category
    endif.
  endif.

* validating the Sales Organization.
  if not s_vkorg is initial.
    select single vkorg from vbak into v_vkorg
      where vkorg eq s_vkorg-low.
    if sy-subrc ne 0.
      gv_valid_flag = 'E'.        " validation flag set to Error
      message text-016 type 'E'.  " Invalid Sales Organisation
    endif.
  endif.

* validating the distribution channel.
  if not s_vtweg is initial.
    select single vtweg from vbak into v_vtweg
      where vtweg eq s_vtweg-low.
    if sy-subrc ne 0.
      gv_valid_flag = 'E'.        " validation flag set to Error
      message text-017 type 'E'.  " Invalid Distribution channel
    endif.
  endif.

* validating the Division.
  if not s_spart is initial.
    select single spart from vbak into v_spart
      where spart eq s_spart-low.
    if sy-subrc ne 0.
      gv_valid_flag = 'E'.        " validation flag set to Error
      message text-018 type 'E'.  " Invalid Division
    endif.
  endif.
endform.                    " data_validation

*&---------------------------------------------------------------------*
*&      Module  STATUS_2000  OUTPUT
*&---------------------------------------------------------------------*

module status_2000 output.
  set pf-status 'Z101756_ALV_OOPS'.
  set titlebar 'Sales Records'.
  perform display_alv.

*if the data fetched successfully for the selection criteria entered by
*the user,the subroutine to display the ALV grid output is called
endmodule.                 " STATUS_2000  OUTPUT

*&---------------------------------------------------------------------*
*&      Module  USER_COMMAND_2000  INPUT
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*

module user_command_2000 input.
  if sy-ucomm eq 'BACK'.
    leave to screen 0.
  endif.
endmodule.                 " USER_COMMAND_2000  INPUT

*&---------------------------------------------------------------------*
*&      Form  handle_double_click
*&---------------------------------------------------------------------*
*      -->P_E_ROW  text
*      -->P_E_COLUMN  text
*      -->P_ES_ROW_NO  text
*----------------------------------------------------------------------*

form handle_double_click  using     i_row type lvc_s_row
                                    i_column type lvc_s_col
                                    is_row_no type lvc_s_roid.
endform.                    " handle_double_click */

----------- 

ABAP Tips

Read Also
Reading attribute of a BOR (Business Object)  in ABAP

Get help for your ABAP problems
Do you have a ABAP Question?

ABAP Books
ABAP Certification, BAPI, Java, Web Programming, Smart Forms, Sapscripts Reference Books

More ABAP Tips

Main Index
SAP ERP Modules, Basis, ABAP and Other IMG Stuff

All the site contents are Copyright © www.erpgreat.com and the content authors. All rights reserved.
All product names are trademarks of their respective companies.  The site www.erpgreat.com is in no way affiliated with SAP AG. 
Every effort is made to ensure the content integrity.  Information used on this site is at your own risk. 
 The content on this site may not be reproduced or redistributed without the express written permission of 
www.erpgreat.com or the content authors.