Monday, 2 March 2026

ALV Report with Custom Layout Selection - SAP ABAP

 *&---------------------------------------------------------------------*

*& Report ZAK_LAYOUT_SAVE
*&---------------------------------------------------------------------*
*& This code will show you how to use Enable Save Layout Functionality with Custom Layout in SALV ALV
*&---------------------------------------------------------------------*
report zak_layout_save.

include zak_layout_save_top.
include zak_layout_save_lcl.
include zak_layout_save_f01.

at selection-screen on value-request for p_layout.
  perform f4_layout.

start-of-selection.

  datago_report type ref to lcl_report.
  create object go_report.

  go_report->get_data).
  go_report->display_alv).


*&---------------------------------------------------------------------*
*&  Include           ZAK_LAYOUT_SAVE_TOP
*&---------------------------------------------------------------------*

tablesekkoekpo.

"""""""""Selection Screen""""""""""""""
select-optionss_ebeln for ekko-ebeln.
parametersp_layout type slis_vari.
"""""""""""""""""""""""""""""""""""""""

typesbegin of ty_final,
         ebeln type ebeln,
         bsart type esart,
         bedat type ebdat,
         matnr type matnr,
         menge type bstmg,
         meins type bstme,
         netpr type bprei,
       end of ty_final.

datagt_final type standard table of ty_final.

datago_alv  type ref to cl_salv_table,
      go_cols type ref to cl_salv_columns,
      go_col  type ref to cl_salv_column,
      go_lay  type ref to cl_salv_layout,
      ls_key  type salv_s_layout_key,
      gt_lay  type salv_t_layout_info,
      gs_lay  type salv_s_layout_info.

*&---------------------------------------------------------------------*
*&  Include           ZAK_LAYOUT_SAVE_LCL
*&---------------------------------------------------------------------*

class lcl_report definition.
  public section.
    methodsget_data,
      display_alv.
endclass.

class lcl_report implementation.

  method get_data.

    select a~ebeln,
           a~bsart,
           a~bedat,
           b~matnr,
           b~menge,
           b~meins,
           b~netpr from ekko as a inner join ekpo as on a~ebeln b~ebeln into table @gt_final
      where a~ebeln in @s_ebeln.

  endmethod.

  method display_alv.

    try.
        cl_salv_table=>factory(
          importing
            r_salv_table   go_alv    " Basis Class Simple ALV Tables
          changing
            t_table        gt_final
        ).
      catch cx_salv_msg into data(lx_msg).
        message lx_msg->get_texttype 'E'.
    endtry.

    go_cols go_alv->get_columns).
    go_cols->set_optimize'X' ).
    go_alv->get_functions)->set_all'X' ).

    go_lay go_alv->get_layout).

    ls_key-report sy-repid.
    go_lay->set_keyvalue ls_key ).

    go_lay->set_save_restriction(
        value if_salv_c_layout=>restrict_none
    ).

    if p_layout is not initial.
      go_lay->set_initial_layoutvalue p_layout ).
    endif.

    go_alv->display).

  endmethod.

endclass.

*&---------------------------------------------------------------------*
*&  Include           ZAK_LAYOUT_SAVE_F01
*&---------------------------------------------------------------------*
*&---------------------------------------------------------------------*
*&      Form  F4_LAYOUT
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
form f4_layout .

  ls_key-report sy-repid.

  try.
      cl_salv_layout_service=>get_layouts(
        exporting
          s_key         ls_key    " Layout Key
        receiving
          t_layout      =  gt_lay   " Layout
      ).

      call function 'F4IF_INT_TABLE_VALUE_REQUEST'
        exporting
          retfield    'LAYOUT'
          dynpprog    sy-repid
          dynpnr      sy-dynnr
          dynprofield 'P_LAYOUT'
          value_org   'S'
        tables
          value_tab   gt_lay.
    catch cx_salv_msg into data(lx_msg2).
      message lx_msg2->get_texttype 'E'.
  endtry.

endform.


  



No comments:

Post a Comment