Search code examples
abapalv

Call transaction when double clicked on ALV line


I currently displaying a table as an ALV as follows:

DATA: alv          TYPE REF TO cl_salv_table,
      output_table TYPE TABLE OF output_table.
TRY.
    cl_salv_table=>factory(
      IMPORTING
        r_salv_table = alv
      CHANGING
        t_table      = output_table ).
  CATCH cx_salv_msg INTO DATA(msg).
    cl_demo_output=>display( msg ).
ENDTRY.

alv->display( ).

My output table contains a material number as well as the plant.

Example:

Material Plant ...
123456789 0001 ...
999999999 0002 ...

I want that transaction MM03 (with material and plant and view Accounting 1) is called when clicked on one line in the ALV table.

I've found some solutions on the internet but those are not quite working for me. Do you have some clues on how to proceed with this topic?

What I tried was:

DATA(o_alv) = cl_salv_gui_table_ida=>create( iv_table_name = 'SFLIGHT' ).
o_alv->display_options( )->enable_double_click( ).
SET HANDLER lcl_events=>on_double_click FOR o_alv->display_options( ).
[...]

but my table is not an internal table of SAP.


Solution

  • Before you create your ALV, make sure to define your event handler class. Assuming your output_table is global and has the fields MATNR and WERKS, it could look like this:

    * define class for event handling
    CLASS lcl_events DEFINITION.
      PUBLIC SECTION.
        CLASS-METHODS: on_double_click FOR EVENT double_click OF cl_salv_events_table
          IMPORTING
              row
              column
              sender.
    ENDCLASS.
      
    CLASS lcl_events IMPLEMENTATION.
      METHOD on_double_click.
        " Open Accounting 1 view in MM03
        SET PARAMETER ID 'MAT' FIELD output_table[ row ]-matnr .
        SET PARAMETER ID 'WRK' FIELD output_table[ row ]-werks .
        SET PARAMETER ID 'MXX' FIELD 'B' .
        CALL TRANSACTION 'MM03' AND SKIP FIRST SCREEN .
      ENDMETHOD.
    ENDCLASS.
    

    After calling cl_salv_table=>factory( ), register your event handler for double click:

    SET HANDLER lcl_events=>on_double_click FOR alv->get_event( ).