0% found this document useful (0 votes)
103 views18 pages

Abap Faq'S - Alv: This Is A Set of Queries and Answers, Which We Have Collected From Multiple Sources To Help Abapers

This document contains FAQs and answers related to ABAP and ALV (Advanced List Viewer). It includes questions about dynamically changing an ALV report title and hiding columns based on user selection on the selection screen, creating a dynamic internal table based on fields from a DDIC table, and explanations of type pools, field catalogs, and appending fields to a field catalog.

Uploaded by

sylendrap
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
103 views18 pages

Abap Faq'S - Alv: This Is A Set of Queries and Answers, Which We Have Collected From Multiple Sources To Help Abapers

This document contains FAQs and answers related to ABAP and ALV (Advanced List Viewer). It includes questions about dynamically changing an ALV report title and hiding columns based on user selection on the selection screen, creating a dynamic internal table based on fields from a DDIC table, and explanations of type pools, field catalogs, and appending fields to a field catalog.

Uploaded by

sylendrap
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 18

ABAP FAQ’s – ALV

This is a set of queries and answers,which we have collected from multiple sources to
help ABAPers.

Contribution by
Meenakshi Palaniappan
Vivekanand Nagarajan
ABAP FAQ’s – ALV

Question:

How to change the ALV report title and hide columns as per users input,
in selection-screen after executing the report? I want to change the
report title and hide columns as per user selection.

Answer:

You can change using this Code

Parameter: p_title type LVC_TITLE.

In ALV Function.
IT_Grid_Title = p_title.

Do like this, only if he pass parameter based on your column name.

Or if you are giving a check box or List box, based on that, u must
code. Lastly you have to pass like this

DATA : FIELDCATLOG TYPE SLIS_FIELDCAT_ALV,

FIELDCATLOG_T TYPE SLIS_T_FIELDCAT_ALV.

IF CONDITION BASED ON UR COLOUMN NAME

FIELDCATLOG-NO_OUT = 'X'.

ENDIF

Question:

At run time I get few fields from the DDIC table. I want to create a
dynamic internal table based on the fields. How to achieve?

Answer-1:

* Example: how to create a dynamic internal table  


* The dynamic internal table stucture
ABAP FAQ’s – ALV

DATA: BEGIN OF STRUCT OCCURS 10,


    FILDNAME(8) TYPE C,
    ABPTYPE TYPE C,
    LENGTH TYPE I,
END OF STRUCT.

* The dynamic program source table


DATA: BEGIN OF INCTABL OCCURS 10,
    LINE(72),
END OF INCTABL.

DATA: LNG TYPE I, TYPESRTING(6).

* Sample dynamic internal table stucture


STRUCT-FILDNAME = 'field1'. STRUCT-ABPTYPE = 'c'. STRUCT-LENGTH = '6'.
APPEND STRUCT. CLEAR STRUCT.

STRUCT-FILDNAME = 'field2'. STRUCT-ABPTYPE = 'd'.


APPEND STRUCT. CLEAR STRUCT.

STRUCT-FILDNAME = 'field3'. STRUCT-ABPTYPE = 'i'.


APPEND STRUCT. CLEAR STRUCT.

* Create the dynamic internal table definition in the dyn. program


INCTABL-LINE = 'program zdynpro.'. APPEND INCTABL.
INCTABL-LINE = 'data: begin of dyntab occurs 10,'. APPEND INCTABL.

LOOP AT STRUCT.
  INCTABL-LINE = STRUCT-FILDNAME.
  LNG = STRLEN( STRUCT-FILDNAME ).

  IF NOT STRUCT-LENGTH IS INITIAL .


      TYPESRTING(1) = '('.
      TYPESRTING+1 = STRUCT-LENGTH.
      TYPESRTING+5 = ')'.
      CONDENSE TYPESRTING NO-GAPS.
      INCTABL-LINE+LNG = TYPESRTING.
  ENDIF.

  INCTABL-LINE+15 = 'type '.


  INCTABL-LINE+21 = STRUCT-ABPTYPE.
  INCTABL-LINE+22 = ','.
  APPEND INCTABL.
ENDLOOP.
INCTABL-LINE = 'end of dyntab. '.
APPEND INCTABL.

* Create the code processes the dynamic internal table


INCTABL-LINE = ' '. APPEND INCTABL.
INCTABL-LINE = 'dyntab-field1 = ''aaaaaa''.'. APPEND INCTABL.
INCTABL-LINE = 'dyntab-field1 = ''19970814''.'. APPEND INCTABL.
INCTABL-LINE = 'dyntab-field1 = 1.'. APPEND INCTABL.
INCTABL-LINE = 'append dyntab.'. APPEND INCTABL.
ABAP FAQ’s – ALV

INCTABL-LINE = ' '. APPEND INCTABL.


INCTABL-LINE = 'loop at dyntab.'. APPEND INCTABL.
INCTABL-LINE = 'write: / dyntab.'. APPEND INCTABL.
INCTABL-LINE = 'endloop.'. APPEND INCTABL.

* Create and run the dynamic program


INSERT REPORT 'zdynpro'(001) FROM INCTABL.
SUBMIT ZDYNPRO.

or Just try out this simpler dynamic internal tables

DATA: itab TYPE STANDARD TABLE OF spfli,


              wa LIKE LINE OF itab.

DATA: line(72) TYPE c,


            list LIKE TABLE OF line(72).

START-OF-SELECTION.
*line = ' CITYFROM CITYTO '.
  line = ' AIRPTO '.
 APPEND line TO list.

SELECT DISTINCT (list)


       INTO CORRESPONDING FIELDS OF TABLE itab
            FROM spfli.

IF sy-subrc EQ 0.
  LOOP AT itab INTO wa.
*     WRITE: / wa-cityfrom, wa-cityto.
       WRITE :/ wa-airpto.
  ENDLOOP.
ENDIF.

Answer-2:

Example on Dynamic Internal table. Go through

REPORT YTEST2 .

TYPE-POOLS : SLIS.

DATA: T_FIELDCAT TYPE LVC_T_FCAT, " Field Catalog

X_FIELDCAT LIKE LINE OF T_FIELDCAT. " For Field Catalog

DATA: V_CNT TYPE I,


ABAP FAQ’s – ALV

V_CNT1 TYPE C,

V_FLD(5) VALUE 'FIELD', " For Field name

V_REPID LIKE SY-REPID.

DATA: NEW_TABLE TYPE REF TO DATA, " Dynamic Table

NEW_LINE TYPE REF TO DATA. " Dynamic Line

DATA: BEGIN OF IT_VBAP OCCURS 0,

VBELN LIKE VBAP-VBELN,

POSNR LIKE VBAP-POSNR,

NETWR LIKE VBAP-NETWR,

END OF IT_VBAP.

FIELD-SYMBOLS: <V_TABLE> TYPE TABLE, " For Table

<V_LINE> TYPE ANY, " For Line

<V_FIELD> TYPE ANY. " For Field

*-Field catalog for IT_DISPLAY

DATA: IT_FIELDCAT TYPE SLIS_T_FIELDCAT_ALV WITH HEADER LINE,

* -Events in ALV

IT_EVENTS TYPE SLIS_T_EVENT,

X_LAYOUT TYPE SLIS_LAYOUT_ALV, " Layout for data,

X_EVENTS TYPE SLIS_ALV_EVENT. " For events.

INITIALIZATION.

V_REPID = SY-REPID.

START-OF-SELECTION.

SELECT VBELN POSNR NETWR


ABAP FAQ’s – ALV

FROM VBAP UP TO 50 ROWS

INTO TABLE IT_VBAP

WHERE NETWR <> SPACE.

V_CNT = 4.

PERFORM PREPARE_FIELDCATLOG.

PERFORM CREATE_TABLE.

PERFORM BUILD_FCAT_EVENTS.

LOOP AT IT_VBAP.

PERFORM COMMON_DATA.

INSERT <V_LINE> INTO TABLE <V_TABLE>.

ENDLOOP.

PERFORM ALV_LIST_DISPLAY.

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

*& Form Prepare_fieldcatlog

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

* Prepare the Catalog

*----------------------------------------------------------------------*

FORM PREPARE_FIELDCATLOG.

DATA L_FLD(7).

CLEAR V_CNT1.

CLEAR X_FIELDCAT.

X_FIELDCAT-FIELDNAME = 'VBELN'.

X_FIELDCAT-REF_FIELD = 'VBELN'.
ABAP FAQ’s – ALV

X_FIELDCAT-REF_TABLE = 'VBAP'.

APPEND X_FIELDCAT TO T_FIELDCAT.

CLEAR X_FIELDCAT.

X_FIELDCAT-FIELDNAME = 'POSNR'.

X_FIELDCAT-REF_FIELD = 'POSNR'.

X_FIELDCAT-REF_TABLE = 'VBAP'.

APPEND X_FIELDCAT TO T_FIELDCAT.

DO V_CNT TIMES.

V_CNT1 = V_CNT1 + 1.

CONCATENATE 'NETWR_' V_CNT1 INTO L_FLD.

X_FIELDCAT-FIELDNAME = L_FLD.

X_FIELDCAT-REF_FIELD = 'NETWR'.

X_FIELDCAT-REF_TABLE = 'VBAP'.

APPEND X_FIELDCAT TO T_FIELDCAT.

ENDDO.

ENDFORM. " Prepare_fieldcatlog

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

*& Form create_table

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

* Generate the table

*----------------------------------------------------------------------*

FORM CREATE_TABLE.

DATA: DYN_TAB TYPE REF TO DATA.

CALL METHOD CL_ALV_TABLE_CREATE=>CREATE_DYNAMIC_TABLE


ABAP FAQ’s – ALV

EXPORTING IT_FIELDCATALOG = T_FIELDCAT

IMPORTING EP_TABLE = NEW_TABLE.

* Create a new Line with the same structure of the table.

ASSIGN NEW_TABLE->* TO <V_TABLE>.

CREATE DATA NEW_LINE LIKE LINE OF <V_TABLE>.

ASSIGN NEW_LINE->* TO <V_LINE>.

ENDFORM. " create_table

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

*& Form BUILD_FCAT_EVENTS

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

* Build field catalog and events for ALV

*----------------------------------------------------------------------*

FORM BUILD_FCAT_EVENTS.

DATA: L_POS TYPE I VALUE 1.

REFRESH: IT_FIELDCAT, IT_EVENTS.

CLEAR : IT_FIELDCAT, X_EVENTS.

*-Build field catalog

*-Message type

IT_FIELDCAT-FIELDNAME = 'VBELN'.

IT_FIELDCAT-COL_POS = L_POS.

IT_FIELDCAT-SELTEXT_M = 'Sales Document'.

IT_FIELDCAT-OUTPUTLEN = 10.
ABAP FAQ’s – ALV

APPEND IT_FIELDCAT.

CLEAR IT_FIELDCAT.

L_POS = L_POS + 1.

IT_FIELDCAT-FIELDNAME = 'POSNR'.

IT_FIELDCAT-COL_POS = L_POS.

IT_FIELDCAT-SELTEXT_M = 'Item'(005).

IT_FIELDCAT-OUTPUTLEN = 6.

APPEND IT_FIELDCAT.

CLEAR IT_FIELDCAT.

L_POS = L_POS + 1.

IT_FIELDCAT-FIELDNAME = 'NETWR_1'.

IT_FIELDCAT-COL_POS = L_POS.

IT_FIELDCAT-SELTEXT_M = 'Currency'.

IT_FIELDCAT-OUTPUTLEN = 15.

APPEND IT_FIELDCAT.

CLEAR IT_FIELDCAT.

L_POS = L_POS + 1.

IT_FIELDCAT-FIELDNAME = 'NETWR_2'.

IT_FIELDCAT-COL_POS = L_POS.

IT_FIELDCAT-SELTEXT_M = 'Currency'.

IT_FIELDCAT-OUTPUTLEN = 15.

APPEND IT_FIELDCAT.

CLEAR IT_FIELDCAT.

L_POS = L_POS + 1.
ABAP FAQ’s – ALV

IT_FIELDCAT-FIELDNAME = 'NETWR_3'.

IT_FIELDCAT-COL_POS = L_POS.

IT_FIELDCAT-SELTEXT_M = 'Currency'.

IT_FIELDCAT-OUTPUTLEN = 15.

APPEND IT_FIELDCAT.

CLEAR IT_FIELDCAT.

*-Layout

X_LAYOUT-COLWIDTH_OPTIMIZE = 'X'.

X_LAYOUT-ZEBRA = 'X'.

*-Events

* Top of list

X_EVENTS-NAME = 'TOP_OF_LIST'.

X_EVENTS-FORM = 'TOP_OF_PAGE'.

APPEND X_EVENTS TO IT_EVENTS.

ENDFORM. " BUILD_FCAT_EVENTS

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

*& Form Common_data

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

* Move the Common data into final table

*----------------------------------------------------------------------*

FORM COMMON_DATA.

ASSIGN COMPONENT 'VBELN' OF STRUCTURE <V_LINE> TO <V_FIELD>.

<V_FIELD> = IT_VBAP-VBELN.
ABAP FAQ’s – ALV

ASSIGN COMPONENT 'POSNR' OF STRUCTURE <V_LINE> TO <V_FIELD>.

<V_FIELD> = IT_VBAP-POSNR.

ASSIGN COMPONENT 'NETWR_1' OF STRUCTURE <V_LINE> TO <V_FIELD>.

<V_FIELD> = IT_VBAP-NETWR.

ENDFORM. " Com

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

*& Form alv_list_display

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

FORM ALV_LIST_DISPLAY.

CALL FUNCTION 'REUSE_ALV_LIST_DISPLAY'

EXPORTING

I_CALLBACK_PROGRAM = V_REPID

IS_LAYOUT = X_LAYOUT

IT_FIELDCAT = IT_FIELDCAT[]

IT_EVENTS = IT_EVENTS[]

TABLES

T_OUTTAB = <V_TABLE>

EXCEPTIONS

PROGRAM_ERROR = 1

OTHERS = 2.

IF SY-SUBRC <> 0.

* MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO

* WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.

ENDIF.
ABAP FAQ’s – ALV

ENDFORM. " alv_list_display

Question:

What is meant by type-pools: slis,field_cat, and


append field to field_cat.

Answer:

Type Pools are predefined types in SAP.


SLIS is also one of the types, which are predefined in SAP.

SLIS contains standard table types like bellow:


SLIS_LAYOUT_ALV.
SLIS_FIELDCAT_ALV.
SLIS_T_LISTHEADER.
SLIS_T_EVENT.
SLIS_SELFIELD. Etc...

By using these predefined table types u can declare internal tables, and
u can use these.

Regarding Field_cat:

You can declare like: FIELD_CAT type SLIS_FIELDCAT_ALV.


Now Field_cat will contains attributes like Fieldname, Tabname, Key,
Hotspot, Do_sum etc...

Regarding append Field_cat:

After assigning values to these attributes of Field_cat, you need to


append this to internal table ... then u can pass these values in to
function module REUSE_ALV_GRID_DISPLAY.

For ex:

Field_cat-fieldname = ‘ZAUFNR’.
field_cat-tabname = 'ITAB1'.
field_cat-key = 'X'.
field_cat-hotspot = 'X'.
field_cat-seltext_m = 'ORDER NUMBER'.
append field_cat.
ABAP FAQ’s – ALV

Question:

In ALV reporting, how can we use the command AT-LINE-SELECTION, if we


want a second selection screen to be displayed containing the details of
the line that was selected?

Answer-1:

While calling the FM for ALV, u have to populate the Events table and
mention the same event there so that the alv report becomes interactive.

Answer-2:

The ALV reporting function module needs to know 2 things


- The program that called the ALV
- The form that will handle the at-line-selection callback
*Example:
Data: g_repid like sy-repid value sy-repid.
Data g_user_command (30) type c value 'USER_COMMAND'.

You then pass these things to the FM:

Call function 'REUSE_ALV_LIST_DISPLAY'


Exporting
i_callback_program = g_repid
i_callback_user_command = g_user_command
....
....
tables
t_outtab = i_report
exceptions
program_error = 1
others = 2.

And of course you still need the form 'USER_COMMAND', which must be
of the following form:

form user_command using r_ucomm like sy-ucomm


rs_selfield type slis_selfield.
*Use this for more custom functionality
*WRITE: r_ucomm , rs_selfield.

endform.

Question:
ABAP FAQ’s – ALV

How to attach a custom menu or a custom button, for my alv report, using
function modules (not through oops).

Answer:

In function modules reuse_alv_list_display or reuse_alv_grid_display you


have parameter called i_callback_pf_status_set for which u have to pass
the pf-status that u are going to use and handle that using form
se_pf_status using rt_extab type slis_t_extab.

Rxtab contains all function codes that are hidden on the standard
interface of the alv.

If u want to implement user own pf-status first copy the standard one
which is present in function group salv, and make the necessary changes.
Question:

Does anyone know of a way (or do you have a Z-program or anything)


that can take an ENTIRE module pool (with all the screens, PAI, PBO,
Etc...) and download it to a PC/disk?

Ans
Answer:

Question:

Please tell me what exactly the "&" mean in the following chunk of
code...

DATA: i_fieldcat type lvc_t_fcat with header line.

define fieldcatalog.
i_fieldcat-ref_table = &1.
i_fieldcat-fieldname = &2.
i_fieldcat-coltext = &3.
append i_fieldcat.
clear i_fieldcat.
end-of-definition.
ABAP FAQ’s – ALV

Answer:

&1 means the first argument you pass to the 'fieldcatalog' definition.
Following code will help u understand.
******************************
Report ZTEST.

DATA: i_fieldcat type lvc_t_fcat with header line.

define fieldcatalog.
i_fieldcat-ref_table = &1.
i_fieldcat-fieldname = &2.
i_fieldcat-coltext = &3.
append i_fieldcat.
clear i_fieldcat.
end-of-definition.

fieldcatalog 'ABC' 'BCD' 'CDE'.

loop at i_fieldcat.
write : / i_fieldcat-ref_table.
write : / i_fieldcat-fieldname.
write : / i_fieldcat-coltext.
endloop.
********************************************
Output:
ABC
BCD
CDE
********************************************

Question :

I have created an ALV list and would like to put our company logo in the
header. How can I do this?

Answer :

in the transaction OAOR, you should be able to insert your company Logo.

 GOTO - OAOR (Business Document Navigator)


 Give Class Name - PICTURES Class Type - OT..... then Execute
 It will show you the list, then select ENJOYSAP_LOGO.
 On that list, you will find one control with a "create" tab.
 Click std. doc types.
 Select SCREEN and double-click.
 It will push FILE selection screen.
ABAP FAQ’s – ALV

 Select your company logo (.gif) and press OK.


 It will ask for a description- for instance: "company logo".
 It will let you know your doc has been stored successfully.
 You can find your logo under ENJOYSAP_LOGO->Screen->company logo.
 Just run your ALV program, you should find your company logo in
place of the EnjoySAP logo.

Question :

Give a sample code for Auto refresh in ALV List

Answer :

REPORT z_alv_auto_refresh.
*>*********************************************************************
* This report displays User's info (SM04) using the FM :              *
* REUSE_ALV_LIST_DISPLAY                                              *
* The list is auto-refreshed (refresh time : 5 seconds)               *
*---------------------------------------------------------------------*

TYPE-POOLS: slis.                      " ALV Global Types

DATA :
  gt_user LIKE uinfo OCCURS 0 WITH HEADER LINE. " User info in SM04

*---------------------------------------------------------------------*
START-OF-SELECTION.

  PERFORM f_read_data.

  PERFORM f_display_data.

*---------------------------------------------------------------------*
*       Form  F_LIRE_DATA
*---------------------------------------------------------------------*
FORM f_read_data.

  REFRESH gt_user.

* Get User's info


  CALL FUNCTION 'THUSRINFO'
       TABLES
            usr_tabl = gt_user.

* Wait in a task
  PERFORM f_call_rfc_wait.

ENDFORM.                               " F_READ_DATA


*---------------------------------------------------------------------*
*      Form  F_DISPLAY_DATA
*---------------------------------------------------------------------*
FORM f_display_data.

  DEFINE m_sort.
ABAP FAQ’s – ALV

    add 1 to ls_sort-spos.


    ls_sort-fieldname = &1.
    append ls_sort to lt_sort.
  END-OF-DEFINITION.

  DEFINE m_event_exit.
    clear ls_event_exit.
    ls_event_exit-ucomm = &1.
    ls_event_exit-after = 'X'.
    append ls_event_exit to lt_event_exit.
  END-OF-DEFINITION.

  DATA :
    ls_layout     TYPE slis_layout_alv,
    lt_sort       TYPE slis_t_sortinfo_alv,
    ls_sort       TYPE slis_sortinfo_alv,
    lt_event_exit TYPE slis_t_event_exit,
    ls_event_exit TYPE slis_event_exit.

* Build Sort Table


  m_sort 'ZEIT'.

* Build Event Exit Table


  m_event_exit '&NTE'.                 " Refresh

  ls_layout-zebra = 'X'.
  ls_layout-colwidth_optimize = 'X'.

  CALL FUNCTION 'REUSE_ALV_LIST_DISPLAY'


       EXPORTING
            i_callback_program      = sy-cprog
            i_callback_user_command = 'USER_COMMAND'
            is_layout               = ls_layout
            i_structure_name        = 'UINFO'
            it_sort                 = lt_sort
            it_event_exit           = lt_event_exit
       TABLES
            t_outtab                = gt_user.

ENDFORM.                               " F_DISPLAY_DATA


*---------------------------------------------------------------------*
*       FORM USER_COMMAND                                             *
*---------------------------------------------------------------------*
FORM user_command USING i_ucomm     TYPE syucomm
                        is_selfield TYPE slis_selfield.     "#EC CALLED

  CASE i_ucomm.
    WHEN '&NTE'.
      PERFORM f_read_data.
      is_selfield-refresh = 'X'.
      SET USER-COMMAND '&OPT'.         " Optimize columns width
  ENDCASE.

ENDFORM.                               " USER_COMMAND


*---------------------------------------------------------------------*
*      Form  F_CALL_RFC_WAIT
*---------------------------------------------------------------------*
ABAP FAQ’s – ALV

FORM f_call_rfc_wait.

  DATA lv_mssg(80).                                         "#EC NEEDED

* Wait in a task
  CALL FUNCTION 'RFC_PING_AND_WAIT' STARTING NEW TASK '001'
    PERFORMING f_task_end ON END OF TASK
    EXPORTING
      seconds               = 5        " Refresh time
      busy_waiting          = space
    EXCEPTIONS
      RESOURCE_FAILURE      = 1
      communication_failure = 2  MESSAGE lv_mssg
      system_failure        = 3  MESSAGE lv_mssg
      OTHERS                = 4.

ENDFORM.                               " F_CALL_RFC_WAIT


*---------------------------------------------------------------------*
*      Form  F_TASK_END
*---------------------------------------------------------------------*
FORM f_task_end USING u_taskname.

  DATA lv_mssg(80).                                         "#EC NEEDED

* Receiving task results


  RECEIVE RESULTS FROM FUNCTION 'RFC_PING_AND_WAIT'
    EXCEPTIONS
      RESOURCE_FAILURE      = 1
      communication_failure = 2  MESSAGE lv_mssg
      system_failure        = 3  MESSAGE lv_mssg
      OTHERS                = 4.

  CHECK sy-subrc EQ 0.
  SET USER-COMMAND '&NTE'.             " Refresh

ENDFORM.                               " F_TASK_END


*************** END OF PROGRAM Z_ALV_AUTO_REFRESH *********************

You might also like