What is the difference between free clear and refresh in sap abap.

When you define a variable or a table in a program. And execute it the processor will allocate appropriate memory to the variable hope you know this.

Ex:

DATA : Variable(1) type c.

Then the processor will allocate memory(With memory ID Some say 412536) equal to size 1 character size.

Then if you assign a value for the above variable like

Variable = ‘X’. the variable or memory id 412536 contains this ‘X’ value.

If you write:

1. FREE Variable.

The memory allocated is refreshed and freed. Means after this statement the memory to the variable is no more. It is freed from the program and can be used for some other program are purpose.

2. REFRESH i_tab.

Used for table to clear all the entries from the internal table body. but sill memory allocation exist and you can use some other with in the program.

3. CLEAR variable .

it clears the memory of the variable i.e. variable will be empty.

4.DELETE variable or I_tab index i etc.

It deletes a specific value from a I_tab body based on condition.

what is the use of collect in SAP

APPEND: This keyword will append the current contents of the table header or the structure to the end of the table as a new record.. This will not affect the any existing record of the table.

COLLECT : This keyword is used for summation purposes.. when you collect a record to an internal table, it will find that is the table already have any record with the same key values? If yes, then it will add all the numeric values of the new record to the existing record without changing the non numeric values.

How to use Sorted and hashed internal tables

Has both unique and non unique keys ( unique means only one key will be there, non-unique multiple entries for the key

key must be specified, Example

DATA : it_mat type sorted table of ty_mat with unique key matnr

DATA : it_mat type sorted table of ty_mat with unique key matnr

DATA : it_mat type sorted table of ty_mat with non-unique key matnr

example:-

*&---------------------------------------------------------------------*
*& Report  ZSOTED_TABLE
*&
*&---------------------------------------------------------------------*
*&
*&
*&---------------------------------------------------------------------*

REPORT ZSOTED_TABLE.

TYPES : begin of ty_tab,
am TYPE i,
cou TYPE i,
END OF ty_tab.

DATA : it_tab TYPE SORTED TABLE OF ty_tab WITH UNIQUE KEY cou,
wa_tab TYPE                 ty_tab.

do 3 TIMES.
wa_tab-am = sy-index.
wa_tab-cou = sy-index + 1.
INSERT wa_tab INTO TABLE it_tab.
ENDDO.

wa_tab-am = sy-index.
wa_tab-cou = '3'.
INSERT wa_tab INTO TABLE it_tab.

loop at it_tab INTO wa_tab.
WRITE : / wa_tab-am,
wa_tab-cou.
ENDLOOP.

here duplicate entries are not inserted

Parallel cursor method

REPORT ZPARALLEL_CURSOR.

DATA: it_ekko TYPE  TABLE OF ekko,
wa_ekko TYPE ekko,
it_ekpo TYPE  TABLE OF ekpo,
wa_ekpo TYPE ekpo.
DATA: lv_tabix TYPE sy-tabix.

SELECT * from ekko INTO TABLE it_ekko UP TO 50 ROWS.
SELECT * from ekpo INTO TABLE it_ekpo UP TO 50 ROWS.

SORT : it_ekko by ebeln,
it_ekpo by ebeln.

LOOP at it_ekko INTO wa_ekko.
READ TABLE it_ekpo INTO wa_ekpo WITH KEY
ebeln = wa_ekko-ebeln BINARY SEARCH.
if sy-subrc EQ 0.
lv_tabix = sy-tabix.
ENDIF.
LOOP at it_ekpo INTO wa_ekpo FROM lv_tabix.
WRITE : / wa_ekpo-ebeln.
ENDLOOP.
ENDLOOP.

Internal table operations in sap-abap

APPEND statement is used to append or add a record from work area to internal table, the new record will be added at the end of the internal table.

INSERT statement is used to insert or add a record from work area into internal table at the specified location

SORT is used to sort a Internal table data in ascending order or descending order, by default it will sort the data in ascending order. In addition to this we can able to sort data based on specified fields.

DESCRIBE TABLE is used to count the no of records in a internal table

READ TABLE WITH KEY .. BINARY SEARCH is used to read a single record from an internal table into work area specified by field name and field value .

BINARY SEARCH is a search mechanism which is used to read a record from internal table into work area very fast, the functionality of binary search it divides the into parts and searches, for full details Binary Search mechanism in SAP ABAPThe internal table must be sorted in ascending order before using binary search.

READ TABLE WITH INDEX is used to read a single record from an internal table into work area specified by index.

MODIFY is used to modify a single or multiple internal table records based on condition

TRANSPORTING is a keyword which is used to specify a list of fields to be modified instead of all fields.

DELETE is used to delete single or multiple records from an internal table from work area based on some condition

DELETE ADJACENT DUPLICATES is used to delete delete duplicate records which are adjacent to each-other.Pre-requisite for this is the internal table must be sorted in ascending order

APPEND LINES OF is used to append multiple records to an internal table from another internal table.

INSERT LINES OF is used to INSERT multiple records to an internal table from another internal table at the specified location.

MOVE keyword is used to move one internal table data to another.

Inner Join in SAP ABAP

*&---------------------------------------------------------------------*
*& Report  ZTEST_INNER_JOIN
*&
*&---------------------------------------------------------------------*
*&
*&
*&---------------------------------------------------------------------*

REPORT ZTEST_INNER_JOIN.

TYPES : BEGIN OF TY_MARA,
MATNR TYPE MATNR,
MAKTX TYPE MAKTX,
MTART TYPE MTART,
END OF TY_MARA.

DATA  : IT_MARA TYPE TABLE OF TY_MARA,
WA_MARA TYPE          TY_MARA.

SELECT MARA~MATNR
MARA~MTART
MAKT~MAKTX
INTO TABLE IT_MARA
FROM MARA
INNER JOIN MAKT
ON ( MARA~MATNR = MAKT~MATNR )
UP TO 10 ROWS.

LOOP AT IT_MARA INTO WA_MARA.
WRITE : / WA_MARA-MATNR,
WA_MARA-MAKTX,
WA_MARA-MTART.
ENDLOOP.

Difference between For all entries and inner join

Inner Join : here you are going to use two database tables having common fields, on the basis of which you will fetch the data.

For all entries: Here at first you are going to fetch data from Db1 into itab1 for specified condition. then again you will use one more select query to fetch the values from DB2 for the corresponding entries in internal table itab1. Some drawbacks of for all entries are :-

1. duplicate rows are automatically removed

2. if the itab used in the clause is empty , all the rows in the source table will be selected .

3. performance degradation when using the clause on big tables.

Having statement is only used with group by

Native SQL statements begin with EXEC and ends with ENDEXEC.

to exit the debugger /hx

Modularization techniques

subroutines

Function Modules

Methods

events

dialog modules

How to use FM F4IF_INT_TABLE_VALUE_REQUEST

This FM is used to display the values when u press F4 on a selection field.

https://wiki.scn.sap.com/wiki/display/ABAP/ON+VALUE-REQUEST+event

Internal Table events or control break statements

AT FIRST

AT LAST

AT NEW

AT END OF

ON CHANGE OF

Events in a classical report

Load-of-praogram

This event is used to load program into memory for execution and this is the first event in execution sequence.

Initialization

This event is used to initialize variables, screen default values and other default actions.

At Selection-Screen output

By using this event we can manipulate dynamic selection-screen changes.

At Selection-Screen on field

This event is used to validate a single selection-screen input parameter.

At Selection-Screen on value request

This event is used to provide value help ( field help ) for a input field.

At Selection-Screen on help request

By using this event we can provide F1 help for a input field.

At Selection-Screen

This event is used to validate multiple input fields

Start-of-Selection

This is default event which is used to write actual business logic.

End-of-Selection

We can use this event just to state that start-of-selection is ended, this event is used with logical databases, logical databases are in HR ABAP only. In normal ABAP we don`t have much importance .

Top-of-Page

This event prints constant heading for all pages.

End-of-Page

This event prints constant footer for all pages.

Before using this event, we need to reserve some lines for displaying footer.

Below is an Example:-

*&---------------------------------------------------------------------*
*& Report  Z_EVENT_CLASSICAL
*&
*&---------------------------------------------------------------------*
*&
*&
*&---------------------------------------------------------------------*

REPORT Z_EVENT_CLASSICAL LINE-COUNT 34(2).
TABLES : mara.
TYPES: BEGIN OF TY_MARA,
MATNR TYPE MARA-MATNR,
ERSDA TYPE MARA-ERSDA,
MTART TYPE MARA-MTART,
MBRSH TYPE MARA-MBRSH,
MATKL TYPE MARA-MATKL,
MEINS TYPE MARA-MEINS,
END OF TY_MARA.
DATA: IT_MARA TYPE TABLE OF TY_MARA. "material out put internal table
DATA:  WA_MARA TYPE TY_MARA. " work area

DATA: LV_MTART TYPE MARA-MTART.
DATA: LV_START_TIME TYPE SY-UZEIT.
DATA: LV_END_TIME TYPE SY-UZEIT.
SELECTION-SCREEN BEGIN OF BLOCK B1 WITH FRAME TITLE TEXT-001. "designs a block just for design double click on TEXT-001 to add text

SELECT-OPTIONS: S_MATNR FOR mara-MATNR. " Material range input
PARAMETERS: P_MTART TYPE MARA-MTART. "material type input

SELECTION-SCREEN END OF BLOCK B1.

PARAMETERS  P_DLOAD AS CHECKBOX USER-COMMAND UC1.
PARAMETERS  P_FILE TYPE RLGRAP-FILENAME MODIF ID DLD.
PARAMETERS P_LIMIT TYPE I.

LOAD-OF-PROGRAM.
lv_start_time = sy-uzeit.

INITIALIZATION. "triggers second
P_MTART = 'FERT'. "MATERIAL TYPE DEFAULT VALUE
P_LIMIT = '50'. "Limit rows to 50

AT SELECTION-SCREEN OUTPUT. "For dynamic modifications
IF P_DLOAD IS INITIAL.
LOOP AT SCREEN.
CHECK SCREEN-GROUP1 = 'DLD'.
SCREEN-INPUT = '0'.
MODIFY SCREEN.
ENDLOOP.
ENDIF.

AT SELECTION-SCREEN ON P_MTART. " Validate single input field at selection-screen is an alternative and good see http://www.sapnuts.com/courses/core-abap/classical-reports/selection-screen-event.html

AT SELECTION-SCREEN ON VALUE-REQUEST FOR P_MTART. "This event is not required here will use in the next lesson
PERFORM MTART_VALUE_HELP.

AT SELECTION-SCREEN ON VALUE-REQUEST FOR P_FILE.
PERFORM FILE_VALUE_HELP.

AT SELECTION-SCREEN ON HELP-REQUEST FOR P_MTART. " Provide help request F1 help.
PERFORM MTART_HELP.

AT SELECTION-SCREEN.
PERFORM VALIDATE_INPUTS.

START-OF-SELECTION.
PERFORM GET_MATERIALS.

END-OF-SELECTION.
LV_END_TIME = SY-UZEIT.
PERFORM DISPLAY_OUTPUT.
IF P_DLOAD = 'X'.
PERFORM DOWNLOAD_DATA.
ENDIF.

TOP-OF-PAGE.
WRITE: 'Material Details ' COLOR 2.

END-OF-PAGE.
WRITE:'The above materials are active materials available in database' COLOR 3.
WRITE: 'Start time'.
WRITE: LV_START_TIME.
WRITE: 'End time'.
WRITE: LV_END_TIME.

FORM VALIDATE_INPUTS.
IF S_MATNR IS INITIAL OR P_MTART IS INITIAL.

MESSAGE 'Please enter required inputs' TYPE 'E'.

ELSE.

***Validate material type is valid or not
SELECT MTART FROM MARA INTO LV_MTART
UP TO 1 ROWS WHERE MTART = P_MTART.
ENDSELECT.
IF LV_MTART IS INITIAL.
MESSAGE 'Material type is not available in MARA' TYPE 'E'.
ENDIF.
ENDIF.
ENDFORM.                    " VALIDATE_INPUTS

FORM GET_MATERIALS.
SELECT MATNR ERSDA MTART MBRSH MATKL MEINS FROM MARA
INTO TABLE IT_MARA
UP TO P_LIMIT ROWS
WHERE MATNR IN S_MATNR AND MTART = P_MTART.

ENDFORM.                    " GET_MATERIALS

FORM DISPLAY_OUTPUT .
IF IT_MARA IS NOT INITIAL.
LOOP AT IT_MARA INTO WA_MARA.

WRITE :/ WA_MARA-MATNR, WA_MARA-ERSDA, WA_MARA-MTART, WA_MARA-MBRSH, WA_MARA-MATKL, WA_MARA-MEINS .

ENDLOOP.
ELSE.
WRITE :'No Data Found for your Query'.
ENDIF.
ENDFORM.                    " DISPLAY_OUTPUT

FORM MTART_HELP.
MESSAGE 'Enter a Material Type ' TYPE 'I'.
ENDFORM.                    " MTART_HELP

FORM MTART_VALUE_HELP.
*  MESSAGE 'Material type input ex: FERT' TYPE 'I'.
DATA : it_mara1 TYPE TABLE OF ty_mara.
DATA: IT_RETURN LIKE DDSHRETVAL OCCURS 0 WITH HEADER LINE.
SELECT mtart FROM mara
INTO TABLE it_mara1
WHERE matnr = S_matnr-LOW.

CALL FUNCTION 'F4IF_INT_TABLE_VALUE_REQUEST'
EXPORTING
*       DDIC_STRUCTURE         = ' '
retfield               = 'P_MTART'
*       PVALKEY                = ' '
*       DYNPPROG               = ' '
*       DYNPNR                 = ' '
*       DYNPROFIELD            = ' '
*       STEPL                  = 0
*       WINDOW_TITLE           =
*       VALUE                  = ' '
VALUE_ORG              = 'S'
*       MULTIPLE_CHOICE        = ' '
*       DISPLAY                = ' '
*       CALLBACK_PROGRAM       = sy-repid
*       CALLBACK_FORM          = ' '
*       CALLBACK_METHOD        =
*       MARK_TAB               =
*     IMPORTING
*       USER_RESET             =
tables
value_tab              = it_mara1
*       FIELD_TAB              =
RETURN_TAB             = it_return
*       DYNPFLD_MAPPING        =
*     EXCEPTIONS
*       PARAMETER_ERROR        = 1
*       NO_VALUES_FOUND        = 2
*       OTHERS                 = 3
.
IF sy-subrc <> 0.
* Implement suitable error handling here
ENDIF.

ENDFORM.                    " MTART_VSLUE_HELP

FORM DOWNLOAD_DATA .
DATA : LV_FILE TYPE STRING .
LV_FILE = P_FILE .
CALL FUNCTION 'GUI_DOWNLOAD'
EXPORTING
*   BIN_FILESIZE                    =
FILENAME                        = LV_FILE
FILETYPE                        = 'ASC'
*   APPEND                          = ' '
WRITE_FIELD_SEPARATOR           = 'X'
*   HEADER                          = '00'
*   TRUNC_TRAILING_BLANKS           = ' '
*   WRITE_LF                        = 'X'
*   COL_SELECT                      = ' '
*   COL_SELECT_MASK                 = ' '
*   DAT_MODE                        = ' '
*   CONFIRM_OVERWRITE               = ' '
*   NO_AUTH_CHECK                   = ' '
*   CODEPAGE                        = ' '
*   IGNORE_CERR                     = ABAP_TRUE
*   REPLACEMENT                     = '#'
*   WRITE_BOM                       = ' '
*   TRUNC_TRAILING_BLANKS_EOL       = 'X'
*   WK1_N_FORMAT                    = ' '
*   WK1_N_SIZE                      = ' '
*   WK1_T_FORMAT                    = ' '
*   WK1_T_SIZE                      = ' '
*   WRITE_LF_AFTER_LAST_LINE        = ABAP_TRUE
*   SHOW_TRANSFER_STATUS            = ABAP_TRUE
* IMPORTING
*   FILELENGTH                      =
TABLES
DATA_TAB                        = IT_MARA
*   FIELDNAMES                      =
* EXCEPTIONS
*   FILE_WRITE_ERROR                = 1
*   NO_BATCH                        = 2
*   GUI_REFUSE_FILETRANSFER         = 3
*   INVALID_TYPE                    = 4
*   NO_AUTHORITY                    = 5
*   UNKNOWN_ERROR                   = 6
*   HEADER_NOT_ALLOWED              = 7
*   SEPARATOR_NOT_ALLOWED           = 8
*   FILESIZE_NOT_ALLOWED            = 9
*   HEADER_TOO_LONG                 = 10
*   DP_ERROR_CREATE                 = 11
*   DP_ERROR_SEND                   = 12
*   DP_ERROR_WRITE                  = 13
*   UNKNOWN_DP_ERROR                = 14
*   ACCESS_DENIED                   = 15
*   DP_OUT_OF_MEMORY                = 16
*   DISK_FULL                       = 17
*   DP_TIMEOUT                      = 18
*   FILE_NOT_FOUND                  = 19
*   DATAPROVIDER_EXCEPTION          = 20
*   CONTROL_FLUSH_ERROR             = 21
*   OTHERS                          = 22
.
IF SY-SUBRC = 0.
WRITE :/ 'Data downloaded to'.
WRITE:P_FILE.
ENDIF.

ENDFORM.                    " DOWNLOAD_DATA

FORM FILE_VALUE_HELP.

CALL FUNCTION 'F4_FILENAME'
EXPORTING
FIELD_NAME = 'P_FILE'
IMPORTING
FILE_NAME  = P_FILE.
ENDFORM.                    " FILE_VALUE_HELP

Events in a interactive Report

At Line-Selection

This event will trigger whenever the user double click on any list line.

At User Command

This event will trigger whenever user clicks on any custom buttons of the GUI.

At PF Status

This event will trigger whenever user clicks on any function buttons.

Top Of Page During line selection

This is used to print heading for secondary lists in interactive reports.

Techniques used in interactive reporting

Hide area

It is a key word which is used to store the data into a temporary memory call as HIDE area.

Functionality of HIDE is
  • Whenever the user uses the HIDE statement, the data will be stored in ‘HIDE’ area along with line numbers.
  • Whenever user double clicks on any list line the system takes the line number and checks the HIDE area for the corresponding data in that particular line, then the data will be returned to the HIDE variables.
GET CURSOR

This key word is used to read the field name and field value where the mouse cursor is placed or double click action is raised. It dosen`t use hide area.

Using AT-USER-COMMAND

*&---------------------------------------------------------------------*
*& Report  ZINTERACTIVE_1
*&
*&---------------------------------------------------------------------*
*&
*&
*&---------------------------------------------------------------------*

REPORT ZINTERACTIVE_1.

TYPES : BEGIN OF ty_vbrk,
vbeln TYPE vbeln,
fktyp TYPE fktyp,
land1 TYPE land1,
kunag TYPE kunag,
END OF ty_vbrk,

BEGIN OF ty_vbrp,
vbeln TYPE vbeln,
posnr TYPE posnr,
uepos TYPE uepos,
END OF ty_vbrp,

BEGIN OF ty_kna1,
kunnr TYPE kunnr,
land1 TYPE land1,
END OF ty_kna1.

DATA : it_vbrk TYPE TABLE OF ty_vbrk,
wa_vbrk TYPE          ty_vbrk,

it_vbrp TYPE TABLE OF ty_vbrp,
wa_vbrp TYPE          ty_vbrp,

it_kna1 TYPE TABLE OF ty_kna1,
wa_kna1 TYPE          ty_kna1.

SELECT-OPTIONS : s_vbeln FOR wa_vbrk-vbeln.

START-OF-SELECTION.
SET PF-STATUS 'SHIV'.
PERFORM get_vbrk.
PERFORM get_vbrp.
PERFORM get_kna1.
PERFORM display_vbrk.
PERFORM display_vbrp.
PERFORM display_kna1.

end-of-SELECTION.
*&---------------------------------------------------------------------*
*&      Form  GET_VBRK
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM get_vbrk .
SELECT vbeln
fktyp
land1
kunag FROM vbrk
INTO TABLE it_vbrk
WHERE vbeln In  s_vbeln.
ENDFORM.                    " GET_VBRK
*&---------------------------------------------------------------------*
*&      Form  GET_VBRP
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM get_vbrp .
SELECT vbeln
posnr
uepos FROM vbrp
INTO TABLE it_vbrp
*       FOR ALL ENTRIES,. IN IT_VBRK
WHERE vbeln = wa_vbrk-VBELN.
ENDFORM.                    " GET_VBRP
*&---------------------------------------------------------------------*
*&      Form  GET_KNA1
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM get_kna1 .
SELECT kunnr
land1 FROM kna1
INTO TABLE it_kna1
WHERE kunnr = wa_vbrk-kunag.
ENDFORM.                    " GET_KNA1
*&---------------------------------------------------------------------*
*&      Form  DISPLAY_KNA1
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM display_kna1 .
LOOP at it_kna1 into wa_kna1.
WRITE : / wa_kna1-kunnr,
wa_kna1-land1.
ENDLOOP.
ENDFORM.                    " DISPLAY_KNA1
*&---------------------------------------------------------------------*
*&      Form  DISPLAY_VBRK
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM display_vbrk .
LOOP at it_vbrk INTO wa_vbrk.
WRITE : / wa_vbrk-vbeln,
wa_vbrk-fktyp,
wa_vbrk-land1,
wa_vbrk-kunag.
HIDE wa_vbrk-vbeln.
ENDLOOP.
ENDFORM.                    " DISPLAY_VBRK
*&---------------------------------------------------------------------*
*&      Form  DISPLAY_VBRP
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM display_vbrp .
LOOP at it_vbrp INTO wa_vbrp.
WRITE : / wa_vbrp-vbeln,
wa_vbrp-posnr,
wa_vbrp-uepos.
HIDE wa_vbrp-vbeln.
ENDLOOP.
ENDFORM.                    " DISPLAY_VBRP

at USER-COMMAND.
CASE sy-ucomm.
WHEN 'VBRK'.
PERFORM get_vbrk.
PERFORM display_vbrk.
WHEN 'VBRP'.
PERFORM get_vbrp.
PERFORM display_vbrp.
WHEN OTHERS.
PERFORM get_kna1.
PERFORM display_kna1.
ENDCASE.

NOTE: you need to create a PF-status by giving some name ( in CAPITALS ) and you need to assign the function keys.

ALV

REUSE_ALV_GRID_DISPLAY. “Display ALV grid format
REUSE_ALV_LIST_DISPLAY. “Display ALV List format
REUSE_ALV_COMMENTARY_WRITE. “Display Top of page, logo, etc.
REUSE_ALV_FIELDCATELOGUE_MERGE. “Used to generate field catalogue
REUSE_ALV_EVENTS_GET. “Use events in ALV
REUSE_ALV_HEIRARCHY_LIST_DISPLAY. “Display ALV Hierarchy

Field Catalog

Field catalog is an internal table which is used to pass a list of fields to display in ALV report, we can set different properties to fields which are going to display in ALV.

Type Group

It is a data dictionary object which contains all the reusable user-defined types.

Example for a type group is SLIS, which contains all the user-defined types for developing ALV reports.

TYPE-POOLS is a keyword which is used to assign the type-group to a ALV report .

when we want to display some of the fields from a particular table we will use field-catalog.

How to edit and ALV report

you can use the type group SLIS property SLIS_LAYOUT_ALV, below are some of the properties. Particularly to edit feature use the below highlighted property.

*  WA_LAYOUT-ZEBRA = 'X' .
WA_LAYOUT-COLWIDTH_OPTIMIZE 'X' .
  WA_LAYOUT-EDIT 'X' .
*  WA_LAYOUT-NO_VLINE = 'X' .
*  WA_LAYOUT-NO_HLINE = 'X' .
DATA : I_FCAT TYPE SLIS_T_FIELDCAT_ALV ."internal table for field catalog
DATA : WA_FCAT TYPE SLIS_FIELDCAT_ALV ." workarea for fieldcatalog
DATA : WA_LAYOUT TYPE SLIS_LAYOUT_ALV ." for layout

ALV EVENTS

We use function module REUSE_ALV_EVENTS_GET  to get events and use in our report.

EXAMPLE:-

REPORT ZALV_TEST_1.
TABLES : vbap.
TYPES : BEGIN OF TY_VBAP,
VBELN TYPE VBAP-VBELN,
POSNR TYPE VBAP-POSNR,
MATNR TYPE VBAP-MATNR,
NETWR TYPE VBAP-NETWR,
END OF TY_VBAP.
DATA : It_VBAP TYPE TABLE OF TY_VBAP .
DATA : WA_VBAP TYPE TY_VBAP .

DATA : i_fcat TYPE slis_t_fieldcat_alv,
wa_fcat like LINE OF i_fcat,
i_sort TYPE slis_t_sortinfo_alv,
wa_sort LIKE LINE OF i_sort,
wa_layout TYPE slis_layout_alv.
DATA : I_EVENTS TYPE SLIS_T_EVENT .
DATA : WA_EVENTS LIKE LINE OF I_EVENTS .
DATA : I_HEADING TYPE SLIS_T_LISTHEADER .
DATA : WA_HEADING LIKE LINE OF I_HEADING .
SELECT-OPTIONS : s_vbeln for vbap-vbeln.

PERFORM get_data.
PERFORM fieldcat.
PERFORM sort.
PERFORM GET_EVENTS .
PERFORM get_disp.
*&---------------------------------------------------------------------*
*&      Form  GET_DATA
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM get_data .
SELECT VBELN
POSNR
MATNR
NETWR
FROM vbap
INTO TABLE it_vbap
WHERE vbeln in s_vbeln.
ENDFORM.                    " GET_DATA
*&---------------------------------------------------------------------*
*&      Form  FIELDCAT
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM fieldcat .
wa_fcat-fieldname = 'VBELN'.
wa_fcat-col_pos = '1'.
wa_fcat-tabname = 'it_vbap'.
wa_fcat-seltext_m = 'sd_no'.
wa_fcat-key = 'x'.
APPEND wa_fcat to i_fcat.

WA_FCAT-COL_POS = '2' .
WA_FCAT-FIELDNAME = 'POSNR' .
WA_FCAT-TABNAME = 'I_VBAP' .
WA_FCAT-SELTEXT_M = 'ITEMNO' .
*  WA_FCAT-NO_OUT = 'X' .
WA_FCAT-HOTSPOT = 'X' .
APPEND WA_FCAT TO I_FCAT .
CLEAR WA_FCAT .

WA_FCAT-COL_POS = '3' .
WA_FCAT-FIELDNAME = 'MATNR' .
WA_FCAT-TABNAME = 'I_VBAP' .
WA_FCAT-SELTEXT_M = 'MATERIALNO' .
WA_FCAT-EDIT = 'X' .
APPEND WA_FCAT TO I_FCAT .
CLEAR WA_FCAT .

WA_FCAT-COL_POS = '4' .
WA_FCAT-FIELDNAME = 'NETWR' .
WA_FCAT-TABNAME = 'I_VBAP' .
WA_FCAT-SELTEXT_M = 'NETPRICE' .
WA_FCAT-EMPHASIZE = 'C610'.
WA_FCAT-DO_SUM = 'X' .
APPEND WA_FCAT TO I_FCAT .
CLEAR WA_FCAT .
ENDFORM.                    " FIELDCAT
*&---------------------------------------------------------------------*
*&      Form  SORT
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM sort .
wa_sort-fieldname = 'VBELN'.
WA_SORT-UP = 'X'.
WA_SORT-SUBTOT = 'X '.
APPEND WA_SORT TO I_SORT .
ENDFORM.                    " SORT
*&---------------------------------------------------------------------*
*&      Form  GET_DISP
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM get_disp .
CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
EXPORTING
*   I_INTERFACE_CHECK                 = ' '
*   I_BYPASSING_BUFFER                = ' '
*   I_BUFFER_ACTIVE                   = ' '
I_CALLBACK_PROGRAM                = sy-repid
*   I_CALLBACK_PF_STATUS_SET          = ' '
*   I_CALLBACK_USER_COMMAND           = ' '
*   I_CALLBACK_TOP_OF_PAGE            = ' '
*   I_CALLBACK_HTML_TOP_OF_PAGE       = ' '
*   I_CALLBACK_HTML_END_OF_LIST       = ' '
*   I_STRUCTURE_NAME                  = i_
*   I_BACKGROUND_ID                   = ' '
*   I_GRID_TITLE                      =
*   I_GRID_SETTINGS                   =
*   IS_LAYOUT                         =
IT_FIELDCAT                       = i_fcat
*   IT_EXCLUDING                      =
*   IT_SPECIAL_GROUPS                 =
IT_SORT                           = i_sort
*   IT_FILTER                         =
*   IS_SEL_HIDE                       =
I_DEFAULT                         = 'S'
*   I_SAVE                            = ' '
*   IS_VARIANT                        =
IT_EVENTS                         = i_events
*   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
*   I_HTML_HEIGHT_TOP                 = 0
*   I_HTML_HEIGHT_END                 = 0
*   IT_ALV_GRAPHICS                   =
*   IT_HYPERLINK                      =
*   IT_ADD_FIELDCAT                   =
*   IT_EXCEPT_QINFO                   =
*   IR_SALV_FULLSCREEN_ADAPTER        =
* IMPORTING
*   E_EXIT_CAUSED_BY_CALLER           =
*   ES_EXIT_CAUSED_BY_USER            =
TABLES
t_outtab                          = it_vbap
* EXCEPTIONS
*   PROGRAM_ERROR                     = 1
*   OTHERS                            = 2
.
IF sy-subrc <> 0.
* Implement suitable error handling here
ENDIF.

ENDFORM.                    " GET_DISP
*&---------------------------------------------------------------------*
*&      Form  GET_EVENTS
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM get_events .

CALL FUNCTION 'REUSE_ALV_EVENTS_GET'
* EXPORTING
*   I_LIST_TYPE           = 0
IMPORTING
ET_EVENTS             = I_EVENTS .

READ TABLE I_EVENTS INTO WA_EVENTS WITH KEY NAME = 'TOP_OF_PAGE' .
WA_EVENTS-FORM = 'FORM_TOP_OF_PAGE' .
MODIFY  I_EVENTS FROM WA_EVENTS INDEX SY-TABIX .

ENDFORM.                    " GET_EVENTS

FORM FORM_TOP_OF_PAGE.
wa_heading-typ = 'H'.
wa_heading-info = 'sales order report'.
APPEND WA_HEADING TO I_HEADING .

WA_HEADING-TYP = 'S' .
WA_HEADING-KEY = 'USERNAME' .
WA_HEADING-INFO = SY-UNAME .
APPEND WA_HEADING TO I_HEADING .

WA_HEADING-TYP = 'A' .
WA_HEADING-KEY = 'DATE' .
WA_HEADING-INFO = SY-DATUM .
APPEND WA_HEADING TO I_HEADING .

CALL FUNCTION 'REUSE_ALV_COMMENTARY_WRITE'
EXPORTING
it_list_commentary       = i_heading
*     I_LOGO                   =
*     I_END_OF_LIST_GRID       =
*     I_ALV_FORM               =
.

ENDFORM.

BLOCK LIST ALV

Blocked list ALV is used to display multiple ALV`s on the same screen with blocks.

List of Function Modules used for blocked list ALV

  • REUSE_ALV_BLOCK_LIST_INIT
  • REUSE_ALV_BLOCK_LIST_APPEND
  • REUSE_ALV_BLOCK_LIST_DISPLAY

REUSE_ALV_BLOCK_LIST_INIT: is used to initialize blocked list ALV.

REUSE_ALV_BLOCK_LIST_APPEND: is used to add blocked list ALV’s(we can add multiple).

REUSE_ALV_BLOCK_LIST_DISPLAY: is used to display blocked list ALV.

Dynamic ALV Display

*&---------------------------------------------------------------------*
*& Report  ZALV_DYNAMIC_TABLE
*&
*&---------------------------------------------------------------------*
*&
*&
*&---------------------------------------------------------------------*

REPORT ZALV_DYNAMIC_TABLE.
DATA : IT_FCAT TYPE LVC_T_FCAT.
PARAMETERS: P_TABLE TYPE DD02L-TABNAME.
DATA : WA_TABLE TYPE DD02L.
*FIELD-SYMBOLS :<fs_tab> TYPE ANY TABLE.

AT SELECTION-SCREEN.
IF P_TABLE IS NOT INITIAL.
SELECT SINGLE * FROM DD02L INTO WA_TABLE WHERE TABNAME = P_TABLE AND TABCLASS = 'TRANSP'.
ELSE.
MESSAGE 'Please enter a table' TYPE 'E'.
STOP.
ENDIF.
IF WA_TABLE IS INITIAL.
MESSAGE 'Table Dosent exit or is not transparent table' TYPE 'E'.
STOP.
ENDIF.

START-OF-SELECTION.

CALL FUNCTION 'LVC_FIELDCATALOG_MERGE'
EXPORTING
*     I_BUFFER_ACTIVE              =
I_STRUCTURE_NAME             = p_table
*     I_CLIENT_NEVER_DISPLAY       = 'X'
*     I_BYPASSING_BUFFER           =
*     I_INTERNAL_TABNAME           =
CHANGING
ct_fieldcat                  = it_fcat.
*   EXCEPTIONS
*     INCONSISTENT_INTERFACE       = 1
*     PROGRAM_ERROR                = 2
*     OTHERS                       = 3
.
IF sy-subrc <> 0.
* Implement suitable error handling here
ENDIF.
DATA : fs_tab TYPE REF TO data.
CALL METHOD cl_alv_table_create=>create_dynamic_table
EXPORTING
*       i_style_table             =
it_fieldcatalog           = it_fcat
*       i_length_in_byte          =
IMPORTING
ep_table                  = fs_tab
*       e_style_fname             =
*     EXCEPTIONS
*       generate_subpool_dir_full = 1
*       others                    = 2
.
IF sy-subrc <> 0.
*    Implement suitable error handling here
ENDIF.

FIELD-SYMBOLS :<fs_tab> TYPE STANDARD TABLE.
ASSIGN fs_tab->* to <fs_tab>.
SELECT * FROM (P_TABLE) INTO CORRESPONDING FIELDS OF TABLE <FS_TAB> UP TO 50 ROWS.
CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
EXPORTING
I_STRUCTURE_NAME = P_TABLE
TABLES
T_OUTTAB         = <FS_TAB>.
IF SY-SUBRC <> 0.
* Implement suitable error handling here
ENDIF.

Interactive ALV

It will be same as ALV EVENTS mentioned above only the call back program parameter will change while calling the FM REUSE_ALV_GRID_DISPLAY. we will pass the user command and perform the action accordingly.

Please see the highlighted part which is different from the program mentioned for ALV EVENTS

Example:-

**&---------------------------------------------------------------------*
**& Report  ZALV_TEST_1
**&
**&---------------------------------------------------------------------*
**&
**&
**&---------------------------------------------------------------------*
*
REPORT ZALV_TEST_1.
TABLES : vbap.
TYPES : BEGIN OF TY_VBAP,
VBELN TYPE VBAP-VBELN,
POSNR TYPE VBAP-POSNR,
MATNR TYPE VBAP-MATNR,
NETWR TYPE VBAP-NETWR,
END OF TY_VBAP.
DATA : It_VBAP TYPE TABLE OF TY_VBAP .
DATA : WA_VBAP TYPE TY_VBAP .

DATA : i_fcat TYPE slis_t_fieldcat_alv,
wa_fcat like LINE OF i_fcat,
i_sort TYPE slis_t_sortinfo_alv,
wa_sort LIKE LINE OF i_sort,
wa_layout TYPE slis_layout_alv.
DATA : I_EVENTS TYPE SLIS_T_EVENT .
DATA : WA_EVENTS LIKE LINE OF I_EVENTS .
DATA : I_HEADING TYPE SLIS_T_LISTHEADER .
DATA : WA_HEADING LIKE LINE OF I_HEADING .
SELECT-OPTIONS : s_vbeln for vbap-vbeln.
*PARAMETER : s_vbeln TYPE vbap-vbeln.

PERFORM get_data.
PERFORM fieldcat.
PERFORM sort.
PERFORM GET_EVENTS .
PERFORM get_disp.
*&---------------------------------------------------------------------*
*&      Form  GET_DATA
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM get_data .
SELECT VBELN
POSNR
MATNR
NETWR
FROM vbap
INTO TABLE it_vbap
WHERE vbeln in s_vbeln.
ENDFORM.                    " GET_DATA
*&---------------------------------------------------------------------*
*&      Form  FIELDCAT
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM fieldcat .
wa_fcat-fieldname = 'VBELN'.
wa_fcat-col_pos = '1'.
wa_fcat-tabname = 'it_vbap'.
wa_fcat-seltext_m = 'sd_no'.
wa_fcat-key = 'x'.
WA_FCAT-HOTSPOT = 'X' .
APPEND wa_fcat to i_fcat.

WA_FCAT-COL_POS = '2' .
WA_FCAT-FIELDNAME = 'POSNR' .
WA_FCAT-TABNAME = 'I_VBAP' .
WA_FCAT-SELTEXT_M = 'ITEMNO' .
*  WA_FCAT-NO_OUT = 'X' .
WA_FCAT-HOTSPOT = 'X' .
APPEND WA_FCAT TO I_FCAT .
CLEAR WA_FCAT .

WA_FCAT-COL_POS = '3' .
WA_FCAT-FIELDNAME = 'MATNR' .
WA_FCAT-TABNAME = 'I_VBAP' .
WA_FCAT-SELTEXT_M = 'MATERIALNO' .
WA_FCAT-EDIT = 'X' .
APPEND WA_FCAT TO I_FCAT .
CLEAR WA_FCAT .

WA_FCAT-COL_POS = '4' .
WA_FCAT-FIELDNAME = 'NETWR' .
WA_FCAT-TABNAME = 'I_VBAP' .
WA_FCAT-SELTEXT_M = 'NETPRICE' .
WA_FCAT-EMPHASIZE = 'C610'.
WA_FCAT-DO_SUM = 'X' .
APPEND WA_FCAT TO I_FCAT .
CLEAR WA_FCAT .
ENDFORM.                    " FIELDCAT
*&---------------------------------------------------------------------*
*&      Form  SORT
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM sort .
*wa_sort-fieldname = 'VBELN'.
* WA_SORT-UP = 'X'.
*  WA_SORT-SUBTOT = 'X '.
*  APPEND WA_SORT TO I_SORT .
ENDFORM.                    " SORT
*&---------------------------------------------------------------------*
*&      Form  GET_DISP
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM get_disp .
CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
EXPORTING
*   I_INTERFACE_CHECK                 = ' '
*   I_BYPASSING_BUFFER                = ' '
*   I_BUFFER_ACTIVE                   = ' '
I_CALLBACK_PROGRAM                = sy-repid
*   I_CALLBACK_PF_STATUS_SET          = ' '
I_CALLBACK_USER_COMMAND           = 'USER_COMMAND'
*   I_CALLBACK_TOP_OF_PAGE            = ' '
*   I_CALLBACK_HTML_TOP_OF_PAGE       = ' '
*   I_CALLBACK_HTML_END_OF_LIST       = ' '
*   I_STRUCTURE_NAME                  = i_
*   I_BACKGROUND_ID                   = ' '
*   I_GRID_TITLE                      =
*   I_GRID_SETTINGS                   =
*   IS_LAYOUT                         =
IT_FIELDCAT                       = i_fcat
*   IT_EXCLUDING                      =
*   IT_SPECIAL_GROUPS                 =
IT_SORT                           = i_sort
*   IT_FILTER                         =
*   IS_SEL_HIDE                       =
I_DEFAULT                         = 'S'
*   I_SAVE                            = ' '
*   IS_VARIANT                        =
IT_EVENTS                         = i_events
*   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
*   I_HTML_HEIGHT_TOP                 = 0
*   I_HTML_HEIGHT_END                 = 0
*   IT_ALV_GRAPHICS                   =
*   IT_HYPERLINK                      =
*   IT_ADD_FIELDCAT                   =
*   IT_EXCEPT_QINFO                   =
*   IR_SALV_FULLSCREEN_ADAPTER        =
* IMPORTING
*   E_EXIT_CAUSED_BY_CALLER           =
*   ES_EXIT_CAUSED_BY_USER            =
TABLES
t_outtab                          = it_vbap
* EXCEPTIONS
*   PROGRAM_ERROR                     = 1
*   OTHERS                            = 2
.
IF sy-subrc <> 0.
* Implement suitable error handling here
ENDIF.

ENDFORM.                    " GET_DISP
*&---------------------------------------------------------------------*
*&      Form  GET_EVENTS
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM get_events .

CALL FUNCTION 'REUSE_ALV_EVENTS_GET'
* EXPORTING
*   I_LIST_TYPE           = 0
IMPORTING
ET_EVENTS             = I_EVENTS .

READ TABLE I_EVENTS INTO WA_EVENTS WITH KEY NAME = 'TOP_OF_PAGE' .
WA_EVENTS-FORM = 'FORM_TOP_OF_PAGE' .
MODIFY  I_EVENTS FROM WA_EVENTS INDEX SY-TABIX .

ENDFORM.                    " GET_EVENTS

FORM FORM_TOP_OF_PAGE.
wa_heading-typ = 'H'.
wa_heading-info = 'sales order report'.
APPEND WA_HEADING TO I_HEADING .

WA_HEADING-TYP = 'S' .
WA_HEADING-KEY = 'USERNAME' .
WA_HEADING-INFO = SY-UNAME .
APPEND WA_HEADING TO I_HEADING .

WA_HEADING-TYP = 'A' .
WA_HEADING-KEY = 'DATE' .
WA_HEADING-INFO = SY-DATUM .
APPEND WA_HEADING TO I_HEADING .

CALL FUNCTION 'REUSE_ALV_COMMENTARY_WRITE'
EXPORTING
it_list_commentary       = i_heading
*     I_LOGO                   =
*     I_END_OF_LIST_GRID       =
*     I_ALV_FORM               =
.

ENDFORM.

FORM user_command USING r_ucomm TYPE sy-ucomm
                         rs_selfield TYPE slis_selfield.
   CASE R_UCOMM.
     WHEN '&IC1'.
       READ TABLE it_vbap INTO wa_vbap INDEX rs_selfield-tabindex.
       if sy-subrc = 0.
         SET PARAMETER ID 'vbeln' FIELD wa_vbap-vbeln.
         CALL TRANSACTION 'VA03' AND SKIP FIRST SCREEN.
         ENDIF.
 *    WHEN .
 *    WHEN OTHERS.
   ENDCASE.
   ENDFORM.

To provide ALV as input help, we need to use function module REUSE_ALV_POPUP_TO_SELECTunder event AT SELECTION-SCREEN ON VALUE-REQUEST , for this requirement, we need to provide checkbox in popup ALV, we need to create field catalog .

OO-ALV

1

Steps need to follow to create OOALV

  1. Create Screen
  2. Insert Custom Container UI element.
  3. Create Module.
  4. Create instance for Custom Container and add instance to ALV.
  5. Get data from tables
  6. Set data to ALV
*&---------------------------------------------------------------------*
*& Report  ZOOALV_TEST_1
*&
*&---------------------------------------------------------------------*
*&
*&
*&---------------------------------------------------------------------*

REPORT zooalv_test_1.

TYPES : BEGIN OF ty_mara,
matnr TYPE matnr,
mtart TYPE mtart,
mbrsh TYPE mbrsh,
END OF ty_mara.

DATA : lv_container TYPE REF TO cl_gui_custom_container,
lv_alv      TYPE REF TO cl_gui_alv_grid,
it_mara     TYPE TABLE OF ty_mara,
it_fcat     TYPE lvc_t_fcat,
wa_fcat     TYPE lvc_s_fcat.

START-OF-SELECTION.
CALL  SCREEN 100.
*&---------------------------------------------------------------------*
*&      Module  STATUS_0100  OUTPUT
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
MODULE status_0100 OUTPUT.
SET PF-STATUS 'ZTEST1'.
SET TITLEBAR 'ZTEST2'.
CASE sy-ucomm.
WHEN 'BACK' OR 'CANC'.
LEAVE TO SCREEN 0.
WHEN 'SAVE'.
MESSAGE 'OPERATION NOT POSSIBLE NOW' TYPE 'I'.
WHEN OTHERS.
ENDCASE.
CREATE OBJECT lv_container
EXPORTING
*    parent                      =
container_name              = 'CC_ALV'
*    style                       =
*    lifetime                    = lifetime_default
*    repid                       =
*    dynnr                       =
*    no_autodef_progid_dynnr     =
*  EXCEPTIONS
*    cntl_error                  = 1
*    cntl_system_error           = 2
*    create_error                = 3
*    lifetime_error              = 4
*    lifetime_dynpro_dynpro_link = 5
*    others                      = 6
.
IF sy-subrc <> 0.
* MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
*            WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
ENDIF.

CREATE OBJECT lv_alv
EXPORTING
*    i_shellstyle      = 0
*    i_lifetime        =
i_parent          = lv_container
*    i_appl_events     = space
*    i_parentdbg       =
*    i_applogparent    =
*    i_graphicsparent  =
*    i_name            =
*    i_fcat_complete   = SPACE
*  EXCEPTIONS
*    error_cntl_create = 1
*    error_cntl_init   = 2
*    error_cntl_link   = 3
*    error_dp_create   = 4
*    others            = 5
.
IF sy-subrc <> 0.
* MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
*            WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
ENDIF.

*SELECT * FROM mara into TABLE it_mara
*  UP TO 50 rows.

*  Build Field catalog
wa_fcat-col_pos = '1'.
wa_fcat-fieldname = 'MATNR'.
wa_fcat-tabname = 'MARA'.
wa_fcat-scrtext_m = 'Material No'.
APPEND wa_fcat to it_fcat.
CLEAR : wa_fcat.
wa_fcat-col_pos = '2'.
wa_fcat-fieldname = 'MTART'.
wa_fcat-tabname = 'MARA'.
wa_fcat-scrtext_m = 'Material Type'.
APPEND wa_fcat to it_fcat.
CLEAR : wa_fcat.
wa_fcat-col_pos = '3'.
wa_fcat-fieldname = 'MBRsh'.
wa_fcat-tabname = 'MARA'.
wa_fcat-scrtext_m = 'Induxtry sector'.
APPEND wa_fcat to it_fcat.
CLEAR : wa_fcat.

SELECT matnr
mtart
mbrsh
FROM mara
into  TABLE it_mara
UP TO 50 ROWS.

CALL METHOD lv_alv->set_table_for_first_display
EXPORTING
*    i_buffer_active               =
*    i_bypassing_buffer            =
*    i_consistency_check           =
i_structure_name               = 'ty_MARA'
*    is_variant                    =
*    i_save                        =
*    i_default                     = 'X'
*    is_layout                     =
*    is_print                      =
*    it_special_groups             =
*    it_toolbar_excluding          =
*    it_hyperlink                  =
*    it_alv_graphics               =
*    it_except_qinfo               =
*    ir_salv_adapter               =
CHANGING
it_outtab                     = it_mara
it_fieldcatalog               = it_fcat
*    it_sort                       =
*    it_filter                     =
*  EXCEPTIONS
*    invalid_parameter_combination = 1
*    program_error                 = 2
*    too_many_lines                = 3
*    others                        = 4
.
IF sy-subrc <> 0.
* Implement suitable error handling here
ENDIF.

ENDMODULE.                 " STATUS_0100  OUTPUT

OO ALV FACTORY METHOD

CL_SALV_TABLE is the class used for this purpose

When ever we use ALV factory methods to display ALV, we don`t need to create any field catalog, we can directly add our user defined tables instance as it automatically determine fields and displays.

Below demonstrate a nice example

REPORT ZSAPN_ALV_MARA_FACTORY.
TYPES: BEGIN OF TY_MARA,
MATNR TYPE MARA-MATNR,
MTART TYPE MARA-MTART,
MBRSH TYPE MARA-MBRSH,
MATKL TYPE MARA-MATKL,
MEINS TYPE MARA-MEINS,
END OF TY_MARA.
DATA : IT_MARA TYPE TABLE OF TY_MARA,
WA_MARA TYPE MARA.
DATA : LR_ALV TYPE REF TO CL_SALV_TABLE.
SELECT MATNR MTART MBRSH MATKL MEINS FROM MARA INTO TABLE IT_MARA UP TO 50 ROWS.
*  TRY.
CALL METHOD CL_SALV_TABLE=>FACTORY "get SALV factory instance
*    EXPORTING
*      LIST_DISPLAY   = IF_SALV_C_BOOL_SAP=>FALSE
*      R_CONTAINER    =
*      CONTAINER_NAME =
IMPORTING
R_SALV_TABLE = LR_ALV
CHANGING
T_TABLE      = IT_MARA.
*   CATCH CX_SALV_MSG .
*  ENDTRY.

**get ALV columns
DATA : LR_COLUMNS TYPE REF TO CL_SALV_COLUMNS_TABLE. "columns instance
DATA : LR_COL TYPE REF TO CL_SALV_COLUMN_TABLE. "column instance
CALL METHOD LR_ALV->GET_COLUMNS  "get all columns
RECEIVING
VALUE = LR_COLUMNS.
IF LR_COLUMNS IS NOT INITIAL.
*TRY.
*   Get VBELN column
TRY.
LR_COL ?= LR_COLUMNS->GET_COLUMN( 'MATNR' ). "get MATNR columns to insert hotspot
CATCH CX_SALV_NOT_FOUND.
ENDTRY.
*
*   Set the Hotspot for MATNR Column
TRY.
CALL METHOD LR_COL->SET_CELL_TYPE "set cell type hotspot
EXPORTING
VALUE = IF_SALV_C_CELL_TYPE=>HOTSPOT.
.
CATCH CX_SALV_DATA_ERROR .
ENDTRY.
ENDIF.
LR_ALV->DISPLAY( ).

What does ‘?=’ means in CL_SALV_TABLE class

This ‘?=’ denotes a widening cast operator. In widening cast we assign reference of superclass to reference of subclass.

Suppose u have a superclass lcl_vehicle

its reference is r_vehicle.

abd you have its subclass lcl_car

with reference r_car.

r_car ?= r_vehicle.

‘?=’ denotes the WIDECASTING Operator in ABAP Object.

The assignment of an object reference to an interface reference is known as a narrowing cast since, as with inheritance, only a part of the object interface is visible once you have assigned the reference.

With an interface reference, you can no longer address all components in the class carrying out the implementation, but only the components defined in the interface. These components are now addressed using the interface reference exclusively with their own u2018shortu2019 name!

When an object reference is assigned to an interface reference, the static types Must be convertible, that is, the class that was used to define the object reference must have implemented the interface-reference interface. Otherwise there will be a syntax error.

The widening cast is, as with inheritance, the opposite of the narrowing cast: here it is used to retrieve an object reference from an interface reference. Obviously it cannot be statically checked, since an interface can be implemented by more than one class.

An object reference cannot be assigned to an interface reference if it has itself not implemented the corresponding interface. It cannot be assigned even if a subclass has implemented the interface and the interface reference points to an object in this class.

Assignments between interface references whose interfaces are not related to each other cannot be checked statically and must therefore be formulated using the cast operator u201C?=u201D.

For this type of assignment, a check must be carried out at runtime to see whether the class of the instance that the source reference points to also supports the interface that the target reference refers to. If this is the case, the cast is carried out, otherwise the catchable runtime MOVE_CAST_ERROR occurs.

This type of cast is neither a widening nor a narrowing cast, rather a switch from one view of an object to another.