Sunday, December 1, 2013

Simple Code to consume Web service using SAP ABAP

REPORT  zpw_webservice.
 
*&---------------------------------------------------------------------*
*&      Selection Screen
*&---------------------------------------------------------------------*
PARAMETERS : p_cnt TYPE t005t-landx .
 
*&---------------------------------------------------------------------*
*&      Types and Data
*&---------------------------------------------------------------------*
DATA:  http_client    TYPE REF TO if_http_client ,
       http_url       TYPE string                ,
       p_content      TYPE string                .
 
*&---------------------------------------------------------------------*
*&      Start of Selection
*&---------------------------------------------------------------------*
START-OF-SELECTION .
 
* Build the url string based on input
  '/GetCitiesByCountry?CountryName='
  p_cnt
  INTO http_url .
 
* Creation of new IF_HTTP_Client object
  CALL METHOD cl_http_client=>create_by_url
    EXPORTING
      url                = http_url
    IMPORTING
      client             = http_client
    EXCEPTIONS
      argument_not_found = 1
      plugin_not_active  = 2
      internal_error     = 3
      OTHERS             = 4.
 
  http_client->request->set_header_field( name  = '~request_method'
  value = 'GET' ).
* Send the request
  http_client->send( ).
 
* Reterive the result
  CALL METHOD http_client->receive
    EXCEPTIONS
      http_communication_failure = 1
      http_invalid_state         = 2
      http_processing_failed     = 3
      OTHERS                     = 4.
 
  p_content = http_client->response->get_cdata( ).
  REPLACE  ALL OCCURRENCES OF '&lt;' IN p_content WITH '<' .
  REPLACE  ALL OCCURRENCES OF '&gt;' IN p_content WITH '>' .
 
*&---------------------------------------------------------------------*
*&      Processing string
*&---------------------------------------------------------------------*
 
  DATA : moff  TYPE syst-tabix ,
         moff1 TYPE syst-tabix ,
         len   TYPE syst-tabix .
 
  DO .
    FIND '<City>' IN SECTION OFFSET moff OF p_content IGNORING CASE MATCH OFFSET moff .
    IF sy-subrc = 0 .
      moff = moff + 6 .
      FIND '</City>' IN SECTION OFFSET moff OF p_content IGNORING CASE MATCH OFFSET moff1 .
      len = moff1 - moff .
      WRITE : / p_content+moff(len) .
    ELSE.
      EXIT.
    ENDIF.
  ENDDO .

In this code processing of XML string is done using FIND statement. SAP has introduced command CALL TRANSFORMATION since Basis release 6.10 for XML processing. This command CALL TRANSFORMATION process the whole XML string based on defined transformation and return it in variable/internal table. Transformations can be defined using transaction SE80. Ok I am leaving this topic for next blog ;)



 

What Are The Events in ALV

Events in alv and their FM    The main events in alv and their FM and why we use these: 
1. SLIS_PRINT_ALV. 
2. SLIS_T_LISTHEADER. 
3. SLIS_T_EVENT. 
4. SLIS_T_SORTINFO_ALV. 
5. SLIS_T_LAYOUT_ALV. 
6. SLIS_T_FIELDCAT_ALV. 
and in classic reports what is the sequence of events:   === Events are 


At selection-screen output. 
Initialization. 
At selection-screen on field 
At selection-screen on end of field 
At selection-screen on Radiobutton Group R1. (If you have any radio buttons) 
At selection-screen on block b1. (If you have any blocks) 
Start-of-selection. 
Get node. (if the data is retreived from a logical database) 
Get node late. (if the data is retreived from a logical database) 
Top-of-page. (if the write statement is in the end-of-selection event or we can say that before the first write statement) 
end-of-selection. 
and fuction modules are 
LISTHEADER - Is used to print the header information in the ALV List. Name, Date, Time, ALV Name and other details are called as Header information.   EVENT - Basically this is the FM to handle Event's. When the user needs to do some event operation like when double clicking the a particular field we need to perform some operation.   These events are captured by this FM.   LAYOUT - This FM is used to define the layout of the List. There are many options available in this FM to define the Layout style.   FIELDCAT - These are used to populate the List header. We can change them according to our req. 

User-defined Text Output Event 
    Application
      print_end_of_list
    Define output text to be printed at the end of the entire list
      print_top_of_list
    Define output text to be printed at the beginning of the entire list
      print_end_of_page
    Define output text to be printed at the end of each page
      print_top_of_page
    Define output text to be printed at the beginning of each page
      subtotal_text
    Define self-defined subtotals texts

Mouse-controlled Actions in the Grid Control Event 
    Application
      button_click
    Query a click on a pushbutton in the ALV Grid Control
      double_click
    Query a double-click on a cell of the ALV Grid control 
      hotspot_click
    Query a hotspot click on columns defined for this purpose in advance
      onDrag
    Collect information when elements of the ALV Grid Control are dragged 
      onDrop
    Process information when elements of the ALV Grid Control are dropped 
      onDropComplete
    Perform final actions after successful Drag&Drop 
      onDropGetFlavor
    Distinguish between options for Drag&Drop behavior
Processing of Self-defined and Standard Functions Event
    Application
      before_user_command
    Query self-defined and standard function codes
      user_command
    Query self-defined function codes
      after_user_command
    Query self-defined and standard function codes
Definition of Self-defined Functions Event
    Application
      toolbar
    Change, delete or add GUI elements in the toolbar
      menu_button
    Define menus for menu buttons in the toolbar
      context_menu_request
    Change context menu
      onf1
    Define self-defined F1 help

All of these can be found under type group SLIS.
* Events
SLIS_EV_ITEM_DATA_EXPAND        TYPE SLIS_FORMNAME VALUE 'ITEM_DATA_EXPAND',
SLIS_EV_REPREP_SEL_MODIFY       TYPE SLIS_FORMNAME VALUE 'REPREP_SEL_MODIFY', SLIS_EV_CALLER_EXIT_AT_START TYPE SLIS_FORMNAME VALUE 'CALLER_EXIT',
SLIS_EV_USER_COMMAND              TYPE SLIS_FORMNAME VALUE 'USER_COMMAND',
SLIS_EV_TOP_OF_PAGE                     TYPE SLIS_FORMNAME VALUE 'TOP_OF_PAGE',
SLIS_EV_DATA_CHANGED                TYPE SLIS_FORMNAME VALUE 'DATA_CHANGED',
SLIS_EV_TOP_OF_COVERPAGE       TYPE SLIS_FORMNAME VALUE 'TOP_OF_COVERPAGE',
SLIS_EV_END_OF_COVERPAGE       TYPE SLIS_FORMNAME VALUE 'END_OF_COVERPAGE',
SLIS_EV_FOREIGN_TOP_OF_PAGE TYPE SLIS_FORMNAME
VALUE 'FOREIGN_TOP_OF_PAGE', SLIS_EV_FOREIGN_END_OF_PAGE TYPE SLIS_FORMNAME
VALUE 'FOREIGN_END_OF_PAGE',
SLIS_EV_PF_STATUS_SET                  TYPE SLIS_FORMNAME VALUE 'PF_STATUS_SET',
SLIS_EV_LIST_MODIFY                      TYPE SLIS_FORMNAME VALUE 'LIST_MODIFY',
SLIS_EV_TOP_OF_LIST                       TYPE SLIS_FORMNAME VALUE 'TOP_OF_LIST',
SLIS_EV_END_OF_PAGE                    TYPE SLIS_FORMNAME VALUE 'END_OF_PAGE',
SLIS_EV_END_OF_LIST                      TYPE SLIS_FORMNAME VALUE 'END_OF_LIST',
SLIS_EV_AFTER_LINE_OUTPUT       TYPE SLIS_FORMNAME VALUE 'AFTER_LINE_OUTPUT', SLIS_EV_BEFORE_LINE_OUTPUT     TYPE SLIS_FORMNAME VALUE 'BEFORE_LINE_OUTPUT',
SLIS_EV_SUBTOTAL_TEXT                TYPE SLIS_FORMNAME VALUE 'SUBTOTAL_TEXT'.

How to find USER EXITS

Display the program where you are searching for and exit and search for CALL CUSTOMER-EXIT
If you know the Exit name, go to transaction CMOD.
Choose menu Utillities->SAP Enhancements. Enter the exit name and press enter.
You will now come to a screen that shows the function module exits for the exit.

Sunday, September 8, 2013

Nesting ALV Report

ABAP code:

report ZNESTINGALVRPT .

tables: ekko, ekpo.
type-pools: slis.

types: begin of ty_ekko,
        ebeln type ekko-ebeln,
        bukrs type ekko-bukrs,
        lifnr type ekko-lifnr,
       end of ty_ekko,

       begin of ty_out_ekko,
        sel,
        ebeln type ekko-ebeln,
        bukrs type ekko-bukrs,
        lifnr type ekko-lifnr,
       end of ty_out_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,
         menge type ekpo-menge,
         meins type ekpo-meins,
       end of ty_ekpo,

       begin of ty_out_ekpo,
         sel,
         ebeln type ekko-ebeln,
         ebelp type ekpo-ebelp,
         matnr type ekpo-matnr,
         werks type ekpo-werks,
         lgort type ekpo-lgort,
         menge type ekpo-menge,
         meins type ekpo-meins,
       end of ty_out_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,

      wa_out_ekko type ty_out_ekko,
      wa_out_ekpo type ty_out_ekpo,
      it_out_ekko type standard table of ty_out_ekko,
      it_out_ekpo type standard table of ty_out_ekpo,

      wa_fcat_ekko type slis_fieldcat_alv,
      wa_fcat_ekpo type slis_fieldcat_alv,
      it_fcat_ekko type slis_t_fieldcat_alv,
      it_fcat_ekpo type slis_t_fieldcat_alv,

      wa_layout type slis_layout_alv,

      wa_top_ekko type slis_listheader,
      wa_top_ekpo type slis_listheader,
      it_top_ekko type slis_t_listheader,
      it_top_ekpo type slis_t_listheader,

      wa_event_ekko type slis_alv_event,
      wa_event_ekpo type slis_alv_event,
      it_event_ekko type slis_t_event,
      it_event_ekpo type slis_t_event,

      r_ucomm     type sy-ucomm,
      rs_selfield type slis_selfield,
      v_selfield  type slis_selfield-value,
      v_ebeln type ekko-ebeln,
      v_prog  type sy-repid,
      v_name  type sy-uname.

initialization.
  v_prog = sy-repid.
  v_name = sy-uname.

  selection-screen begin of block b1 with frame title text-001.
  select-options   s_ebeln for ekko-ebeln obligatory.
  selection-screen end of block b1.

start-of-selection.
  perform get_ekko.
  perform fieldcat_ekko.
  perform layout.
  perform event_ekko.
  perform grid_ekko.
  perform ucomm_ekko using r_ucomm
                     changing rs_selfield.

top-of-page.
  perform top_ekko.

top-of-page during line-selection.
  perform top_ekpo.

*&---------------------------------------------------------------------*
*&      Form  ucomm_ekko
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*      -->R_UCOMM      text
*      -->RS_ESLFIELD  text
*----------------------------------------------------------------------*
form ucomm_ekko using    r_ucomm_ekko     type sy-ucomm
                changing rs_selfield_ekko type slis_selfield.

  case r_ucomm_ekko.
    when '&IC1'.
      if rs_selfield_ekko-fieldname = 'EBELN'.
        clear v_selfield.
        v_selfield = rs_selfield_ekko-value.
        perform conversion_po.
        perform get_ekpo.
        perform fieldcat_ekpo.
        perform layout.
        perform event_ekpo.
        perform grid_ekpo.
        perform ucomm_ekpo using r_ucomm
                           changing rs_selfield.
      else.
        message 'Invalid Field' type 'S'.
      endif.
  endcase.
endform.                    "ucomm_ekko
*&---------------------------------------------------------------------*
*&      Form  GET_EKKO
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
form get_ekko .

  refresh it_ekko.
  select ebeln bukrs lifnr
    from ekko into table it_ekko
    where ebeln in s_ebeln.

  if sy-subrc = 0.
    sort it_ekko by ebeln.
    refresh it_out_ekko.

    loop at it_ekko into wa_ekko.
      wa_out_ekko-ebeln = wa_ekko-ebeln.
      wa_out_ekko-bukrs = wa_ekko-bukrs.
      wa_out_ekko-lifnr = wa_ekko-lifnr.
      append wa_out_ekko to it_out_ekko.
      clear: wa_out_ekko, wa_ekko.
    endloop.

  else.
    message 'Purchase Order doesn''t exist' type 'I'.
  endif.

endform.                    " GET_EKKO
*&---------------------------------------------------------------------*
*&      Form  FIELDCAT_EKKO
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
form fieldcat_ekko .

  clear wa_fcat_ekko.
  refresh it_fcat_ekko.

  if it_out_ekko is not initial.
    data lv_col type i value 0.

    lv_col                 = 1 + lv_col.
    wa_fcat_ekko-col_pos   = lv_col.
    wa_fcat_ekko-fieldname = 'EBELN'.
    wa_fcat_ekko-tabname   = 'IT_OUT_EKKO'.
    wa_fcat_ekko-seltext_l = 'Purchase Order'.
    append wa_fcat_ekko to it_fcat_ekko.
    clear wa_fcat_ekko.

    lv_col                 = 1 + lv_col.
    wa_fcat_ekko-col_pos   = lv_col.
    wa_fcat_ekko-fieldname = 'BUKRS'.
    wa_fcat_ekko-tabname   = 'IT_OUT_EKKO'.
    wa_fcat_ekko-seltext_l = 'Company Code'.
    append wa_fcat_ekko to it_fcat_ekko.
    clear wa_fcat_ekko.

    lv_col                 = 1 + lv_col.
    wa_fcat_ekko-col_pos   = lv_col.
    wa_fcat_ekko-fieldname = 'LIFNR'.
    wa_fcat_ekko-tabname   = 'IT_OUT_EKKO'.
    wa_fcat_ekko-seltext_l = 'Vendor'.
    append wa_fcat_ekko to it_fcat_ekko.
    clear wa_fcat_ekko.
  endif.

endform.                    " FIELDCAT_EKKO
*&---------------------------------------------------------------------*
*&      Form  LAYOUT
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
form layout .

  wa_layout-zebra = 'X'.
  wa_layout-colwidth_optimize = 'X'.
  wa_layout-box_fieldname = 'SEL'.

endform.                    " LAYOUT
*&---------------------------------------------------------------------*
*&      Form  EVENT_EKKO
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
form event_ekko .

  refresh it_event_ekko.

  call function 'REUSE_ALV_EVENTS_GET'
*  EXPORTING
*    I_LIST_TYPE           = 0
   importing
     et_events             = it_event_ekko
   exceptions
     list_type_wrong       = 1
     others                = 2.

  if it_event_ekko is not initial.
    clear wa_event_ekko.
    read table it_event_ekko into wa_event_ekko
    with key name = 'USER_COMMAND'.

    if sy-subrc = 0.
      wa_event_ekko-form = 'UCOMM_EKKO'.
      modify it_event_ekko from wa_event_ekko
      index sy-tabix transporting form.
    endif.

    clear wa_event_ekko.
    read table it_event_ekko into wa_event_ekko
    with key name = 'TOP_OF_PAGE'.

    if sy-subrc = 0.
      wa_event_ekko-form = 'TOP_EKKO'.
      modify it_event_ekko from wa_event_ekko
      index sy-tabix transporting form.
    endif.
  endif.

endform.                    " EVENT_EKKO
*&---------------------------------------------------------------------*
*&      Form  GRID_EKKO
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
form grid_ekko .

  if    it_out_ekko is not initial
    and it_fcat_ekko is not initial.

    call function 'REUSE_ALV_LIST_DISPLAY'
     exporting
*      I_INTERFACE_CHECK              = ' '
*      I_BYPASSING_BUFFER             =
*      I_BUFFER_ACTIVE                = ' '
       i_callback_program             = v_prog
*      i_callback_pf_status_set       = ' '
       i_callback_user_command        = 'UCOMM_EKKO'
*      I_STRUCTURE_NAME               =
       is_layout                      = wa_layout
       it_fieldcat                    = it_fcat_ekko
*      IT_EXCLUDING                   =
*      IT_SPECIAL_GROUPS              =
*      IT_SORT                        =
*      IT_FILTER                      =
*      IS_SEL_HIDE                    =
*      I_DEFAULT                      = 'X'
*      I_SAVE                         = ' '
*      IS_VARIANT                     =
       it_events                      = it_event_ekko
*      IT_EVENT_EXIT                  =
*      IS_PRINT                       =
*      IS_REPREP_ID                   =
*      I_SCREEN_START_COLUMN          = 0
*      I_SCREEN_START_LINE            = 0
*      I_SCREEN_END_COLUMN            = 0
*      I_SCREEN_END_LINE              = 0
*      IR_SALV_LIST_ADAPTER           =
*      IT_EXCEPT_QINFO                =
*      I_SUPPRESS_EMPTY_DATA          = ABAP_FALSE
*    IMPORTING
*      E_EXIT_CAUSED_BY_CALLER        =
*      ES_EXIT_CAUSED_BY_USER         =
     tables
       t_outtab                       = it_out_ekko
     exceptions
       program_error                  = 1
       others                         = 2.

  endif.

endform.                    " GRID_EKKO
*&---------------------------------------------------------------------*
*&      Form  top_ekko
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
form top_ekko.

  clear wa_top_ekko.
  refresh it_top_ekko.

  data date type char12.

  call function 'CONVERT_DATE_TO_EXTERNAL'
    exporting
      date_internal            = sy-datum
    importing
      date_external            = date
    exceptions
      date_internal_is_invalid = 1
      others                   = 2.

  wa_top_ekko-typ = 'H'.
  wa_top_ekko-info = 'Purchase Order Header'.
  append wa_top_ekko to it_top_ekko.
  clear wa_top_ekko.

  wa_top_ekko-typ = 'S'.
  wa_top_ekko-info = 'Report: '.
  concatenate wa_top_ekko-info v_prog
  into wa_top_ekko-info.
  append wa_top_ekko to it_top_ekko.
  clear wa_top_ekko.

  wa_top_ekko-typ = 'S'.
  wa_top_ekko-info = 'User Name: '.
  concatenate wa_top_ekko-info v_name
  into wa_top_ekko-info.
  append wa_top_ekko to it_top_ekko.
  clear wa_top_ekko.

  wa_top_ekko-typ = 'S'.
  wa_top_ekko-info = 'Date: '.
  concatenate wa_top_ekko-info date
  into wa_top_ekko-info.
  append wa_top_ekko to it_top_ekko.
  clear wa_top_ekko.

  call function 'REUSE_ALV_COMMENTARY_WRITE'
    exporting
      it_list_commentary       = it_top_ekko
*     I_LOGO                   =
*     I_END_OF_LIST_GRID       =
*     I_ALV_FORM               =
            .

endform.                    "top_ekko
*&---------------------------------------------------------------------*
*&      Form  CONVERSION_PO
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
form conversion_po .

  clear v_ebeln.

  call function 'CONVERSION_EXIT_ALPHA_INPUT'
    exporting
      input  = v_selfield
    importing
      output = v_ebeln.

endform.                    " CONVERSION_PO
*&---------------------------------------------------------------------*
*&      Form  GET_EKPO
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
form get_ekpo .

  if v_ebeln is not initial.
    refresh it_ekpo.
    select ebeln ebelp matnr werks lgort menge meins
      from ekpo into table it_ekpo
      where ebeln = v_ebeln.

    if sy-subrc = 0.
      sort it_ekpo by ebelp.
      refresh it_out_ekpo.

      loop at it_ekpo into wa_ekpo.
        at new ebeln.
          wa_out_ekpo-ebeln = wa_ekpo-ebeln.
        endat.
        wa_out_ekpo-ebelp = wa_ekpo-ebelp.
        wa_out_ekpo-matnr = wa_ekpo-matnr.
        wa_out_ekpo-werks = wa_ekpo-werks.
        wa_out_ekpo-lgort = wa_ekpo-lgort.
        wa_out_ekpo-menge = wa_ekpo-menge.
        wa_out_ekpo-meins = wa_ekpo-meins.
        append wa_out_ekpo to it_out_ekpo.
        clear: wa_out_ekpo, wa_ekpo.
      endloop.
    endif.
  endif.

endform.                    " GET_EKPO
*&---------------------------------------------------------------------*
*&      Form  FIELDCAT_EKPO
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
form fieldcat_ekpo .

  clear wa_fcat_ekpo.
  refresh it_fcat_ekpo.

  if it_out_ekpo is not initial.
    data lv_col type i value 0.

    lv_col                 = 1 + lv_col.
    wa_fcat_ekpo-col_pos   = lv_col.
    wa_fcat_ekpo-fieldname = 'EBELN'.
    wa_fcat_ekpo-tabname   = 'IT_OUT_EKPO'.
    wa_fcat_ekpo-seltext_l = 'Purchase Order'.
    append wa_fcat_ekpo to it_fcat_ekpo.
    clear wa_fcat_ekpo.

    lv_col                 = 1 + lv_col.
    wa_fcat_ekpo-col_pos   = lv_col.
    wa_fcat_ekpo-fieldname = 'EBELP'.
    wa_fcat_ekpo-tabname   = 'IT_OUT_EKPO'.
    wa_fcat_ekpo-seltext_l = 'PO Item'.
    append wa_fcat_ekpo to it_fcat_ekpo.
    clear wa_fcat_ekpo.

    lv_col                 = 1 + lv_col.
    wa_fcat_ekpo-col_pos   = lv_col.
    wa_fcat_ekpo-fieldname = 'MATNR'.
    wa_fcat_ekpo-tabname   = 'IT_OUT_EKPO'.
    wa_fcat_ekpo-seltext_l = 'Material'.
    append wa_fcat_ekpo to it_fcat_ekpo.
    clear wa_fcat_ekpo.

    lv_col                 = 1 + lv_col.
    wa_fcat_ekpo-col_pos   = lv_col.
    wa_fcat_ekpo-fieldname = 'WERKS'.
    wa_fcat_ekpo-tabname   = 'IT_OUT_EKPO'.
    wa_fcat_ekpo-seltext_l = 'Plant'.
    append wa_fcat_ekpo to it_fcat_ekpo.
    clear wa_fcat_ekpo.

    lv_col                 = 1 + lv_col.
    wa_fcat_ekpo-col_pos   = lv_col.
    wa_fcat_ekpo-fieldname = 'LGORT'.
    wa_fcat_ekpo-tabname   = 'IT_OUT_EKPO'.
    wa_fcat_ekpo-seltext_l = 'Storage Location'.
    append wa_fcat_ekpo to it_fcat_ekpo.
    clear wa_fcat_ekpo.

    lv_col                 = 1 + lv_col.
    wa_fcat_ekpo-col_pos   = lv_col.
    wa_fcat_ekpo-fieldname = 'MENGE'.
    wa_fcat_ekpo-tabname   = 'IT_OUT_EKPO'.
    wa_fcat_ekpo-seltext_l = 'PO Quantity'.
    append wa_fcat_ekpo to it_fcat_ekpo.
    clear wa_fcat_ekpo.

    lv_col                 = 1 + lv_col.
    wa_fcat_ekpo-col_pos   = lv_col.
    wa_fcat_ekpo-fieldname = 'MEINS'.
    wa_fcat_ekpo-tabname   = 'IT_OUT_EKPO'.
    wa_fcat_ekpo-seltext_l = 'Unit of Measure'.
    append wa_fcat_ekpo to it_fcat_ekpo.
    clear wa_fcat_ekpo.
  endif.

endform.                    " FIELDCAT_EKPO
*&---------------------------------------------------------------------*
*&      Form  EVENT_EKPO
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
form event_ekpo .

  refresh it_event_ekpo.

  call function 'REUSE_ALV_EVENTS_GET'
* EXPORTING
*   I_LIST_TYPE           = 0
   importing
     et_events             = it_event_ekpo
   exceptions
     list_type_wrong       = 1
     others                = 2.

  if it_event_ekpo is not initial.
    clear wa_event_ekpo.
    read table it_event_ekpo into wa_event_ekpo
    with key name = 'USER_COMMAND'.

    if sy-subrc = 0.
      wa_event_ekpo-form = 'UCOMM_EKPO'.
      modify it_event_ekpo from wa_event_ekpo
      index sy-tabix transporting form.
    endif.

    clear wa_event_ekpo.
    read table it_event_ekpo into wa_event_ekpo
    with key name = 'TOP_OF_PAGE'.

    if sy-subrc = 0.
      wa_event_ekpo-form = 'TOP_EKPO'.
      modify it_event_ekpo from wa_event_ekpo
      index sy-tabix transporting form.
    endif.
  endif.

endform.                    " EVENT_EKPO
*&---------------------------------------------------------------------*
*&      Form  GRID_EKPO
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
form grid_ekpo .

  if    it_out_ekpo is not initial
    and it_fcat_ekpo is not initial.

    call function 'REUSE_ALV_LIST_DISPLAY'
     exporting
*      I_INTERFACE_CHECK              = ' '
*      I_BYPASSING_BUFFER             =
*      I_BUFFER_ACTIVE                = ' '
       i_callback_program             = v_prog
*      I_CALLBACK_PF_STATUS_SET       = ' '
       i_callback_user_command        = 'UCOMM_EKPO'
*      I_STRUCTURE_NAME               =
       is_layout                      = wa_layout
       it_fieldcat                    = it_fcat_ekpo
*      IT_EXCLUDING                   =
*      IT_SPECIAL_GROUPS              =
*      IT_SORT                        =
*      IT_FILTER                      =
*      IS_SEL_HIDE                    =
*      I_DEFAULT                      = 'X'
*      I_SAVE                         = ' '
*      IS_VARIANT                     =
       it_events                      = it_event_ekpo
*      IT_EVENT_EXIT                  =
*      IS_PRINT                       =
*      IS_REPREP_ID                   =
*      I_SCREEN_START_COLUMN          = 0
*      I_SCREEN_START_LINE            = 0
*      I_SCREEN_END_COLUMN            = 0
*      I_SCREEN_END_LINE              = 0
*      IR_SALV_LIST_ADAPTER           =
*      IT_EXCEPT_QINFO                =
*      I_SUPPRESS_EMPTY_DATA          = ABAP_FALSE
*    IMPORTING
*      E_EXIT_CAUSED_BY_CALLER        =
*      ES_EXIT_CAUSED_BY_USER         =
     tables
       t_outtab                       = it_out_ekpo
     exceptions
       program_error                  = 1
       others                         = 2.
  endif.

endform.                    " GRID_EKPO

*&---------------------------------------------------------------------*
*&      Form  top_ekpo
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
form top_ekpo.

  clear wa_top_ekpo.
  refresh it_top_ekpo.

  wa_top_ekpo-typ = 'H'.
  wa_top_ekpo-info = 'Purchase Order Item wise Display'.
  append wa_top_ekpo to it_top_ekpo.
  clear wa_top_ekpo.

  call function 'REUSE_ALV_COMMENTARY_WRITE'
    exporting
      it_list_commentary       = it_top_ekpo
*   I_LOGO                   =
*   I_END_OF_LIST_GRID       =
*   I_ALV_FORM               =
            .

endform.                    "top_ekpo
*&---------------------------------------------------------------------*
*&      Form  UCOMM_EKPO
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*      -->P_R_UCOM  text
*      <--P_RS_SELFIELD  text
*----------------------------------------------------------------------*
form ucomm_ekpo  using    r_ucomm_ekpo      type sy-ucomm
                 changing rs_selfield_ekpo  type slis_selfield.

  case r_ucomm_ekpo.
    when '&IC1'.
      if rs_selfield_ekpo-fieldname = 'EBELN'.

        set parameter id 'BES' field v_ebeln.
        call transaction 'ME23N'.

      else.
        message 'Invalid Field' type 'S'.
      endif.
  endcase.


endform.                    " UCOMM_EKPO



OUTPUT: 



                                        Run the program. Get the output like below


   Select and double click on PO number. You will get the details of the selected PO number.