SAP4TECH

SAP and ABAP Free Tutorials
Menu
  • SAP Technical
    • ABAP Code Snippets
    • ABAP WebDynPro
    • SAP GW
    • SAP IDOC (ALE)
    • SAP PI (XI)
    • SAP Screen Personas
    • SAP Workflow
  • SAP Functional
    • SAP FI
    • SAP FSCM
    • SAP HR
    • SAP SD & SAP MM
    • SAP PM
    • SAP PP
    • SAP PS
    • SAP QM
    • SAP VIM
    • SAP WM
  • SAP Fiori
  • SAP HANA
  • SAP BW
  • SAP CRM
  • SAP SRM
  • SAP4TECH
    • About Us
    • Contact Us
    • Terms & Conditions
    • Privacy and Cookie Policy

SAP4TECH » The fastest method to create ALV ABAP (with Sample ABAP Code)

ABAP Snippets

The fastest method to create ALV ABAP (with Sample ABAP Code)

January 20, 2016 John

Create ALV ABAP is very comment requirment for an ABAP developer.
This post will give the fastest way and the most easy way to create and display ALV in ABAP.

Create ALV in ABAP

The easiest way to create and display ALV in ABAP is to used the class??CL_SALV_TABLE.
This class CL_SALV_TABLE offers a very powerful method to create AVL ABAP.

For example,

  • To initialize the ALV, use the CL_SALV_TABLE=>FACTORY method.
    • no need anymore to build manually your FIELDCALATOG
    • or no need to create a SAP DDIC table/structure to retrieve the FIELDCATALOG
    • it is done by passing juste an internal table.
  • To display, just call the method CL_SALV_TABLE=>DISPLAY
    • no need to create a new screen
    • display method will use the current screen

Create ALV ABAP Sample code

Here a sample code to display an ABAP internal in ALV with:

  • A basic layout setting including ABAP ALV title
  • Optimized Columns size
  • Renaming of Columns text
  • and a tool bar

First, let start with all the ABAP Declaration to make

Data declaration for ABAP ALV
ABAP
1
2
3
4
5
6
7
8
9
10
11
12
"-----------------------------------------------------------"
  " Data declaration for ABAP ALV
  "-----------------------------------------------------------"
  DATA lo_alv               TYPE REF TO cl_salv_table.
  DATA lex_message          TYPE REF TO cx_salv_msg.
  DATA lo_layout_settings   TYPE REF TO cl_salv_layout.
  DATA lo_layout_key        TYPE        salv_s_layout_key.
  DATA lo_columns           TYPE REF TO cl_salv_columns_table.
  DATA lo_column            TYPE REF TO cl_salv_column.
  DATA lex_not_found        TYPE REF TO cx_salv_not_found.
  DATA lo_functions         TYPE REF TO cl_salv_functions_list.
  DATA lo_display_settings  TYPE REF TO cl_salv_display_settings.

Second, the ABAP Code to create and display an ALV the fast way.

Create ALV ABAP the easiest way
ABAP
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
  " Initialize the ALV using the CL_SALV_TABLE=>factory
  "-----------------------------------------------------------"
  TRY.
      cl_salv_table=>factory(
        IMPORTING
          r_salv_table = lo_alv
        CHANGING
          t_table      = lt_itab ).
      "@ the ABAP Internal Table to display in ALV with ABAP
      "@ LT_ITAB to be defined and filled of course
    CATCH cx_salv_msg INTO lex_message.
      " error handling
  ENDTRY.
 
  " Set the ALV Layouts
  "-----------------------------------------------------------"
  lo_layout_settings   = lo_alv->get_layout( ).
  lo_layout_key-report = sy-repid.
  lo_layout_settings->set_key( lo_layout_key ).
  lo_layout_settings->set_save_restriction( if_salv_c_layout=>restrict_none ).
 
  " set the ALV Toolbars
  "-----------------------------------------------------------"
  lo_functions = lo_alv->get_functions( ).
  lo_functions->set_all( ).
 
  " Optimize ALV Columns size
  "-----------------------------------------------------------"
  lo_columns = lo_alv->get_columns( ).
  lo_columns->set_optimize( ).
 
  " Set Zebra Lines display
  "-----------------------------------------------------------"
  lo_display_settings = lo_alv->get_display_settings( ).
  lo_display_settings->set_striped_pattern( if_salv_c_bool_sap=>true ).
 
  " Set ALV Header title
  "-----------------------------------------------------------"
  lo_display_settings->set_list_header( 'Your ALV Title' ).
 
  " Hide ALV Columns from Display.
  "-----------------------------------------------------------"
  TRY.
      lo_column = lo_columns->get_column( 'NAME2' ).
      lo_column->set_visible( if_salv_c_bool_sap=>false ).
    CATCH cx_salv_not_found INTO lex_not_found.
      " write some error handling
  ENDTRY.
 
 
  " Change ALV Columns Name ( Short, medium and Long text)
  "-----------------------------------------------------------"
  TRY.
      lo_column = lo_columns->get_column( 'XREF1' ).
      lo_column->set_short_text( 'Vendor' ).
      lo_column->set_medium_text( 'Vendor' ).
      lo_column->set_long_text( 'Vendor Reference' ).
    CATCH cx_salv_not_found INTO lex_not_found.
      " write some error handling
  ENDTRY.
 
  " Display the ALV in ABAP in the whole main screen
  "-----------------------------------------------------------"
  lo_alv->display( ).

Here it is !

 

You can go further and find all the detail about CL_SALV_TABLE and ALV in ABAP in this excellent post.

Prev Article
Next Article
Tags:ABAP Sample

Related Articles

SAP ABAP
Upload Excel File to internal table in SAP using the …

How to SAP Upload Excel File with TEXT_CONVERT_XLS_TO_SAP

List of WorkItems for a User
You will find here the different methods to extract the …

How to retrieve List of WorkItems for a user ?

Search on SAP4TECH

The Most Populars

  • The Most Important SAP ISU Tables
    The Most Important SAP ISU Tables
  • SAP Users Tables
    SAP Users Tables (for Personal, Logon, and Address Data)
  • List of SAP Purchase Order Tables in SAP MM SAP PO Tables
    SAP Purchase Order Tables: Main PO tables in SAP MM – SAP PO Tables
  • Full list of SAP Movement Types
    SAP Good Movement Types – Full list of SAP Movement Types
  • The Main SAP GL Account Tcodes SAP GL Account Tables
    The Main SAP G/L Account Tcodes & SAP GL Account Tables
  • SAP BOM Tables
    SAP BOM Tables for BOM Header, Items and components and Category

Related Posts

  • SAP Security Transaction Codes
    List of Main SAP Security Transaction Codes ( Security Tcodes in SAP )
  • SAP Process Integration
    Important SAP PI Tcodes (Process Integration Transaction Codes) – SAP XI Tcodes
  • SAP IDOC Qualifier
    All what you need to know about SAP IDOC Qualifier Overview
  • SAP Warehouse Management e1499171805310
    SAP BOM Category Details
  • Find String in any SAP ABAP Code source
    Different ways to Find any String or Hard Coded Value in SAP ABAP Code

SAP4TECH

SAP and ABAP Free Tutorials

Trending Posts

  • The Most Important SAP ISU Tables
  • SAP Users Tables (for Personal, Logon, and Address Data)
  • SAP Purchase Order Tables: Main PO tables in SAP MM – SAP PO Tables
  • SAP Good Movement Types – Full list of SAP Movement Types
  • The Main SAP G/L Account Tcodes & SAP GL Account Tables
  • SAP BOM Tables for BOM Header, Items and components and Category

The Most Recents

  • The Most Important SAP ISU Tables
  • SAP Fiori 3 UX and Design of SAP Fiori Apps for SAP S/4HANA, SAP TechEd Lecture
  • The Main SAP Dunning Transaction Codes
  • SAP Accounts Payable Tcodes & Accounts Receivable Tcodes ( SAP AP Tcodes & SAP AR Tcodes)
  • The Most Important SAP Payment Terms Tables (ZTERM, Text…)

Search

SAP Tutorials by Topics

  • SAP Tables
  • SAP Tcodes
  • SAP BAPI
  • ABAP Snippets
  • Top SAP Courses
  • Top SAP Books
Copyright © 2021 SAP4TECH

Ad Blocker Detected

Our website is made possible by displaying online advertisements to our visitors. Please consider supporting us by disabling your ad blocker.

Refresh
This website uses cookies to improve your experience. We'll assume you accept this policy as long as you are using this websiteAcceptView Policy