Saturday, November 22, 2008

Create transaction on a class method

By Joyjit Ghosh,
Kolkata
, India
.

In this demo I am going to show how to create transaction on a local class method.

Step1: First create a local class in a report from transaction SE38.

REPORT z_demo_oop_jg .

*---------------------------------------------------------------------*
* CLASS create_report DEFINITION
*---------------------------------------------------------------------*
*
*---------------------------------------------------------------------*

CLASS create_report DEFINITION.

PUBLIC SECTION.

METHODS: main.

PRIVATE SECTION.

DATA: i_data TYPE STANDARD TABLE OF sbook INITIAL SIZE 0.

METHODS: fetch_data,

display_data.

ENDCLASS. "create_report DEFINITION

*---------------------------------------------------------------------*
* CLASS create_report IMPLEMENTATION
*---------------------------------------------------------------------*
*
*---------------------------------------------------------------------*

CLASS create_report IMPLEMENTATION.

METHOD fetch_data.

* Select 100 records from SBOOK table

SELECT * FROM sbook

INTO TABLE i_data

UP TO 100 ROWS.

ENDMETHOD. "fetch_data

METHOD display_data.

CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'

EXPORTING

i_structure_name = 'SBOOK'

TABLES

t_outtab = i_data

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.

ENDMETHOD. "display_data

METHOD main.

fetch_data( ).

display_data( ).

ENDMETHOD. "main

ENDCLASS. "create_report IMPLEMENTATION

Step2. Now from transaction SE93 create a transaction for the method MAIN as shown in the screen shots given below:

Give a transaction name and press create button.


In the next screen give a description and choose the proper radio button

In the next screen provide report name (where the local class is defined), local class name and method name.


Now save the transaction and execute it.
In this case it will display the report.



This technique can be used to call a method (local class) from another program using statement: call transaction.

EX: call transaction 'Z_OOP'.

Note: In the same way you can create a transaction on method of a global class.

Problem with system refresh

By Joyjit Ghosh,
Kolkata, India.

Problem :

In few cases I have seen that after system refresh custom codes developed in the original system are not editable. This problem mainly happens in a landscape which contains two parallel paths one dedicated to development and another one for maintaining the old code. After a certain point in development cycle it is important to make the 2 paths in sync with each other and that’s why we have to refresh one system by the other. And after this refresh this problem may arise.

Solution:

From the above message you can get a hint and based on it you can check the original system name of this program from the menu path (Goto à Object directory) shown below





In this case original system name is different than the current system (BSP) name. This is happened because of system refresh. As code is refreshed from BT1 to BSP; due to this original system name is also changed to BT1 from BSP. And now SAP is considering BT1 as the original system that’s why it is showing this above message and not permitting to edit the program directly.

So to avoid this problem we need to manually change the original system from BT1 to BSP.

Go to transaction SE03. Set the first check box and give the program name to its corresponding text box and press execute button.



In the next screen select program entry and press the change object directory button.



In the next popup screen change the original system and press save button.


You will get the following message:



Now you can edit the program without any problem

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:

Thursday, September 25, 2008

Go to any page of a list directly form selection screen

By Joyjit Ghosh,
Kolkata, India.


Code:

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

*& Report Z_DEMO_JG

*&

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

*& Go to any page of a list directly form selection screen

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

REPORT z_demo_jg LINE-SIZE 80 LINE-COUNT 50

NO STANDARD PAGE HEADING.

TABLES: sflight.

* Type declaration

TYPES : BEGIN OF ty_flight,

carrid(19) TYPE c,

connid(18) TYPE c,

fldate LIKE sflight-fldate,

fldate1(14) TYPE c,

planetype(13) TYPE c,

END OF ty_flight.

* Data declaration

DATA: i_flight TYPE STANDARD TABLE OF ty_flight,

w_flight TYPE ty_flight.

DATA: field(50) TYPE c.

DATA: part1(30) TYPE c,

part2(30) TYPE c.

DATA: l_counter TYPE i.

* Selection screen

SELECT-OPTIONS: s_carrid FOR sflight-carrid.

PARAMETERS: p_page TYPE int3.

START-OF-SELECTION.

* Fetch data from sflight table

SELECT carrid

connid

fldate

planetype

FROM sflight

INTO CORRESPONDING FIELDS OF TABLE i_flight

WHERE carrid IN s_carrid.

CHECK sy-subrc = 0.


* Display final data

PERFORM display_data.

TOP-OF-PAGE.

* Display report header

PERFORM display_column_name.

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

*& Form display_column_name

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

* Display column header

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

* None

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

FORM display_column_name .


WRITE: /10 'Program name:', sy-repid.

WRITE: /10 'Page no:', sy-pagno.

* Store report header texts

w_flight-carrid = 'Airline Code'(001).

w_flight-connid = 'Flight Connection'(002).

w_flight-fldate1 = 'Flight date'(003).

w_flight-planetype = 'Aircraft Type'(004).

* Display report header

SKIP.

ULINE.

FORMAT INTENSIFIED ON COLOR 1.

WRITE: 1 sy-vline,

2 w_flight-carrid,

21 sy-vline,

22 w_flight-connid,

40 sy-vline,

41 w_flight-fldate1,

55 sy-vline,

56 w_flight-planetype,

80 sy-vline.

ULINE.

ENDFORM. " display_column_name

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

*& Form display_data

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

* Display flight data

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

* None

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

FORM display_data .

CLEAR w_flight.

* Display report final data

LOOP AT i_flight INTO w_flight.

w_flight-fldate1 = w_flight-fldate.

FORMAT INTENSIFIED ON COLOR 2.

WRITE: 1 sy-vline,

2 w_flight-carrid,

21 sy-vline,

22 w_flight-connid,

40 sy-vline,

41 w_flight-fldate1,

55 sy-vline,

56 w_flight-planetype,

80 sy-vline.

ULINE.

ENDLOOP.

IF p_page IS NOT INITIAL.

* Check whether any page exist with the number entered

* in the selection screen

READ LINE 1 OF PAGE p_page.

IF sy-subrc = 0.

* If exist then scroll down to that page

SCROLL LIST TO PAGE p_page LINE 1 .

ELSE.

* Otherwise display information message and continue

* processing

MESSAGE i000(z_zzz_ca_messages)

WITH 'Page number does not exist'(001).

ENDIF.

ENDIF.

ENDFORM. " display_data

Screen shots:

Input:

Output:

Input:



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: