Ir al contenido principal

Link en campo de ALV

OO ALV with hotspot and call transaction


OO ALV report with call transaction on hotspot click using CL_SALV_TABLE

There was a requirement to display list of materials in an ALV control and put hyperlink(hotspot) to material no, when ever we click on material number, it should call MM03 and directly display material.
In this kind of requirement, we need to use below CALL TRANSACTION with parameter id for calling a transaction with parameters.
In the below example, we use object oriented techniques to display ALV table and will use events to handle hyperlink click.
CLASSCL_SALV_TABLE

Step1: Display ALV using class CL_SALV_TABLE and method DISPLAY()

Refer working with ALV Factory methods for more details on factory methods
TYPES: BEGIN OF ty_mara,
         matnr TYPE mara-matnr,
         mtart TYPE mara-mtart,
         matkl TYPE mara-matkl,
       END OF ty_mara.

DATA: it_mara TYPE TABLE OF ty_mara,
      wa_mara TYPE ty_mara.
DATA: o_alv TYPE REF TO cl_salv_table.

START-OF-SELECTION.
  SELECT matnr mtart matkl
    FROM mara
    INTO TABLE it_mara
    UP TO 10 ROWS.

  DATA: lx_msg TYPE REF TO cx_salv_msg.
  TRY.
      cl_salv_table=>factory(
        IMPORTING
          r_salv_table = o_alv
        CHANGING
          t_table      = it_mara ).
    CATCH cx_salv_msg INTO lx_msg.
  ENDTRY.

  o_alv->display( ).

Step2: Create a local class to create hyperlink and event handler

CLASS lcl_alv DEFINITION.
  PUBLIC SECTION.
    METHODS:
      set_hotspot_matnr
        CHANGING
          co_alv TYPE REF TO cl_salv_table.
    METHODS:
      on_matnr_click
            FOR EVENT link_click OF cl_salv_events_table
        IMPORTING
            row
            column  .
ENDCLASS.
CLASS lcl_alv IMPLEMENTATION.

  METHOD set_hotspot_matnr.
* Set Hotspot
    DATA: lo_cols_tab TYPE REF TO cl_salv_columns_table,
          lo_col_tab  TYPE REF TO cl_salv_column_table.
*   Get columns object
    lo_cols_tab = co_alv->get_columns( ).
*   Get MATNR column
    TRY.
        lo_col_tab ?= lo_cols_tab->get_column( 'MATNR' ).
      CATCH cx_salv_not_found.
    ENDTRY.
*   Set the hotspot for MATNR column
    TRY.
        CALL METHOD lo_col_tab->set_cell_type
          EXPORTING
            value = if_salv_c_cell_type=>hotspot.
      CATCH cx_salv_data_error .
    ENDTRY.
*...Events
    DATA: lo_events TYPE REF TO cl_salv_events_table.
*   All events
    lo_events = o_alv->get_event( ).
*   Event handler
    SET HANDLER me->on_matnr_click FOR lo_events.
*
  ENDMETHOD.
  METHOD on_matnr_click.
*   Get the material number from the table
    READ TABLE it_mara INTO wa_mara INDEX row.
    IF wa_mara-matnr IS NOT INITIAL.
** Call Transaction
      SET PARAMETER ID 'MAT' FIELD wa_mara-matnr . "Set Material no to MM03 matnr parameter id
      SET PARAMETER ID 'MXX' FIELD 'K' . "Directly Display Basic Data
      CALL TRANSACTION 'MM03' AND SKIP FIRST SCREEN . 
    ENDIF.
  ENDMETHOD.
ENDCLASS.

Step3: Create Object set hotspot .

  CREATE OBJECT lo_alv.
  lo_alv->set_hotspot_matnr( CHANGING co_alv = o_alv ).
  o_alv->display( ).

Final and complete code

REPORT zalv_hyperlink.
TYPES: BEGIN OF ty_mara,
         matnr TYPE mara-matnr,
         mtart TYPE mara-mtart,
         matkl TYPE mara-matkl,
       END OF ty_mara.
DATA: it_mara TYPE TABLE OF ty_mara,
      wa_mara TYPE ty_mara.
DATA: o_alv TYPE REF TO cl_salv_table.
CLASS lcl_alv DEFINITION DEFERRED.
DATA: lo_alv TYPE REF TO lcl_alv.

CLASS lcl_alv DEFINITION.
  PUBLIC SECTION.
    METHODS:
      set_hotspot_matnr
        CHANGING
          co_alv TYPE REF TO cl_salv_table.
    METHODS:
      on_matnr_click
            FOR EVENT link_click OF cl_salv_events_table
        IMPORTING
            row
            column  .
ENDCLASS.
START-OF-SELECTION.
  SELECT matnr mtart matkl
    FROM mara
    INTO TABLE it_mara
  UP TO 10 ROWS.

  DATA: lx_msg TYPE REF TO cx_salv_msg.
  TRY.
      cl_salv_table=>factory(
        IMPORTING
          r_salv_table = o_alv
        CHANGING
          t_table      = it_mara ).
    CATCH cx_salv_msg INTO lx_msg.
  ENDTRY.
  CREATE OBJECT lo_alv.
  lo_alv->set_hotspot_matnr( CHANGING co_alv = o_alv ).
  o_alv->display( ).

CLASS lcl_alv IMPLEMENTATION.

  METHOD set_hotspot_matnr.
* Set Hotspot
    DATA: lo_cols_tab TYPE REF TO cl_salv_columns_table,
          lo_col_tab  TYPE REF TO cl_salv_column_table.
*   Get columns object
    lo_cols_tab = co_alv->get_columns( ).
*   Get MATNR column
    TRY.
        lo_col_tab ?= lo_cols_tab->get_column( 'MATNR' ).
      CATCH cx_salv_not_found.
    ENDTRY.
*   Set the hotspot for MATNR column
    TRY.
        CALL METHOD lo_col_tab->set_cell_type
          EXPORTING
            value = if_salv_c_cell_type=>hotspot.
      CATCH cx_salv_data_error .
    ENDTRY.
*...Events
    DATA: lo_events TYPE REF TO cl_salv_events_table.
*   All events
    lo_events = o_alv->get_event( ).
*   Event handler
    SET HANDLER me->on_matnr_click FOR lo_events.
*
  ENDMETHOD.
  METHOD on_matnr_click.
*   Get the material number from the table
    READ TABLE it_mara INTO wa_mara INDEX row.
    IF wa_mara-matnr IS NOT INITIAL.
** Call Transaction
      SET PARAMETER ID 'MAT' FIELD wa_mara-matnr . "Set Material no to MM03 matnr parameter id
      SET PARAMETER ID 'MXX' FIELD 'K' . "Directly Display Basic Data
      CALL TRANSACTION 'MM03' AND SKIP FIRST SCREEN . 
    ENDIF.
  ENDMETHOD.
ENDCLASS.
ALV hyperlink and call transaction

Fuente:

Comentarios

Entradas más populares de este blog

A to Z of OLE Excel in ABAP 7.4

  SAP users, both business and end users always need to download the output of a report to spreadsheet and do their analytics.   The standard excel output from a report is very simple process but it is old fashioned and the spreadsheet looks quite boring.  There is no default formatting and the users have to do all the hard work of changing the fonts, coloring the texts, marking the borders etc. Updated 16th Aug 2019  –  If you are working in non-ABAP 7.4 (below), there is complete reference program for you too. Go to the end of this article.  Thank you  Legxis  ( LeonievonK ) for the share. I acknowledge, whatever I mentioned above can be achieved in many ways programmatically. We can do it in the old traditional ABAP way but  providing multiple tabs in the spreadsheet and formatting is quite tricky with non  OLE  method. OLE = Object Linking and Embedding The high level agenda of this article is to be the  G.O.A.T.  (please google it if you do not know the full form)  of OLE Excel Gu
How to add custom field in Additional B Tab for SAP Sales Order In this article we want to explain step by step how to add custom field in SAP Sales Order transaction VA01/VA02/VA03. This time Mr ABAPGurus will give the tutorial of enhancement to add custom field in SAP Sales order. In this sample we will add new custom field for comments and customer satisfaction, this data will save into separate table from SAP Standard table ( VBAK / VBAP ). SAP provided us with ADDITIONAL TAB in the sales order transaction ( VA01/VA02/VA03 ) which allow customer to add custom fields.  T he different between ADDITIONAL A and ADDITIONAL B is the ADDITIONAL A for field which already predefine in Sales Order Header ( VBAK ) fields and ADDITIONAL B is for field that freely define. This sample we will use ADDITIONAL B because we will store data from customer satisfaction into Z database table. 1. Create one Z table using SE11 SAP Transaction code. 2.Using SE38 Transaction code and ope

VOFM

Creating VOFM custom routine, functionality same as SAP standard routine 103 with Additional Functionality 1.    Introduction 1.1   Document Purpose 1.2    This is the document helps you to create custom routine for sales order for below specific requirement: Functional description: In case of sale order with the SD document category ‘K’ or ‘L’ or ‘H’ related to an invoice, we need to fill the fixed value date (VBKD_VALDT) with the billing reference date (VBRK_FKDAT). For example: Invoice 90001111, the billing date is 22.07.2013 If we create a sale order related to this invoice, we want the fixed value date equal to the billing reference date. As per the requirement: Sap standard routine ‘103’ triggers whenever sales order is being created with reference. Sap standard routine is configured in VOFM transaction as: Sap standard routine ‘103’ has the functionality of copying the billing document header data and billing document line item data into the