Showing posts with label ALV using function module. Show all posts
Showing posts with label ALV using function module. Show all posts

Monday, May 18, 2009

ALV interface check

By Joyjit Ghosh

In ALV we have an option to check the interface parameters, which we are passing from our program. This check will identify any problem(s) that may occur during calling the ALV FM or ALV class method. It is advisable to perform this check before formal submission of the code to avoid any inconsistency that can lead to problem(s), even program termination in worst case. To enable this check interface parameter I_INTERFACE_CHECK of ALV FM(s) must contain ‘X’.

Example: 
*&-------------------------------------------------------------*
*& Report Z_ALV_INTERFACE_CHECK *
*& *
*&--------------------------------------------------------------
REPORT  z_alv_interface_check                   .
***************************************************************
* Type pool declaration for ALV
***************************************************************
TYPE-POOLS: slis.
***************************************************************
* Table
***************************************************************
TABLES: sbook.
***************************************************************
* Type declaration for SBOOK table
***************************************************************
TYPES: BEGIN OF ty_sbook,
carrid TYPE s_carr_id, " Airline Code
connid TYPE s_conn_id, " Flight Connection Number
fldate TYPE s_date, " Flight date
bookid TYPE s_book_id, " Booking number
customid TYPE s_customer," Customer Number
END OF ty_sbook.
*****************************************************************
* Table declaration for SBOOK table
*****************************************************************
DATA: i_sbook TYPE STANDARD TABLE OF ty_sbook INITIAL SIZE 0,

****************************************************************
* Table declaration for field catalog
****************************************************************
i_fcata TYPE slis_t_fieldcat_alv.
****************************************************************
* Workarea declaration for field catalog table
****************************************************************
DATA: w_fcata TYPE slis_fieldcat_alv.
****************************************************************
* Selection screen
**************************************************************
SELECT-OPTIONS: s_carrid FOR sbook-carrid.
***************************************************************
* Start-of-selection event
****************************************************************
* Fetch data from sbook table
PERFORM fetch_data.
* Populate field catalog
PERFORM populate_catalog.
* Display data in ALV
PERFORM display_alv.
*&--------------------------------------------------------------
*& Form fetch_data
*&-------------------------------------------------------------
FORM fetch_data .
  SELECT  carrid       " Airline Code
connid " Flight Connection Number
fldate " Flight date
bookid " Booking number
customid " Customer Number
FROM sbook
INTO TABLE i_sbook
WHERE carrid IN s_carrid.
  IF sy-subrc = 0.
    SORT i_sbook BY carrid.
  ENDIF.
ENDFORM.                    " fetch_data
*&-------------------------------------------------------------*
*& Form populate_catalog
*&--------------------------------------------------------------
FORM populate_catalog .
  w_fcata-fieldname = 'CARRID'.
w_fcata-tabname = 'I_SBOOK'.
w_fcata-seltext_m = 'Airline Code'(001).
w_fcata-ddictxt = 'M'.
APPEND w_fcata to i_fcata.
  w_fcata-fieldname = 'CONNID'.
w_fcata-tabname = 'I_SBOOK'.
w_fcata-seltext_m = 'Flight Connection Number'(002).
w_fcata-ddictxt = 'M'.
APPEND w_fcata to i_fcata.
* Wrong field name FIDATE
  w_fcata-fieldname = 'FIDATE'.
w_fcata-tabname = 'I_SBOOK'.
w_fcata-seltext_m = 'Flight date'(003).
w_fcata-ddictxt = 'M'.
APPEND w_fcata to i_fcata.
* Wrong field name BOOKLD
  w_fcata-fieldname = 'BOOKLD'.
w_fcata-tabname = 'I_SBOOK'.
w_fcata-seltext_m = 'Booking number'(004).
w_fcata-ddictxt = 'M'.
APPEND w_fcata to i_fcata.
  w_fcata-fieldname = 'CUSTOMID'.
w_fcata-tabname = 'I_SBOOK'.
w_fcata-seltext_m = 'Customer Number'(005).
w_fcata-ddictxt = 'M'.
APPEND w_fcata to i_fcata.
ENDFORM.                    " populate_catalog
*&----------------------------------------------------------
*& Form display_alv
*&-----------------------------------------------------------
form display_alv .
data: l_repid type sy-repid.
l_repid = sy-repid.
* Interface check parameter, we have to pass ‘X’ as value in this parameter
CALL FUNCTION 'REUSE_ALV_LIST_DISPLAY'
EXPORTING
I_INTERFACE_CHECK = 'X'
I_CALLBACK_PROGRAM = l_repid
IT_FIELDCAT = i_fcata
TABLES
t_outtab = i_sbook
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.
endform.                    " display_alv

Selection screen:

Output:

Additional screen (before showing the final output) to show the error or warning or success message(s)

Also various buttons are present on the application tool bar to check contents of Fieldcatalog, Layout, Sort, Filter, Events, Variant, and Special Fieldgroup.

Fieldcatalog:

Layout:

Sort:

Filter:

Event:

Variant:

Field group:

From the initial screen if we press back button then it will display the final output.

Final output:

Note:

  1. Always remember to switch off this check before formal code delivery otherwise these extra screen(s) can confuse the end user.
  2. In ECC 5.0, I have seen that this check does not work with FM REUSE_ALV_GRID_DISPLAY, in that case we need to convert grid to list temporarily to implement this check.

Printing ALV along with Page numbers

By Joyjit Ghosh

Expected Layout of the ALV report (when printing):

Step 1: Display each new document in a new page

To achieve this below routine is written to populate the IT_SORT table parameter of ALV function module REUSE_ALV_GRID_DISPLAY. And ALV will automatically insert page break whenever a new document is encountered.
CONSTANTS:    
c_spos TYPE SLIS_SPOS VALUE '01', " Column position
c_up TYPE SLIS_SOUP VALUE 'X', " Sorting order
c_table TYPE tabname VALUE 'I_FINAL'," Name of o/p table
c_group TYPE SLIS_CTRLS VALUE '*', " Sorting group
c_fld2 TYPE fieldname VALUE 'IDCNGA04-IDCN037'." Field name(Document no)
* Prepare sort table
PERFORM prepare_sort_table CHANGING i_sort[].
FORM prepare_sort_table CHANGING pi_sort TYPE slis_t_sortinfo_alv.
  CLEAR st_sort.
st_sort-spos = c_spos. " Sort sequence
st_sort-fieldname = c_fld2. " Document no
st_sort-tabname = c_table. " Table name
st_sort-up = c_up. " Ascending
st_sort-group = c_group. " Group
* Populate sort table
APPEND st_sort TO pi_sort.
ENDFORM.                    "prepare_sort_table

Step 2: Reset page number to 1 for each new document

From ALV function module we cannot control the page numbers as it will automatically increment the page counter whenever a new page is printed. So to achieve this we have written our custom code within the TOP_OF_PAGE routine.

Register TOP_OF_PAGE routine with the TOP_OF_PAGE event in IT_EVENTS table parameter of the ALV function module.
* Prepare event table
PERFORM prepare_event_table.
FORM prepare_event_table .
* Populate top-of-page event
st_event-name = slis_ev_top_of_page.
st_event-form = slis_ev_top_of_page.
APPEND st_event TO i_event.
  CLEAR st_event.
ENDFORM.                    " prepare_event_table

Custom code to control the page number in TOP_OF_PAGE routine

Code block marked in bold are used to control the page number. As in our case we are using ALV grid to display the report so during report display page number is not visible. It is only visible in print preview or at the time of printing.

FORM top_of_page.
* Local variable declaration
STATICS: l_comm TYPE syucomm. " Store user command
* Declaration of local variables
DATA: l_line_size TYPE sylinsz, " Line size
l_line TYPE slis_listheader, " Hold list header
l_currdoc TYPE idcn037, " Current doc.
l_currtabix TYPE sytabix. " Current index
* Check for print or print preview
IF ( sy-ucomm = c_prin OR
sy-ucomm = c_rnt_prev OR
sy-ucomm = c_rnt ).
    IF l_comm <> sy-ucomm.
CLEAR : g_page_cnt.
    ENDIF. " l_comm <> sy-ucomm
*   Store current table index
l_currtabix = sy-tabix.
CLEAR st_final.
*   If current index is 1 then start page numbering from 1
IF l_currtabix = 1.
*     Read 1st record and store the document no
READ TABLE i_final INDEX l_currtabix INTO st_final.
g_prevdoc = st_final-idcnga04-idcn037.
*     Start page numbering from 1
g_page_cnt = 1.
g_prevtabix = 1.
    ELSE.
*     Read the table line
READ TABLE i_final INDEX l_currtabix INTO st_final.
*     Store the current document
l_currdoc = st_final-idcnga04-idcn037.
*     If the current doc. is same as previou doc.
* increament the page no, otherwise start it from 1
IF l_currdoc = g_prevdoc.
*       Increament the page no
g_page_cnt = g_page_cnt + 1.
ELSE.
*       Start page from 1
g_page_cnt = 1.
*       Store current doc. as previous doc.
g_prevdoc = l_currdoc.
      ENDIF. " l_currdoc = g_prevdoc
    ENDIF. " l_currtabix = 1
  ENDIF. " sy-ucomm = c_prin OR
  IF g_page_cnt = 1.
*   Store the user command
l_comm = sy-ucomm.
  ENDIF. " g_page_cnt = 1
* Display page no
IF g_page_cnt > 0 .
*  Store the report width
l_line_size = sy-linsz.
*  Calculate position
l_line_size = l_line_size - 10.
*  Display page no
WRITE AT l_line_size 'Page no:'(021).
*  Calculate position
l_line_size = l_line_size + 5.
    WRITE AT l_line_size g_page_cnt.
  ENDIF. " g_page_cnt > 0
  REFRESH st_list_top_of_page.
* Populate company name
CLEAR l_line.
l_line-typ = c_typ.
l_line-info = g_comp_name.
APPEND l_line TO st_list_top_of_page.
* Populate heading
CLEAR l_line.
l_line-typ = c_typ.
l_line-info = text-020.
APPEND l_line TO st_list_top_of_page.
* At the time of printing or print preview display additional info
* on 1st page
IF g_page_cnt = 1 AND
( sy-ucomm = c_prin OR
sy-ucomm = c_rnt_prev OR
sy-ucomm = c_rnt ).
    CLEAR l_line.
l_line-typ = 'A'.
*   Populate Staff name
CONCATENATE text-017
st_final-idcnga04-idcn053
INTO l_line-info
SEPARATED BY ':'.
APPEND l_line TO st_list_top_of_page.
    CLEAR l_line.
l_line-typ = 'A'.
*   Populate Accountant
CONCATENATE text-018
'______________________'
INTO l_line-info
SEPARATED BY ':'.
    APPEND l_line TO st_list_top_of_page.
    CLEAR l_line.
l_line-typ = 'A'.
*   Populate Book keeper
CONCATENATE text-019
'______________________'
INTO l_line-info
SEPARATED BY ':'.
    APPEND l_line TO st_list_top_of_page.
    l_line-typ  = 'A'.
l_line-info = space.
APPEND l_line TO st_list_top_of_page.
  ENDIF.
* Display list header
CALL FUNCTION 'REUSE_ALV_COMMENTARY_WRITE'
EXPORTING
i_logo = space
it_list_commentary = st_list_top_of_page.
* No sy-subrc check is required
ENDFORM.                    " top_of_page

Step 3: Display report

Display the ALV report using 'REUSE_ALV_GRID_DISPLAY' function module.
  CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
EXPORTING
i_callback_program = l_prog " Call back progrm
it_fieldcat = i_fieldcat_msg " Field catalog
is_layout = st_layout " Layout
it_sort = i_sort[] " Sort table
i_save = c_save " 'A'
is_variant = l_variant " ALV variant
it_events = i_event " ALV events
is_print = st_print " Print parameters
TABLES
t_outtab = i_final " Output table
EXCEPTIONS
program_error = 1
OTHERS = 2.

Result:

Report is displayed in ALV grid. Note that no page number is displayed. Now press the print preview button

Report is displayed in print preview with page numbers.

Limitations:

  1. For positioning the page number in the report we have to calculate the position based on the list width. As in this case we are using the formula

Page number position = sy-linsz – 10. But if you want you can change it.

2. In print preview only 1st page number is visible for every document i.e. if a document spreading multiple pages
only the 1st page number will be visible. You can see all the page numbers at the time of actual printing.

ALV with EDIT and SAVE functionality

By Joyjit Ghosh.

Code:
REPORT z_demo_alv_jg.
*******************************************************************
* TYPE-POOLS *
*******************************************************************
TYPE-POOLS: slis.
*******************************************************************
* INTERNAL TABLES/WORK AREAS/VARIABLES *
*******************************************************************
DATA: i_fieldcat TYPE slis_t_fieldcat_alv,
i_index TYPE STANDARD TABLE OF i WITH HEADER LINE,
w_field TYPE slis_fieldcat_alv,
p_table LIKE dd02l-tabname,
dy_table TYPE REF TO data,
dy_tab TYPE REF TO data,
dy_line TYPE REF TO data.
*******************************************************************
* FIELD-SYMBOLS *
*******************************************************************
FIELD-SYMBOLS: TYPE STANDARD TABLE,
TYPE ANY,
TYPE ANY,
TYPE STANDARD TABLE.
*******************************************************************
* SELECTION SCREEN *
*******************************************************************
PARAMETERS: tabname(30) TYPE c,
lines(5) TYPE n.
*******************************************************************
* START-OF-SELECTION *
*******************************************************************
START-OF-SELECTION.
* Storing table name
p_table = tabname.
* Create internal table dynamically with the stucture of table name
* entered in the selection screen
CREATE DATA dy_table TYPE STANDARD TABLE OF (p_table).
ASSIGN dy_table->* TO .
IF sy-subrc <> 0.
MESSAGE i000(z_zzz_ca_messages) WITH ' No table found'.
    LEAVE TO LIST-PROCESSING.
ENDIF.
* Create workarea for the table
CREATE DATA dy_line LIKE LINE OF .
ASSIGN dy_line->* TO .
* Create another temp. table
CREATE DATA dy_tab TYPE STANDARD TABLE OF (p_table).
ASSIGN dy_tab->* TO .
  SORT i_fieldcat BY col_pos.
* Select data from table
SELECT * FROM (p_table)
INTO TABLE
UP TO lines ROWS.
  REFRESH .
* Display report
CALL FUNCTION 'REUSE_ALV_LIST_DISPLAY'
EXPORTING
i_callback_program = sy-repid
i_structure_name = p_table
i_callback_user_command = 'USER_COMMAND'
i_callback_pf_status_set = 'SET_PF_STATUS'
TABLES
t_outtab =
EXCEPTIONS
program_error = 1
OTHERS = 2.
  IF sy-subrc <> 0.
  ENDIF.
*&-----------------------------------------------------------------*
*& Form SET_PF_STATUS
*&-----------------------------------------------------------------*
* Setting custom PF-Status
*------------------------------------------------------------------*
* -->RT_EXTAB Excluding table
*------------------------------------------------------------------*
FORM set_pf_status USING rt_extab TYPE slis_t_extab.
  SET PF-STATUS 'Z_STANDARD'.
ENDFORM.                    "SET_PF_STATUS
*&----------------------------------------------------------------*
*& Form user_command
*&-----------------------------------------------------------------*
* Handling custom function codes
*------------------------------------------------------------------*
* -->R_UCOMM Function code value
* -->RS_SELFIELD Info. of cursor position in ALV
*------------------------------------------------------------------*
FORM user_command USING r_ucomm LIKE sy-ucomm
rs_selfield TYPE slis_selfield.
* Local data declaration
DATA: li_tab TYPE REF TO data,
l_line TYPE REF TO data.
* Local field-symbols
FIELD-SYMBOLS: TYPE table,
TYPE ANY.
* Create table
CREATE DATA li_tab TYPE STANDARD TABLE OF (p_table).
ASSIGN li_tab->* TO .
* Create workarea
CREATE DATA l_line LIKE LINE OF .
ASSIGN l_line->* TO .
  CASE r_ucomm.
*   When a record is selected
WHEN '&IC1'.
*     Read the selected record
READ TABLE ASSIGNING INDEX
rs_selfield-tabindex.
      IF sy-subrc = 0.
*       Store the record in an internal table
APPEND TO .
*       Fetch the field catalog info
CALL FUNCTION 'REUSE_ALV_FIELDCATALOG_MERGE'
EXPORTING
i_program_name = 'Z_DEMO_PDF_JG'
i_structure_name = p_table
CHANGING
ct_fieldcat = i_fieldcat
EXCEPTIONS
inconsistent_interface = 1
program_error = 2
OTHERS = 3.
IF sy-subrc = 0.
*         Make all the fields input enabled except key fields
w_field-input = 'X'.
          MODIFY i_fieldcat FROM w_field TRANSPORTING input
WHERE key IS INITIAL.
        ENDIF.
*       Display the record for editing purpose
CALL FUNCTION 'REUSE_ALV_LIST_DISPLAY'
EXPORTING
i_callback_program = sy-repid
i_structure_name = p_table
it_fieldcat = i_fieldcat
i_screen_start_column = 10
i_screen_start_line = 15
i_screen_end_column = 200
i_screen_end_line = 20
TABLES
t_outtab =
EXCEPTIONS
program_error = 1
OTHERS = 2.
        IF sy-subrc = 0.
*         Read the modified data
READ TABLE INDEX 1 INTO .
*         If the record is changed then track its index no.
* and populate it in an internal table for future
* action
IF sy-subrc = 0 AND <> .
= .
i_index = rs_selfield-tabindex.
APPEND i_index.
ENDIF.
ENDIF.
      ENDIF.
*   When save button is pressed
WHEN 'SAVE'.
*     Sort the index table
SORT i_index.
*     Delete all duplicate records
DELETE ADJACENT DUPLICATES FROM i_index.
      LOOP AT i_index.
*       Find out the changes in the internal table
* and populate these changes in another internal table
READ TABLE ASSIGNING INDEX i_index.
IF sy-subrc = 0.
APPEND TO .
ENDIF.
      ENDLOOP.
*     Lock the table
CALL FUNCTION 'ENQUEUE_E_TABLE'
EXPORTING
mode_rstable = 'E'
tabname = p_table
EXCEPTIONS
foreign_lock = 1
system_failure = 2
OTHERS = 3.
      IF sy-subrc = 0.
*       Modify the database table with these changes
MODIFY (p_table) FROM TABLE .
        REFRESH .
*       Unlock the table
CALL FUNCTION 'DEQUEUE_E_TABLE'
EXPORTING
mode_rstable = 'E'
tabname = p_table.
      ENDIF.
ENDCASE.
  rs_selfield-refresh = 'X'.
ENDFORM.                    "user_command

Selection screen:

Output:

Wednesday, October 22, 2008

Display footer info in ALV Grid

By Joyjit Ghosh.

Steps:

Step1: Populate the ALV event table with 'END_OF_LIST' event

Step2: Populate the footer text in to an internal table which is of type

slis_t_listheader.

Step3: Create a subroutine with name 'END_OF_LIST' and with in this form we have to call the function module ‘REUSE_ALV_COMMENTARY_WRITE’.

Step4: Now call the function module REUSE_ALV_GRID_DISPLAY’ to display the ALV report. Whenever ‘END_OF_LIST' event is get fired it executes the subroutine END_OF_LIST and displays the text into the footer of the ALV GRID.

Code:

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

*& Report Z_TEST_ALV

*&

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

*&

*&

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

REPORT z_test_alv.

TYPE-POOLS: slis.

DATA: i_mara TYPE STANDARD TABLE OF mara INITIAL SIZE 0.

DATA i_events TYPE slis_t_event.

DATA: i_end_of_page TYPE slis_t_listheader.

DATA: w_events TYPE slis_alv_event.

DATA: v_repid LIKE sy-repid.

START-OF-SELECTION.

SELECT * FROM

mara INTO TABLE i_mara

UP TO 100 ROWS.

CHECK sy-subrc = 0.

PERFORM populate_alv_event.

PERFORM comment_build USING i_end_of_page[].

PERFORM display_grid.

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

*& Form POPULATE_ALV_EVENT

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

FORM populate_alv_event .

* Populate event table

w_events-name = 'END_OF_LIST'.

w_events-form = 'END_OF_LIST'.

APPEND w_events TO i_events.

ENDFORM. " POPULATE_ALV_EVENT

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

*& Form COMMENT_BUILD

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

FORM comment_build USING p_i_end_of_page TYPE slis_t_listheader.

DATA: ls_line TYPE slis_listheader.

REFRESH p_i_end_of_page.

CLEAR ls_line.

ls_line-typ = 'H'.

ls_line-info = 'This is end of list'(001).

APPEND ls_line TO p_i_end_of_page.

ENDFORM. " COMMENT_BUILD

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

*& Form end_of_list

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

FORM end_of_list.

CALL FUNCTION 'REUSE_ALV_COMMENTARY_WRITE'

EXPORTING

it_list_commentary = i_end_of_page.

ENDFORM. "end_of_list

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

*& Form DISPLAY_GRID

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

FORM display_grid .

v_repid = sy-repid.

CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'

EXPORTING

i_callback_program = v_repid

i_structure_name = 'MARA'

it_events = i_events

TABLES

t_outtab = i_mara

EXCEPTIONS

program_error = 1

OTHERS = 2.

IF sy-subrc <> 0.

ENDIF.

ENDFORM. " DISPLAY_GRID

Output:

Monday, September 22, 2008

Display Average in ALV report

By Joyjit Ghosh,
Kolkata, India.


Code:

*&---------------------------------------------------------------------*
*& Report Z_DEMO_JG
*&
*&---------------------------------------------------------------------*
*& Display average in ALV report
*&---------------------------------------------------------------------*

REPORT z_demo_jg LINE-SIZE 80 LINE-COUNT 50

NO STANDARD PAGE HEADING.

TABLES: sflight.

TYPE-POOLS: slis.

* Data declaration

DATA: i_flight TYPE STANDARD TABLE OF sflight,

i_catalog TYPE slis_t_fieldcat_alv,

w_flight TYPE sflight,

w_catalog TYPE slis_fieldcat_alv.

DATA: v_repid TYPE syrepid.

* Selection screen

SELECT-OPTIONS: s_carrid FOR sflight-carrid.


START-OF-SELECTION.

* Fetch data from sflight table

SELECT *

FROM sflight

INTO TABLE i_flight

WHERE carrid IN s_carrid.


CHECK sy-subrc = 0.

v_repid = sy-repid.

* Build field catalog for ALV report

PERFORM build_catalog.

* Display ALV report

PERFORM display_report.

*&---------------------------------------------------------------------*
*& Form build_catalog
*&---------------------------------------------------------------------*
* Build field catalog for ALV report
*----------------------------------------------------------------------*
* None
*----------------------------------------------------------------------*

FORM build_catalog .

CALL FUNCTION 'REUSE_ALV_FIELDCATALOG_MERGE'

EXPORTING

i_program_name = v_repid

i_structure_name = 'SFLIGHT'

CHANGING

ct_fieldcat = i_catalog

EXCEPTIONS

inconsistent_interface = 1

program_error = 2

OTHERS = 3.

IF sy-subrc = 0.

* For average populate 'C' as value in

* field DO_SUM

w_catalog-do_sum = 'C'. " C = Average

* Modify record with new value

MODIFY i_catalog FROM w_catalog TRANSPORTING

do_sum WHERE fieldname = 'PRICE'.

ENDIF.

ENDFORM. " build_catalog

*&---------------------------------------------------------------------*
*& Form display_report
*&---------------------------------------------------------------------*
* Display ALV report
*----------------------------------------------------------------------*
* None
*----------------------------------------------------------------------*

FORM display_report .

IF i_catalog[] IS NOT INITIAL.

* Call ALV grid

CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'

EXPORTING

i_callback_program = v_repid

it_fieldcat = i_catalog

TABLES

t_outtab = i_flight

EXCEPTIONS

program_error = 1

OTHERS = 2.

IF sy-subrc = 0.

ENDIF.

ENDIF.

ENDFORM. " display_report

Selection Screen:

Output:

Sunday, September 21, 2008

Display subtotal text in ALV grid

By Joyjit Ghosh,
Kolkata, India.

Steps:
• Declare the fields twice in the table structure on which subtotal needs to be calculated.
• Populate these fields with same values in the internal table.
• When populating field catalog with these fields, hide one of the field by populating no_out = ‘X’ and tech = ‘X’ in the field catalog.
• Populate alv sort table for the field which is hidden in the field catalog.
• Populate alv event table for subtotal text (slis_ev_subtotal_text) event with routine name.
• Define the routine in the program to display subtotal text.

Code:

*&---------------------------------------------------------------------*
*& Report Z_ALV_SUBTOTAL
*&
*&---------------------------------------------------------------------*
*&
*&
*&---------------------------------------------------------------------*
REPORT z_alv_subtotal.

*&---------------------------------------------------------------------*
*& Table declaration
*&---------------------------------------------------------------------*

TABLES: ekko.

*&---------------------------------------------------------------------*
*& Type pool declaration
*&---------------------------------------------------------------------*
TYPE-POOLS: slis. " Type pool for ALV

*&---------------------------------------------------------------------*
*& Selection screen
*&---------------------------------------------------------------------*
SELECT-OPTIONS: s_ebeln FOR ekko-ebeln.

*&---------------------------------------------------------------------*
*& Type declaration
*&---------------------------------------------------------------------*

* Type declaration for internal table to store EKPO data
TYPES: BEGIN OF x_data,
ebeln TYPE char30, " Document no.
ebelp TYPE ebelp, " Item no
matnr TYPE matnr, " Material no
matnr1 TYPE matnr, " Material no
werks TYPE werks_d, " Plant
werks1 TYPE werks_d, " Plant
ntgew TYPE entge, " Net weight
gewe TYPE egewe, " Unit of weight
END OF x_data.

*&---------------------------------------------------------------------*
*& Internal table declaration
*&---------------------------------------------------------------------*
DATA:

* Internal table to store EKPO data
i_ekpo TYPE STANDARD TABLE OF x_data INITIAL SIZE 0,
* Internal table for storing field catalog information
i_fieldcat TYPE slis_t_fieldcat_alv,
* Internal table for Top of Page info. in ALV Display
i_alv_top_of_page TYPE slis_t_listheader,
* Internal table for ALV Display events
i_events TYPE slis_t_event,
* Internal table for storing ALV sort information
i_sort TYPE slis_t_sortinfo_alv,
i_event TYPE slis_t_event.

*&---------------------------------------------------------------------*
*& Work area declaration
*&---------------------------------------------------------------------*

DATA:
wa_ekko TYPE x_data,
wa_layout TYPE slis_layout_alv,
wa_events TYPE slis_alv_event,
wa_sort TYPE slis_sortinfo_alv.

*&---------------------------------------------------------------------*
*& Constant declaration
*&---------------------------------------------------------------------*

CONSTANTS:
c_header TYPE char1
VALUE 'H', "Header in ALV
c_item TYPE char1
VALUE 'S'.

*&---------------------------------------------------------------------*
*& Start-of-selection event
*&---------------------------------------------------------------------*

START-OF-SELECTION.

* Select data from ekpo
SELECT ebeln " Doc no
ebelp " Item
matnr " Material
matnr " Material
werks " Plant
werks " Plant
ntgew " Quantity
gewei " Unit
FROM ekpo
INTO TABLE i_ekpo
WHERE ebeln IN s_ebeln
AND ntgew NE '0.00'.

IF sy-subrc = 0.
SORT i_ekpo BY ebeln ebelp matnr .
ENDIF.

* To build the Page header
PERFORM sub_build_header.

* To prepare field catalog
PERFORM sub_field_catalog.

* Perform to populate the layout structure
PERFORM sub_populate_layout.

* Perform to populate the sort table.
PERFORM sub_populate_sort.

* Perform to populate ALV event
PERFORM sub_get_event.

END-OF-SELECTION.

* Perform to display ALV report
PERFORM sub_alv_report_display.


*&---------------------------------------------------------------------*
*& Form sub_build_header
*&---------------------------------------------------------------------*
* To build the header
*----------------------------------------------------------------------*
* No Parameter
*----------------------------------------------------------------------*
FORM sub_build_header .

* Local data declaration
DATA: l_system TYPE char10 , "System id
l_r_line TYPE slis_listheader, "Hold list header
l_date TYPE char10, "Date
l_time TYPE char10, "Time
l_success_records TYPE i, "No of success records
l_title(300) TYPE c. " Title


* Title Display
l_r_line-typ = c_header. " header
l_title = 'Test report'(001).
l_r_line-info = l_title.
APPEND l_r_line TO i_alv_top_of_page.
CLEAR l_r_line.

* Run date Display
CLEAR l_date.
l_r_line-typ = c_item. " Item
WRITE: sy-datum TO l_date MM/DD/YYYY.
l_r_line-key = 'Run Date :'(002).
l_r_line-info = l_date.
APPEND l_r_line TO i_alv_top_of_page.
CLEAR: l_r_line,
l_date.



ENDFORM. " sub_build_header


*&---------------------------------------------------------------------*
*& Form sub_field_catalog
*&---------------------------------------------------------------------*
* Build Field Catalog
*----------------------------------------------------------------------*
* No Parameter
*----------------------------------------------------------------------*
FORM sub_field_catalog .

* Build Field Catalog
PERFORM sub_fill_alv_field_catalog USING:

'01' '01' 'EBELN' 'I_EKPO' 'L'
'Doc No'(003) ' ' ' ' ' ' ' ',

'01' '02' 'EBELP' 'I_EKPO' 'L'
'Item No'(004) 'X' 'X' ' ' ' ',

'01' '03' 'MATNR' 'I_EKPO' 'L'
'Material No'(005) 'X' 'X' ' ' ' ',

'01' '03' 'MATNR1' 'I_EKPO' 'L'
'Material No'(005) ' ' ' ' ' ' ' ',


'01' '04' 'WERKS' 'I_EKPO' 'L'
'Plant'(006) 'X' 'X' ' ' ' ',

'01' '04' 'WERKS1' 'I_EKPO' 'L'
'Plant'(006) ' ' ' ' ' ' ' ',

'01' '05' 'NTGEW' 'I_EKPO' 'R'
'Net Weight'(007) ' ' ' ' 'GEWE' 'I_EKPO'.


ENDFORM. " sub_field_catalog

*&---------------------------------------------------------------------*
*& Form sub_fill_alv_field_catalog
*&---------------------------------------------------------------------*
*& For building Field Catalog
*&---------------------------------------------------------------------*
*& p_rowpos Row position
*& p_colpos Col position
*& p_fldnam Fldname
*& p_tabnam Tabname
*& p_justif Justification
*& p_seltext Seltext
*& p_out no out
*& p_tech Technical field
*& p_qfield Quantity field
*& p_qtab Quantity table
*&---------------------------------------------------------------------*
FORM sub_fill_alv_field_catalog USING p_rowpos TYPE sycurow
p_colpos TYPE sycucol
p_fldnam TYPE fieldname
p_tabnam TYPE tabname
p_justif TYPE char1
p_seltext TYPE dd03p-scrtext_l
p_out TYPE char1
p_tech TYPE char1
p_qfield TYPE slis_fieldname
p_qtab TYPE slis_tabname.

* Local declaration for field catalog
DATA: wa_lfl_fcat TYPE slis_fieldcat_alv.

wa_lfl_fcat-row_pos = p_rowpos. "Row
wa_lfl_fcat-col_pos = p_colpos. "Column
wa_lfl_fcat-fieldname = p_fldnam. "Field Name
wa_lfl_fcat-tabname = p_tabnam. "Internal Table Name
wa_lfl_fcat-just = p_justif. "Screen Justified
wa_lfl_fcat-seltext_l = p_seltext. "Field Text
wa_lfl_fcat-no_out = p_out. "No output
wa_lfl_fcat-tech = p_tech. "Technical field
wa_lfl_fcat-qfieldname = p_qfield. "Quantity unit
wa_lfl_fcat-qtabname = p_qtab . "Quantity table

IF p_fldnam = 'NTGEW'.
wa_lfl_fcat-do_sum = 'X'.
ENDIF.
APPEND wa_lfl_fcat TO i_fieldcat.
CLEAR wa_lfl_fcat.
ENDFORM. " sub_fill_alv_field_catalog

*&---------------------------------------------------------------------*
*& Form sub_populate_layout
*&---------------------------------------------------------------------*
* Populate ALV layout
*----------------------------------------------------------------------*
* No Parameter
*----------------------------------------------------------------------*
FORM sub_populate_layout .

CLEAR wa_layout.
wa_layout-colwidth_optimize = 'X'." Optimization of Col width

ENDFORM. " sub_populate_layout

*&---------------------------------------------------------------------*
*& Form sub_populate_sort
*&---------------------------------------------------------------------*
* Populate ALV sort table
*----------------------------------------------------------------------*
* No Parameter
*----------------------------------------------------------------------*
FORM sub_populate_sort .

* Sort on material
wa_sort-spos = '01' .
wa_sort-fieldname = 'MATNR'.
wa_sort-tabname = 'I_EKPO'.
wa_sort-up = 'X'.
wa_sort-subtot = 'X'.
APPEND wa_sort TO i_sort .
CLEAR wa_sort.

* Sort on plant
wa_sort-spos = '02'.
wa_sort-fieldname = 'WERKS'.
wa_sort-tabname = 'I_EKPO'.
wa_sort-up = 'X'.
wa_sort-subtot = 'X'.
APPEND wa_sort TO i_sort .
CLEAR wa_sort.


ENDFORM. " sub_populate_sort

*&---------------------------------------------------------------------*
*& Form sub_get_event
*&---------------------------------------------------------------------*
* Get ALV grid event and pass the form name to subtotal_text
* event
*----------------------------------------------------------------------*
* No Parameter
*----------------------------------------------------------------------*
FORM sub_get_event .
CONSTANTS : c_formname_subtotal_text TYPE slis_formname VALUE
'SUBTOTAL_TEXT'.

DATA: l_s_event TYPE slis_alv_event.


CALL FUNCTION 'REUSE_ALV_EVENTS_GET'
EXPORTING
i_list_type = 4
IMPORTING
et_events = i_event
EXCEPTIONS
list_type_wrong = 0
OTHERS = 0.

* Subtotal
READ TABLE i_event INTO l_s_event
WITH KEY name = slis_ev_subtotal_text.
IF sy-subrc = 0.
MOVE c_formname_subtotal_text TO l_s_event-form.
MODIFY i_event FROM l_s_event INDEX sy-tabix.
ENDIF.

ENDFORM. " sub_get_event


*&---------------------------------------------------------------------*
*& Form sub_alv_report_display
*&---------------------------------------------------------------------*
* For ALV Report Display
*----------------------------------------------------------------------*
* No Parameter
*----------------------------------------------------------------------*
FORM sub_alv_report_display .
DATA: l_repid TYPE syrepid .
l_repid = sy-repid .

* This function module for displaying the ALV report
CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
EXPORTING
i_callback_program = l_repid
i_callback_top_of_page = 'SUB_ALV_TOP_OF_PAGE'
is_layout = wa_layout
it_fieldcat = i_fieldcat
it_sort = i_sort
it_events = i_event
i_default = 'X'
i_save = 'A'
TABLES
t_outtab = i_ekpo
EXCEPTIONS
program_error = 1
OTHERS = 2.
IF sy-subrc <> 0.
* MESSAGE i000 WITH 'Error in ALV report display'(055).
ENDIF.

ENDFORM. " sub_alv_report_display


*&---------------------------------------------------------------------*
* FORM sub_alv_top_of_page
*---------------------------------------------------------------------*
* Call ALV top of page
*---------------------------------------------------------------------*
* No parameter
*---------------------------------------------------------------------*

FORM sub_alv_top_of_page. "#EC CALLED

* To write header for the ALV
CALL FUNCTION 'REUSE_ALV_COMMENTARY_WRITE'
EXPORTING
it_list_commentary = i_alv_top_of_page.


ENDFORM. "alv_top_of_page

*&---------------------------------------------------------------------*
*& Form subtotal_text
*&---------------------------------------------------------------------*
* Build subtotal text
*----------------------------------------------------------------------*
* P_total Total
* p_subtot_text Subtotal text info
*----------------------------------------------------------------------*
FORM subtotal_text CHANGING
p_total TYPE any
p_subtot_text TYPE slis_subtot_text.


* Material level sub total
IF p_subtot_text-criteria = 'MATNR'.
p_subtot_text-display_text_for_subtotal
= 'Material level total'(009).
ENDIF.

* Plant level sub total
IF p_subtot_text-criteria = 'WERKS'.
p_subtot_text-display_text_for_subtotal = 'Plant level total'(010).
ENDIF.

ENDFORM. "subtotal_text

Friday, September 12, 2008

Change color of a cell in ALV report

By Joyjit Ghosh,
Kolkata, India.

To change the color of a particular cell at runtime ,we have to perform the following steps:


Step1:

Define an internal table of TYPE slis_specialcol_alv as a field into the output table which has to pass into the ‘REUSE_ALV_GRID_DISPLAY’ Function module.

TYPES: BEGIN OF ty_user_output,

BELNR like RBKP-BELNR, "Doc. Number

BUDAT like RBKP-BUDAT, "Posting Date

BUKRS like RBKP-BUKRS, "Company Code

LIFNR like RBKP-LIFNR, "Invoicing Party

NAME1 like LFA1-NAME1, "Name

USNAM like RBKP-USNAM, "User

BUZEI like RSEG-BUZEI, "Invoice Item

RBWWR like RSEG-RBWWR, "Amount

EBELN like RSEG-EBELN, "Purchase Document

EBELP like RSEG-EBELP, "Purchase Order Item

WERKS like RSEG-WERKS, "Plant

EKGRP like EKKO-EKGRP, "Purchasing Group

TBTKX like RSEG-TBTKZ, "Subsequent Debit/Credit

SPGRG like RSEG-SPGRG, "Order Price Quantity

SPGRM like RSEG-SPGRM, "Blocking Reason:Quantity

SPGRP like RSEG-SPGRP, "Blocking Reason:Price

SPGRC like RSEG-SPGRC, "Blocking Reason:Quality

SPGRS like RSEG-SPGRS, "Blocking Reason:Amount

SPGRT like RSEG-SPGRT, "Blocking Reason:Date

SPGRQ like RSEG-SPGRQ, "Blocking Reason:Manual

parked(1) type c , "Indicates Parked Invoice

Note(1) type c, "Indicates Blocking Notes

GJAHR like RBKP-GJAHR, "Fiscal year

color TYPE slis_specialcol_alv OCCURS 2,

"For cell color

END OF ty_user_output.

Step2:

When building the layout set for ALV ,specify the color field name.

w_layout-coltab_fieldname = 'COLOR'.

Step3:

If we want to give a color to any column when the report is first displayed,

then we have to populate the internal table COLOR.

DATA: w_color TYPE slis_specialcol_alv. " Color

w_color-fieldname = p_field.

w_color-color-col = p_color.

w_color-color-int = p_int.

w_color-color-inv = p_inv.

w_color-nokeycol = p_key.

APPEND w_color TO w_user_output-color.

CLEAR w_color.

Step4:

If the user press any custom button or double click on any particular field,

then we have to track the sy-ucomm value .To do this we have to create a subroutine,

which has two parameters ,one is like sy-ucomm and another is type slis_selfield,

and we have to assign the subroutine name in the ‘i_callback_user_command ‘ parameter of the function module REUSE_ALV_GRID_DISPLAY.

CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'

EXPORTING

i_callback_program = c_repid

i_callback_user_command = 'USER_COMMAND'

i_structure_name = 'i_tab'

is_layout = w_layout

it_fieldcat = i_fieldcat[]

I_SAVE = 'A'

is_variant = w_variant

it_events = i_levents

is_print = w_print

TABLES

t_outtab = i_tab

EXCEPTIONS

program_error = 1

OTHERS = 2.





Form USER_COMMAND USING r_ucomm LIKE sy-ucomm

rs_selfield TYPE slis_selfield.



ENDFORM. "USER_COMMAND


Now when ever user generates a particular function code then we have to first check the field name then modify the color table depending upon field name and lastly modify the output table. This logic is written in to the above subroutine.

IF r_ucomm = V_F2CODE.

if rs_selfield-fieldname = c_doc_name.

*Work area for color table.

DATA: w_color TYPE slis_specialcol_alv.

clear w_user_output.

*Read internal table for proper value.

read table i_user_output

index rs_selfield-tabindex(Contains index of the current record selected by the user)

into w_user_output.

if sy-subrc = 0.

read table w_user_output-color into w_color

with key fieldname = rs_selfield-fieldname(Contains current field name selected by the user).

if sy-subrc = 0.

w_color-color-col = '4'.

w_color-color-int = 1.

w_color-color-inv = 0.

* Modify the internal table Color

MODIFY w_user_output-color FROM w_color

INDEX sy-tabix TRANSPORTING color.

CLEAR w_color.

* Modify the internal table i_user_output

MODIFY i_user_output FROM w_user_output

INDEX p_index TRANSPORTING color.

endif.

endif.

Endif.

Endif.

Now to refresh the ALV grid, set the refresh field of the rs_selfield parameter.

rs_selfield-refresh = ‘X’.


Report Output:

First time when the report is displayed:



After successive processing: