Anda di halaman 1dari 125

Insert hyperlink in ALV Grid

This is a simple tutorial which shows how to insert hyperlinks in the ALV Grid. On clicking the
hyperlink, a web browser will open up which will point to the URL which has been configured
programmatically.
Besides the regular steps for ALV grid some additional steps needs to be followed to populate
hyperlinks into the ALV grid.
Steps to create hyperlink in ALV:
1.

Create additional fields for the hyperlink handles in your type declaration for the final
table(the table that is passed to the it_output parameter of the method
set_table_for_first_display)

2.

Pass the appropriate handle to the web_field field of fieldcatalog.

3.

Create an internal table of type lvc_t_hype.

Assign an appropriate handle number and URL for a particular column and populate the internal
table with this data and pass this internal table to the it_hyperlink parameter of the method
set_table_for_first_display.

4.

Pass the appropriate handle number while populating the list data.

Code:
*&------------------------------------------------------------------*& Report YALV_HYPERLINK
*&
*&-------------------------------------------------------------------

REPORT yalv_hyperlink.

TYPES : BEGIN OF y_dataset,


product TYPE char100,
variant TYPE char100,
price

TYPE char100,

pd_handle TYPE int4,


vr_handle TYPE int4,
END OF y_dataset.

DATA : ts_data TYPE STANDARD TABLE OF y_dataset,


e_fcat TYPE lvc_s_fcat,

ts_fcat TYPE lvc_t_fcat,


e_data TYPE y_dataset,
e_hype TYPE lvc_s_hype,
ts_hype TYPE lvc_t_hype.

DATA : ref_grid TYPE REF TO cl_gui_custom_container,


ref_alv TYPE REF TO cl_gui_alv_grid.

START-OF-SELECTION.

PERFORM f_fill_data.

PERFORM f_fill_fieldcat.

PERFORM f_fill_hyperlink.

CALL SCREEN 9000.

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

Form F_FILL_DATA

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

Populate the list data

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

FORM f_fill_data .

CLEAR e_data.
MOVE : 'iPhone' TO e_data-product,
'iPhone5' TO e_data-variant,
'$599'

TO e_data-price,

'1'

TO e_data-pd_handle,

'2'

TO e_data-vr_handle.

APPEND e_data TO ts_data.

CLEAR e_data.
MOVE : 'iPhone' TO e_data-product,
'iPhone 4S' TO e_data-variant,
'$399'

TO e_data-price,

'1'

TO e_data-pd_handle,

'2'

TO e_data-vr_handle.

APPEND e_data TO ts_data.

CLEAR e_data.
MOVE : 'iPhone' TO e_data-product,
'iPhone 4' TO e_data-variant,
'$299'

TO e_data-price,

'1'

TO e_data-pd_handle,

'2'

TO e_data-vr_handle.

APPEND e_data TO ts_data.

ENDFORM.

" F_FILL_DATA

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

Form F_FILL_FIELDCAT

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

Populate field catalog

*-------------------------------------------------------------------FORM f_fill_fieldcat .

FREE ts_fcat.

CLEAR : e_fcat.

MOVE : 'TS_DATA' TO e_fcat-tabname,


'product' TO e_fcat-fieldname,
1

TO e_fcat-col_pos,

TO e_fcat-indx_field,

20

TO e_fcat-outputlen,

'Product' TO e_fcat-coltext,
'PD_HANDLE' TO e_fcat-web_field.

APPEND e_fcat TO ts_fcat.

CLEAR : e_fcat.

MOVE : 'TS_DATA' TO e_fcat-tabname,


'variant' TO e_fcat-fieldname,
2

TO e_fcat-col_pos,

TO e_fcat-indx_field,

20

TO e_fcat-outputlen,

'Variant' TO e_fcat-coltext,
'VR_HANDLE' TO e_fcat-web_field.

APPEND e_fcat TO ts_fcat.

MOVE : 'TS_DATA' TO e_fcat-tabname,


'Price' TO e_fcat-fieldname,
3

TO e_fcat-col_pos,

TO e_fcat-indx_field,

20

TO e_fcat-outputlen,

'Price' TO e_fcat-coltext,
*"

Handle from the previous field catalog is carried here

*"

in order to prevent this handle to continue here pass a null

*"

value here.

''

TO e_fcat-web_field.

APPEND e_fcat TO ts_fcat.

ENDFORM.

" F_FILL_FIELDCAT

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

Module STATUS_9000 OUTPUT

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

PBO for screen 9000

*-------------------------------------------------------------------MODULE status_9000 OUTPUT.

SET PF-STATUS 'STATUS'.

CREATE OBJECT ref_grid


EXPORTING
container_name

= 'CONT'

EXCEPTIONS
cntl_error

=1

cntl_system_error

=2

create_error

=3

lifetime_error

=4

lifetime_dynpro_dynpro_link = 5
OTHERS

= 6.

IF sy-subrc EQ 0.

ENDIF.

CREATE OBJECT ref_alv


EXPORTING

i_parent

= ref_grid

EXCEPTIONS
error_cntl_create = 1
error_cntl_init = 2
error_cntl_link = 3
error_dp_create = 4
OTHERS

= 5.

IF sy-subrc <> 0.

ENDIF.

CALL METHOD ref_alv->set_table_for_first_display


EXPORTING
it_hyperlink

= ts_hype

CHANGING
it_outtab

= ts_data

it_fieldcatalog

= ts_fcat

EXCEPTIONS
invalid_parameter_combination = 1
program_error

=2

too_many_lines

=3

OTHERS

= 4.

IF sy-subrc <> 0.

ENDIF.

ENDMODULE.

" STATUS_9000 OUTPUT

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

Module USER_COMMAND_9000 INPUT

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

PAI for screen 9000

*-------------------------------------------------------------------MODULE user_command_9000 INPUT.

DATA ok_code TYPE sy-ucomm.

CASE ok_code.

WHEN 'BACK'.

LEAVE TO SCREEN 0.

WHEN OTHERS.

ENDCASE.

ENDMODULE.

" USER_COMMAND_9000 INPUT

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

Form F_FILL_HYPERLINK

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

To populate the hyperlink table

*-------------------------------------------------------------------FORM f_fill_hyperlink .

FREE ts_hype.

CLEAR e_hype.
MOVE : '1' TO e_hype-handle,
'http://www.apple.com' TO e_hype-href.
APPEND e_hype TO ts_hype.

CLEAR e_hype.
MOVE : '2' TO e_hype-handle,

'http://www.apple.com/iphone' TO e_hype-href.
APPEND e_hype TO ts_hype.

ENDFORM.

" F_FILL_HYPERLINK

ALV Summations and Calculation for different columns


This document explains how to get the calculated values at the end of particular column based on
summation values of other columns in a simple ALV report output
Below example we have three columns A, B &C
A and B has the sum Value and C has been calculated based on the sum value of column A and
Column B

Column A -> Sum -> 1+2->3


Column B -> Sum -> 3+4->7
Column C -> (Sum of column A / Sum of Column B) * 100 -> (3/7)*100 -> 42.85
Steps to be followed
Develop a normal ALV report with all the relevant inputs
1.

Final Internal table should have the field rowcolor to differentiate the last row color

2.

Define ALV Layout

3.

Build logic for the requirement

4.

Fill the field cat, layout and Call the ALV Grid function module

5.

Output

STEP1 -> Final Internal table should have the field rowcolor to differentiate the last row color

STEP2-> Define ALV Layout

STEP3-> Build logic for the requirement


Use control break statement at last to get the summation.
To differentiate the last row for colouring, fill the field rowcolor with proper value

STEP4-> Fill the field cat and Layout and Call the ALV Grid function module
Since already we have calculated the sum, just pass the parameter no_sum = X for all the fields

Fill the layout and call the function module

STEP5-> Output

Sending the Multiple ALVs as PDF Attachment through Email


Brief Requirement Overview:
In this document, an email, with the PDF attachment of 3 ALV outputs, will be send to the Email
addresses of multiple persons or single person as required. The principle is to print ALVs to spool
directly, convert spool to PDF, and send the PDF to email. It can be used in background as well as
in foreground. The program must be executed in background to generate the spool request.
Scenario:
The program will display 3 ALV grids using Custom containers in foreground and will use
FMREUSE_ALV_BLOCK_LIST_APPEND to generate spool. When the program is executed in
foreground, the three ALV grids will be displayed and an email with PDF attachment of the ALV
outputs will be send .When the program is executed in background, spool request will be
generated containing the 3 ALV outputs and an email with PDF attachment of the ALV outputs
will be send.
Challenge:

The challenge in this scenario is, the 3 ALV grids built using OOPS concept cannot be sent to Spool
directly
in
foreground.
For
sending
the
3
ALV
grids
to
spool,
FMREUSE_ALV_BLOCK_LIST_APPEND has been used. The foreground mode uses both the normal
ALV grid display methods of OOPS to display the three ALVs in foreground and then uses
REUSE_ALV_BLOCK_LIST_APPEND FM to send the report to spool as list. The background mode
uses only REUSE_ALV_BLOCK_LIST_APPEND to send the report to spool.
Step1: Creating screen for foreground display
Go to Screen painter Transaction Code SE51.
Create a screen with no 100.
Provide description for the screen and click on the Layout Button.
Place 3 Custom container UI elements and give names as G_CONTAINER1 , G_CONTAINER2 and
G_CONTAINER3 .The screen 100 will have 3 custom containers as shown below. Activate the
Object.

Step 2: Flow Logic


Go to the Flow Logic Tab to write coding for PBO & PAI.

Step3: ABAP Editor


Create a Z program with the code as below:
The three internal tables for 3 ALVs are: I_ORDERS1, I_ORDERS2 and I_INVSTATUS.
The three field catalogs are built and the field catalog names are: I_FIELDCATALOG1,
I_FIELDCATALOG2 and I_FIELDCATALOG3.
*&---------------------------------------------------------------------*
*&

Module STATUS_0100 OUTPUT

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

text

*----------------------------------------------------------------------*
MODULE status_0100 OUTPUT.

* PF status of the screen


PERFORM sub_pf_status.
* Set the title of report
SET TITLEBAR 'TTL'.

* Display ALV Data


PERFORM sub_display_firstalv USING i_fieldcatalog1
i_orders1.

PERFORM sub_display_secondalv USING i_fieldcatalog2


i_orders2.

PERFORM sub_display_thirdalv USING i_fieldcatalog3


i_invstatus.
* Send email to customers
PERFORM sub_send_mail.

* Refresh the first display table


CALL METHOD g_grid1->refresh_table_display
EXCEPTIONS
finished = 1
OTHERS = 2.

* Refresh the second display table


CALL METHOD g_grid2->refresh_table_display
EXCEPTIONS
finished = 1
OTHERS = 2.

* Refresh the third display table

CALL METHOD g_grid3->refresh_table_display


EXCEPTIONS
finished = 1
OTHERS = 2.

ENDMODULE.

" STATUS_0100 OUTPUT

Subroutine to set the PF Status of the report

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

Form sub_pf_status

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

text

*----------------------------------------------------------------------*
FORM sub_pf_status.

* Local data declaration


DATA: lt_excl TYPE ty_t_excl.

*Set PF status
SET PF-STATUS 'ZSTATUS_0100' EXCLUDING lt_excl.
ENDFORM.

SUB_PF_STATUS

Subroutine to Display First ALV

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

*&

Form SUB_DISPLAY_FIRSTALV

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

text

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

-->P_I_FIELDCATALOG1 text

-->P_I_ORDERS1 text

*----------------------------------------------------------------------*
FORM sub_display_firstalv USING fp_i_fieldcatalog1 TYPE lvc_t_fcat
fp_i_orders1

DATA:

TYPE ty_t_orders1.

lx_print TYPE lvc_s_prnt.

* Local data declaration


DATA: li_layout TYPE lvc_s_layo.

* Layout for ALV


PERFORM sub_prepare_layout USING c_x
text-011
c_x
c_cellstyle
CHANGING li_layout.
* Use Flush
CALL METHOD cl_gui_cfw=>flush.

IF g_custom_container1 IS INITIAL.

"To ensure that object is created only once

CREATE OBJECT g_custom_container1


EXPORTING
container_name = 'G_CONTAINER1'.

* Splitting the container


CREATE OBJECT g_split
EXPORTING
parent

= g_custom_container1

sash_position = 50 "Position of Splitter Bar (in Percent)


with_border = 0.With Border = 1 Without Border = 0

* Placing the containers in the splitter


g_top_container = g_split->top_left_container.
g_bottom_container = g_split->bottom_right_container.

Create an instance of ALV control


CREATE OBJECT g_grid1
EXPORTING
i_parent = g_bottom_container.

* Creating the document


CREATE OBJECT g_document
EXPORTING
style = 'ALV_GRID'.

*Top of page
PERFORM sub_top_of_page.

CALL METHOD g_grid1->set_table_for_first_display


EXPORTING
it_toolbar_excluding
is_layout
is_print

= i_exclude

= li_layout
= lx_print

CHANGING
it_outtab
it_fieldcatalog

= fp_i_orders1
= fp_i_fieldcatalog1

EXCEPTIONS
invalid_parameter_combination = 1
program_error

=2

too_many_lines

=3

OTHERS

= 4.

IF sy-subrc <> 0.
MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
ENDIF.
ENDIF.

ENDFORM.

SUB_DISPLAY_FIRSTALV

Subroutine to Display Second ALV

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

Form SUB_DISPLAY_SECONDALV

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

text

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

-->P_I_FIELDCATALOG2 text

-->P_I_ORDERS2 text

*----------------------------------------------------------------------*
FORM sub_display_secondalv USING fp_i_fieldcatalog2 TYPE lvc_t_fcat
fp_i_orders2

TYPE ty_t_orders2.

* Local data declaration


DATA: li_layout TYPE lvc_s_layo,
lx_print TYPE lvc_s_prnt.

* Layout for ALV


PERFORM sub_prepare_layout USING c_x
text-012
c_x
c_cellstyle
CHANGING li_layout.
* Use Flush
CALL METHOD cl_gui_cfw=>flush.

IF g_custom_container2 IS INITIAL.

"To ensure that object is created only once

CREATE OBJECT g_custom_container2


EXPORTING
container_name = 'G_CONTAINER2'.

Create an instance of ALV control


CREATE OBJECT g_grid2
EXPORTING
i_parent = g_custom_container2.

CALL METHOD g_grid2->set_table_for_first_display


EXPORTING
it_toolbar_excluding
is_layout
is_print

= i_exclude

= li_layout
= lx_print

CHANGING
it_outtab
it_fieldcatalog

= fp_i_orders2
= fp_i_fieldcatalog2

EXCEPTIONS
invalid_parameter_combination = 1
program_error

=2

too_many_lines

=3

OTHERS

= 4.

IF sy-subrc <> 0.
MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno

WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.


ENDIF.
ENDIF.

ENDFORM.

SUB_DISPLAY_SECONDALV

Subroutine to Display Third ALV

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

Form SUB_DISPLAY_THIRDALV

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

text

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

-->P_I_FIELDCATALOG3 text

-->P_I_INVSTATUS text

*----------------------------------------------------------------------*
FORM sub_display_thirdalv USING fp_i_fieldcatalog3 TYPE lvc_t_fcat
fp_i_invstatus TYPE ty_t_invstatus.

* Local data declaration


DATA: li_layout TYPE lvc_s_layo,
lx_print TYPE lvc_s_prnt.

* Layout for ALV


PERFORM sub_prepare_layout USING c_x

text-013
c_x
c_cellstyle
CHANGING li_layout.
* Use Flush
CALL METHOD cl_gui_cfw=>flush.

IF g_custom_container3 IS INITIAL.

"To ensure that object is created only once

CREATE OBJECT g_custom_container3


EXPORTING
container_name = 'G_CONTAINER3'.

Create an instance of ALV control


CREATE OBJECT g_grid3
EXPORTING
i_parent = g_custom_container3.

CALL METHOD g_grid3->set_table_for_first_display


EXPORTING
it_toolbar_excluding
is_layout
is_print

= i_exclude

= li_layout
= lx_print

CHANGING
it_outtab

= fp_i_invstatus

it_fieldcatalog

= fp_i_fieldcatalog3

EXCEPTIONS
invalid_parameter_combination = 1
program_error

=2

too_many_lines

=3

OTHERS

= 4.

IF sy-subrc <> 0.
MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
ENDIF.
ENDIF.
ENDFORM.

SUB_DISPLAY_THIRDALV

Subroutine for ALV layout

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

Form SUB_PREPARE_LAYOUT

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

text

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

<--P_LI_LAYOUT text

*----------------------------------------------------------------------*
FORM sub_prepare_layout USING fp_c_x
fp_title

TYPE xfeld

TYPE lvc_title

fp_smalltitle TYPE xfeld


fp_stylename TYPE lvc_fname

CHANGING fp_li_layout TYPE lvc_s_layo.

fp_li_layout-zebra

= fp_c_x.

fp_li_layout-grid_title = fp_title.
fp_li_layout-smalltitle = fp_smalltitle.
fp_li_layout-stylefname = fp_stylename.

ENDFORM.

SUB_PREPARE_LAYOUT

Subroutine for Report header, which will be created dynamically based on the fields selected by
the User for a User selected report layout.

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

Form SUB_TOP_OF_PAGE

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

text

*----------------------------------------------------------------------*
* --> p1

text

* <-- p2

text

*----------------------------------------------------------------------*
FORM sub_top_of_page.

* Local data Declaration


DATA: l_text TYPE sdydo_text_element,
l_datel (10) TYPE c,
l_dateh (10) TYPE c.

* Calling the methods for dynamic text


CALL METHOD g_document->add_text
EXPORTING
text

= text-014

sap_emphasis = cl_dd_area=>strong " For bold


sap_fontsize = cl_dd_area=>extra_large.

* Adding Line
CALL METHOD g_document->new_line.
* Adding Line
CALL METHOD g_document->new_line.

* Sold-to customer
IF s_kunnr-high IS NOT INITIAL.
CONCATENATE s_kunnr-low 'to' s_kunnr-high INTO l_text SEPARATED BY ' '.
ELSE.
l_text = s_kunnr-low.
ENDIF.

CALL METHOD g_document->add_text


EXPORTING
text

= text-015

sap_emphasis = cl_dd_area=>strong. For bold.

CALL METHOD g_document->add_gap

EXPORTING
width = '15'.

CALL METHOD g_document->add_text


EXPORTING
text = l_text.
CALL METHOD g_document->new_line.

CLEAR: l_text.
* Change date
* Converting date format
CALL FUNCTION 'CONVERT_DATE_TO_EXTERNAL'
EXPORTING
date_internal = s_erdat-low
IMPORTING
date_external = l_datel.

* Converting date format


CALL FUNCTION 'CONVERT_DATE_TO_EXTERNAL'
EXPORTING
date_internal = s_erdat-high
IMPORTING
date_external = l_dateh.

IF s_erdat-high IS NOT INITIAL.

CONCATENATE l_datel 'to' l_dateh INTO l_text SEPARATED BY ' '.


ELSE.
l_text = l_datel.
ENDIF.

CALL METHOD g_document->add_text


EXPORTING
text

= text-016

sap_emphasis = cl_dd_area=>strong. For bold.

CALL METHOD g_document->add_gap


EXPORTING
width = '24'.

CALL METHOD g_document->add_text


EXPORTING
text = l_text.
CALL METHOD g_document->new_line.

CLEAR: l_text.
IF NOT s_vkorg IS INITIAL.
* Sales Organization
IF s_vkorg-high IS NOT INITIAL.
CONCATENATE s_vkorg-low 'to' s_vkorg-high INTO l_text SEPARATED BY ' '.
ELSE.
l_text = s_vkorg-low.

ENDIF.

CALL METHOD g_document->add_text


EXPORTING
text

= text-017

sap_emphasis = cl_dd_area=>strong. For bold.

CALL METHOD g_document->add_gap


EXPORTING
width = '12'.

CALL METHOD g_document->add_text


EXPORTING
text = l_text.
CALL METHOD g_document->new_line.
ENDIF.

CLEAR : l_text.
IF NOT s_bsark IS INITIAL.
* PO Type
IF s_bsark-high IS NOT INITIAL.
CONCATENATE s_bsark-low 'to' s_bsark-high INTO l_text SEPARATED BY ' '.
ELSE.
l_text = s_bsark-low.
ENDIF.

CALL METHOD g_document->add_text


EXPORTING
text

= text-018

sap_emphasis = cl_dd_area=>strong. For bold.

CALL METHOD g_document->add_gap


EXPORTING
width = '32'.

CALL METHOD g_document->add_text


EXPORTING
text = l_text.
CALL METHOD g_document->new_line.
ENDIF.

* Display the data


CALL METHOD g_document->display_document
EXPORTING
parent = g_top_container.
* Calling the method of ALV to process top of page
CALL METHOD g_grid1->list_processing_events
EXPORTING
i_event_name = text-019
i_dyndoc_id = g_document.

ENDFORM.

SUB_TOP_OF_PAGE

Step4: Sending an Email with PDF attachment of the output.

The ALV output is first send to spool request, then the spool is converted to PDF and then the
PDF attachment is sent to Email.

a)

Converting Spool to PDF

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

Form SUB_CONVERT_SPOOL_TO_PDF

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

text

*----------------------------------------------------------------------*
* --> p1

text

* <-- p2

text

*----------------------------------------------------------------------*
FORM sub_convert_spool_to_pdf CHANGING fp_size
fp_i_mess_att TYPE ty_t_mess_att.

TYPES: ty_t_pdf TYPE STANDARD TABLE OF tline.

DATA: lv_buffer

TYPE string,

lv_spool_nr TYPE tsp01-rqident,


lw_mess_att TYPE solisti1,
li_pdf_output TYPE ty_t_pdf,
lw_pdf_output TYPE tline.

TYPE i

* Get the spool number


MOVE sy-spono TO lv_spool_nr.
CALL FUNCTION 'CONVERT_ABAPSPOOLJOB_2_PDF'
EXPORTING
src_spoolid

= lv_spool_nr "Spool Number

no_dialog

= space

dst_device

= 'LP01' "Printer Name

IMPORTING
pdf_bytecount

= fp_size "Output Size

TABLES
pdf

= li_pdf_output "Spool data in PDF Format

EXCEPTIONS
err_no_abap_spooljob = 1
err_no_spooljob
err_no_permission

=2
=3

err_conv_not_possible = 4
err_bad_destdevice

=5

user_cancelled

=6

err_spoolerror

=7

err_temseerror

=8

err_btcjob_open_failed = 9.

IF sy-subrc EQ 0.
LOOP AT li_pdf_output INTO lw_pdf_output.
TRANSLATE lw_pdf_output USING c_col1.
CONCATENATE lv_buffer lw_pdf_output INTO lv_buffer.

CLEAR :lw_pdf_output.
ENDLOOP.
TRANSLATE lv_buffer USING c_col2.
DO.
lw_mess_att = lv_buffer.
APPEND lw_mess_att TO fp_i_mess_att.
SHIFT lv_buffer LEFT BY c_255 PLACES.
IF lv_buffer IS INITIAL.
EXIT.
ENDIF.
CLEAR lw_mess_att.
ENDDO.
ENDIF.

ENDFORM.

SUB_CONVERT_SPOOL_TO_PDF

b) Sending PDF attachment to Email

The email addresses of the receivers are populated in table I_EMAIL which is passed in the
below subroutine as FP_I_EMAIL.

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

Form SUB_SEND_PDF_TO_MAIL

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

text

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

* --> p1

text

* <-- p2

text

*----------------------------------------------------------------------*
FORM sub_send_pdf_to_mail USING fp_size

TYPE i

fp_i_mess_att TYPE ty_t_mess_att


fp_i_email TYPE ty_t_email.

DATA: l_ref_bcs

TYPE REF TO cl_bcs,

l_ref_document TYPE REF TO cl_document_bcs,


lv_text

TYPE so_obj_des,

lv_data

TYPE bcsy_text,

lv_filesize TYPE so_obj_len,


lw_email

TYPE ty_email,

l_recipient TYPE REF TO if_recipient_bcs,


l_ref_bcs_exception TYPE REF TO cx_bcs,
sent_to_all TYPE os_boolean.

LOOP AT fp_i_email INTO lw_email.

TRY.
* Create persistent send request
l_ref_bcs = cl_bcs=>create_persistent ( ).
* Mail subject
lv_text = text-021.
APPEND text-021 TO lv_data.

* Create document
l_ref_document = cl_document_bcs=>create_document (
i_type = c_raw
i_text = lv_data
i_subject = lv_text).

* Create document reference


lv_filesize = fp_size.
CALL METHOD l_ref_document->add_attachment
EXPORTING
i_attachment_type = c_pdf
i_attachment_size = lv_filesize
i_attachment_subject = lv_text
i_att_content_text = fp_i_mess_att [].

* Set the document


l_ref_bcs->set_document (l_ref_document).

* Get Recipient Object


l_recipient = cl_cam_address_bcs=>create_internet_address (lw_email-smtp_addr).
*
* Add recipient with its respective attributes to send request
CALL METHOD l_ref_bcs->add_recipient
EXPORTING
i_recipient = l_recipient.

* Set that you don't need a Return Status E-mail


CALL METHOD l_ref_bcs->set_status_attributes
EXPORTING
i_requested_status = 'E'
i_status_mail

= 'E'.

* Set send immediately flag


l_ref_bcs->set_send_immediately( 'X' ).

* Send E-mail
CALL METHOD l_ref_bcs->send (
EXPORTING
i_with_error_screen = 'X'
RECEIVING
result

= sent_to_all).

IF sent_to_all = 'X'.
* E-mail sent successfully
MESSAGE i028 WITH text-020.
ENDIF.

CATCH cx_bcs INTO l_ref_bcs_exception.


IF l_ref_bcs_exception IS NOT INITIAL.
CLEAR l_ref_bcs_exception.
ENDIF.
ENDTRY.

ENDLOOP.
COMMIT WORK.

ENDFORM.

SUB_SEND_PDF_TO_MAIL

Step 5: Report output in foreground

Step 6: Functionality for executing the report in Background.


When the report is executed in background, the single spool of three ALV grids will be
created. For displaying the three ALV Grids in single spool, we will use the following FMs:
a)

REUSE_ALV_BLOCK_LIST_INIT

b) REUSE_ALV_BLOCK_LIST_APPEND
c)

REUSE_ALV_BLOCK_LIST_DISPLAY

The three field catalogs are built for this and the field catalog names are: LI_FIELDCAT1,
LI_FIELDCAT2 and LI_FIELDCAT3.
The header of the three ALVs in the spool is created as follows by using the below code:
PERFORM sub_header_list CHANGING li_events_1

li_events_2
li_events_3.

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

Form SUB_HEADER_LIST

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

text

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

<--P_LI_EVENTS_1 text

<--P_LI_EVENTS_2 text

<--P_LI_EVENTS_3 text

*----------------------------------------------------------------------*
FORM sub_header_list CHANGING fp_li_events_1 TYPE slis_t_event
fp_li_events_2 TYPE slis_t_event
fp_li_events_3 TYPE slis_t_event.

DATA: lw_events TYPE slis_alv_event.

lw_events-name = 'TOP_OF_PAGE'.
lw_events-form = 'TOP_OF_PAGE_1'."Subroutine name
APPEND lw_events TO fp_li_events_1.
CLEAR lw_events.
lw_events-name = 'TOP_OF_PAGE'.
lw_events-form = 'TOP_OF_PAGE_2'."Subroutine name
APPEND lw_events TO fp_li_events_2.
CLEAR lw_events.

lw_events-name = 'TOP_OF_PAGE'.
lw_events-form = 'TOP_OF_PAGE_3'."Subroutine name
APPEND lw_events TO fp_li_events_3.
CLEAR lw_events.

ENDFORM.

SUB_HEADER_LIST

Step 7: Displaying the three ALVs in background

lv_repid = sy-repid.

CALL FUNCTION 'REUSE_ALV_BLOCK_LIST_INIT'


EXPORTING
i_callback_program = lv_repid.

*call first ALV append list


CALL FUNCTION 'REUSE_ALV_BLOCK_LIST_APPEND'
EXPORTING
is_layout

= lv_layout

it_fieldcat

= li_fieldcat1

i_tabname
it_events

= 'I_ORDERS1'
= li_events_1[]

TABLES
t_outtab

= i_orders1

EXCEPTIONS
program_error

=1

maximum_of_appends_reached = 2
OTHERS

= 3.

IF sy-subrc <> 0.
MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
ENDIF.

CALL FUNCTION 'REUSE_ALV_BLOCK_LIST_APPEND'


EXPORTING
is_layout

= lv_layout

it_fieldcat

= li_fieldcat2

i_tabname
it_events

= 'I_ORDERS2'
= li_events_2

TABLES
t_outtab

= i_orders2

EXCEPTIONS
program_error

=1

maximum_of_appends_reached = 2
OTHERS

= 3.

IF sy-subrc <> 0.
MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
ENDIF.

CALL FUNCTION 'REUSE_ALV_BLOCK_LIST_APPEND'

EXPORTING
is_layout

= lv_layout

it_fieldcat

= li_fieldcat3

i_tabname
it_events

= 'I_INVSTATUS'
= li_events_3

TABLES
t_outtab

= i_invstatus

EXCEPTIONS
program_error

=1

maximum_of_appends_reached = 2
OTHERS

= 3.

IF sy-subrc <> 0.
MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
ENDIF.

* Get the print parameters


PERFORM sub_get_print_parameters.

NEW-PAGE PRINT ON PARAMETERS x_params


NO DIALOG.

lx_print-print = ' '.


lx_print-prnt_title = ' '.
lx_print-prnt_info = ' '.

lx_print-no_print_selinfos = 'X'. " Display no selection infos


lx_print-no_print_listinfos = 'X'. " Display no listinfos
lx_print-no_new_page = 'X'.

*display the data


CALL FUNCTION 'REUSE_ALV_BLOCK_LIST_DISPLAY'
EXPORTING
i_interface_check = ' '
is_print

= lx_print

EXCEPTIONS
program_error
OTHERS

=1

= 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.
COMMIT WORK.
NEW-PAGE PRINT OFF.

Subroutine to get print parameters

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

Form SUB_GET_PRINT_PARAMETERS

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

text

*----------------------------------------------------------------------*
* --> p1

text

* <-- p2

text

*----------------------------------------------------------------------*
FORM sub_get_print_parameters.

DATA: lv_valid.

CALL FUNCTION 'GET_PRINT_PARAMETERS'


EXPORTING
immediately = 'X'
no_dialog

= 'X'

line_size

= '255'

line_count
layout

= '65'
= 'X_65_255'

destination = 'LP01'
IMPORTING
out_parameters = x_params
valid

= lv_valid

EXCEPTIONS
OTHERS

= 1.

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.

SUB_GET_PRINT_PARAMETERS

Step 8: Report output in background.

The above spool output is finally sent as a PDF attachment to email addresses of multiple
persons.
Radio Buttons in the output of an ALV
As per business need there was a scenario for an ALV Report Output, It must have functionality
selecting only one row at a time. It is possible with Radio buttons.

NOTE: Here we have two types of Radio buttons Icons i.e. icon_wd_radio_button_empty (Empty
Radio Button) and icon_radiobutton (Selected Radio button)

Below are the steps to display Radio Buttons in the output of an ALV.

a)

Include ICONS.
INCLUDE <icons>.

b) Declare an internal table with the required fields and additional field called RADIO of type
CHAR of size 4.
c) Define a class to handle an even HOTSPOT_CLICK o trigger when we click on the radio
button icon. Definition as coded below.
* Handles the Even when user clicks on any row.
METHODS: handle_hotspot_click FOR EVENT hotspot_click

OF cl_gui_alv_grid
IMPORTING e_row_id.

d) Do the implementation for the same class to handle the event i.e. if we select a radio button,
then the already selected radio button should be deselect and new radio button should be
selected. Code as follows.
*&--------------------------------------------------------------*
*& METHOD handle_hotspot_click.
*&--------------------------------------------------------------*
*& On double clicking a particulat row
*&--------------------------------------------------------------*
METHOD handle_hotspot_click .

CLEAR : gs_emp.
READ TABLE gt_emp INTO gs_emp

WITH KEY radio = icon_radiobutton.


IF sy-subrc NE 0.

CLEAR gs_emp .
READ TABLE gt_emp INTO gs_emp INDEX e_row_id.
IF gs_emp-radio = icon_radiobutton.
gs_emp-radio = icon_wd_radio_button_empty.
MODIFY gt_emp INDEX e_row_id FROM gs_emp
TRANSPORTING radio.
ELSE.
gs_emp-radio = icon_radiobutton.
MODIFY gt_emp INDEX e_row_id FROM gs_emp

TRANSPORTING radio.
ENDIF.
ELSE .
gs_emp-radio = icon_wd_radio_button_empty.
MODIFY gt_emp INDEX sy-tabix FROM gs_emp
TRANSPORTING radio.
CLEAR gs_emp.
READ TABLE gt_emp INTO gs_emp INDEX e_row_id .
IF sy-subrc = 0.
gs_emp-radio = icon_radiobutton.
MODIFY gt_emp INDEX e_row_id FROM gs_emp
TRANSPORTING radio.
ENDIF.
ENDIF .

CALL METHOD cl_gui_cfw=>set_new_ok_code

EXPORTING
new_code = 'REFRESH'
*

IMPORTING

rc

.
ENDMETHOD.

e)

"handle_hotspot_click

Update the RADIO button with empty radio button in the internal table. Code as follows.

LOOP AT gt_emp INTO gs_emp.


gs_emp-radio = icon_wd_radio_button_empty.
MODIFY gt_emp FROM gs_emp TRANSPORTING radio.
ENDLOOP.
f) While preparing the field catalogue, Prepare field catalogue for that additional field RADIO
as coded below.
ls_fieldcat-reptext = 'Radio Button'.
ls_fieldcat-fieldname = 'RADIO'.
ls_fieldcat-ref_table = 'gt_emp'.
ls_fieldcat-icon

= 'X'.

"Icons

ls_fieldcat-hotspot = 'X'.

"Hotspot(Hand Symbol)

ls_fieldcat-col_pos = '1'.

g) Refresh the grid, Perform REFRESH action when the radio button is selected.
* Define local data
DATA:ls_stable TYPE lvc_s_stbl.

ls_stable-row = abap_true.

ls_stable-col = abap_true.

CALL METHOD gv_grid->refresh_table_display


EXPORTING
is_stable = ls_stable
EXCEPTIONS
finished = 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.

h)

Please click here for the demo program

Output of the Program:


Display more than one Internal Table in ALV using Object Oriented ABAP Programming
Applies to:
This document applies to SAP ECC 6.0, SAP Net weaver 2004s.
Scenario
Displaying more than one table in ALV grid report by splitting the custom container.
TABLE 2
TABLE 3
TABLE 1
Step by step Solution
Step 1: Creating Screen
Go to Screen painter Transaction Code SE51.
Create a screen with no 9010.

Provide description for the screen and click on the Layout Button.
Place a Custom container UI element and give name as CCONTAINER now you will have a screen
with custom container as below screen shot. Activate the Object.

Step 2: Flow Logic


Go to the Flow Logic Tab to write coding for PBO & PAI.

Step 3: Report
Go to SE38 and create a program in Z name space and paste the following code snippet.
*&---------------------------------------------------------------------*
*& Report ZPC_CONTAINER_CONTROL

*&---------------------------------------------------------------------*
REPORT zpc_container_control.
*Declaration
DATA: Splitter_1

TYPE REF TO cl_gui_splitter_container,

Splitter_2

TYPE REF TO cl_gui_splitter_container,

Container

TYPE REF TO cl_gui_custom_container,

Container_1 TYPE REF TO cl_gui_container,


Container_2 TYPE REF TO cl_gui_container,
Container_3 TYPE REF TO cl_gui_container,
Grid1

TYPE REF TO cl_gui_alv_grid,

Grid2

TYPE REF TO cl_gui_alv_grid,

Grid3

TYPE REF TO cl_gui_alv_grid.

DATA: Gt_sflight_1 TYPE TABLE OF sflight,


Gt_sflight_2 TYPE TABLE OF sflight,
Gt_sflight_3 TYPE TABLE OF sflight,
G_container TYPE scrfname VALUE 'CCONTAINER'.
DATA: ok_code TYPE sy-ucomm.
* fetching data from table for different internal tables
SELECT * FROM sflight INTO TABLE gt_sflight_1. Itab 1
SELECT * FROM sflight INTO TABLE gt_sflight_2 UP TO 3 ROWS. Itab 2
SELECT * FROM sflight INTO TABLE gt_sflight_3 UP TO 2 ROWS. Itab 3
CALL SCREEN 9010.

*&---------------------------------------------------------------------*
*& Module STATUS_9010 OUTPUT

*&---------------------------------------------------------------------*
MODULE status_9010 OUTPUT.
SET PF-STATUS 'TEST'.
*creating object reference for container
CREATE OBJECT container
EXPORTING
container_name = 'CCONTAINER'. Pass name of container created in Screen no 9010
*splitting the main container into 1 row & 2 coloum
CREATE OBJECT splitter_1
EXPORTING
Parent

= container

Rows

=1

Columns = 2.
*getting the reference for the splited container (row 1 & col 1 container)
CALL METHOD splitter_1->get_container
EXPORTING
Row

=1

Column = 1
RECEIVING
Container = container_1.
*getting the reference for the splited container (row 1 & col 2 container)
CALL METHOD splitter_1->get_container
EXPORTING
Row

=1

Column = 2
RECEIVING
Container = container_2.
*splitting the 2nd coloum container in to 2 rows & 1 coloum
CREATE OBJECT splitter_2
EXPORTING
Parent

= container_2

Rows

=2

Columns = 1.
*getting the reference for the splited container2 (row 1 & col 2 container)
CALL METHOD splitter_2->get_container
EXPORTING
Row

=1

Column = 1
RECEIVING
Container = container_2.
*getting the reference for the splited container2 (row 2 & col 1 container)
CALL METHOD splitter_2->get_container
EXPORTING
Row

=2

Column = 1
RECEIVING
Container = container_3.
*populating first internal table to the container
CREATE OBJECT container
EXPORTING

container_name = g_container.
CREATE OBJECT grid1
EXPORTING
i_parent = container_1.
CALL METHOD grid1->set_table_for_first_display
EXPORTING
i_structure_name = 'SFLIGHT'
CHANGING
it_outtab = gt_sflight_1.
*populating second internal table
CREATE OBJECT container
EXPORTING
container_name = g_container.
CREATE OBJECT grid2
EXPORTING
i_parent = container_2.
CALL METHOD grid2->set_table_for_first_display
EXPORTING
i_structure_name = 'SFLIGHT'
CHANGING
it_outtab = gt_sflight_2.
*populating third internal table
CREATE OBJECT container
EXPORTING
container_name = g_container.
CREATE OBJECT grid3

EXPORTING
i_parent = container_3.
CALL METHOD grid3->set_table_for_first_display
EXPORTING
i_structure_name = 'SFLIGHT'
CHANGING
it_outtab = gt_sflight_3.
ENDMODULE. STATUS_9010 OUTPUT
*&---------------------------------------------------------------------*
*& Module EXIT INPUT

*&---------------------------------------------------------------------*
MODULE exit INPUT.
*free the container memory when exit
CALL METHOD container->free.
LEAVE PROGRAM.
ENDMODULE. EXIT INPUT
*&---------------------------------------------------------------------*
*& Module USER_COMMAND_9010 INPUT

*&---------------------------------------------------------------------*
MODULE user_command_9010 INPUT.
CALL METHOD cl_gui_cfw=>dispatch.
CASE ok_code.
WHEN 'BACK'.
LEAVE SCREEN.
WHEN 'CANCEL'.
LEAVE PROGRAM.

WHEN 'EXIT'.
LEAVE SCREEN.
ENDCASE.
ENDMODULE. USER_COMMAND_9010 INPUT
Output

The report will display as below screen shot.

Word Wrap Functionality in ALV


Applies to:
SAP R/3 ECC 6.0, Release NW>04.
Introduction
At times, there is a requirement to set an ALV Column to word wrap so that the text gets moved
to the second line if there is too much text for the Column Width. ALV doesnt provide the
functionality to wrap the texts and hence, challenges are faced in case any column has long texts
to be displayed in single instance.
Purpose

The purpose of this document is to provide an option of word wrap to ALV functionality. Other
options may be possible; however, they only work around and doesnt provide end user the
flexibility to view the texts in single instance. Few of the other alternatives are listed below
You can make the column text as link and show truncated text so that even if the whole
text in a column is not seen, the user can see the entire text on mouse over.
You can implement functionality where in on clicking on the required cell, a popup
comes with the entire text.
However, in the above alternatives, incase end user needs to take print out of the report output,
he will miss out the truncated texts.
Dependencies/Pre-requisites/Assumption
Basic understanding of ALV, ABAP Programming is required to understand the flow and concept
of ALV reporting.
Technical Process
Limitations in Standard output of ALV
For demonstration purpose, I have used 2 columns Employee ID (width of Character
10) and Employee Name (width of Character 20). However, Employee Name field contains
values which are more than 20 characters.
Report output truncates the value after character 21
In case of Invoice printing/ Deliver document scenarios, this becomes as a challenge if
the long text gets truncated. Truncated texts wont make any sense to the end user and he
wont be able to take printouts!!!

Steps Involved to trigger Wrap Functionality


Identify the column that is required to be wrapped. Multiple columns can also be
wrapped as per requirement.

Identify the length to which the column needs to be wrapped and define a field of same
length. Different columns can be wrapped to different length, and hence logic needs to be
modified accordingly.
Usually, for ALV output, data records internal table is defined to populate output. In
order to implement wrap functionality, we need to declare another internal table with the
desired wrapped length. This internal table will contain wrapped texts for the column.
Internal table will need to have first column as identifier to match each record of parent
internal table.
Standard Function Module RKD_WORD_WRAP is used to wrap the desired text.
IMPORT Parameters
1.

TEXTLINE (Mandatory) - Source text line

2.

DELIMITER (Optional) - Indicator, which is used as a separator. SPACE is set in our


logic so that words dont get wrapped in between. In case CSV file needs to be
wrapped, delimiter needs to be set as comma

3.

OUTPUTLEN (Optional) - Maximum output line width. Default value is set to 35.

TABLES
1.

OUT_LINES (Optional) - All output lines after wrapping get stored as table

REUSE_ALV_EVENTS_GET is called to get the list of all active events for particular ALV
report output.
Populate appropriate FORM name for the event AFTER_LINE_OUTPUT.
Call function module REUSE_ALV_LIST_DISPLAY to populate report output. ALV List is
used for demonstration; however, ALV Grid can also be used.
Implement AFTER_LINE_OUTPUT event among other events for the ALV. Below
snapshot shows the logic that is required to trigger multiple lines.

See below for Sample code

*&--------------------------------------------------------------------*& Report ZTEST_WRAP


*&--------------------------------------------------------------------*& Wrap the column into multiple lines incase Column width is less
*&---------------------------------------------------------------------

REPORT ZTEST_WRAP.
TYPE-POOLS: SLIS.
TYPES: BEGIN OF TY_DATA,
EMP

TYPE CHAR10,

EMP_NAME TYPE CHAR100,


EMP_NAME1 TYPE CHAR20,
END OF TY_DATA.
TYPES: BEGIN OF TY_WRD,
EMP TYPE CHAR20,
END OF TY_WRD.
CONSTANTS: C_LEN TYPE I VALUE 100.
DATA: REPORT_ID LIKE SY-REPID,
IT_SENTENCE TYPE TABLE OF TY_WRD,
WA_WORD TYPE TY_WRD,
V_REPID TYPE SYST-REPID,
V_TABIX TYPE SYST-TABIX.
DATA: WS_TITLE TYPE LVC_TITLE VALUE 'An ALV Report'.
DATA: IT_EVT TYPE SLIS_T_EVENT,
IT_FLD TYPE SLIS_T_FIELDCAT_ALV,
WA_FLD TYPE SLIS_FIELDCAT_ALV,
WA_EVT TYPE SLIS_ALV_EVENT,
WA_LAY TYPE SLIS_LAYOUT_ALV.
DATA: WATAB TYPE TY_DATA,
I_DATA TYPE STANDARD TABLE OF TY_DATA,
COUNT TYPE I VALUE 0.
* Number of records

DO 4 TIMES.
COUNT = COUNT + 1.
CASE COUNT.
WHEN 1.
WATAB-EMP = '10'.
WATAB-EMP_NAME = 'Purpose of this tutorial is to provide an easy
and quick reference which may be used as a
guide'.
APPEND WATAB TO I_DATA.
CLEAR WATAB.
WHEN 2.
WATAB-EMP = '20'.
WATAB-EMP_NAME = 'Coding is done for ALV List Display'.
APPEND WATAB TO I_DATA.
CLEAR WATAB.
WHEN 3.
WATAB-EMP = '30'.
WATAB-EMP_NAME = 'Same functionality can be implemented for ALV
Grid Display also'.
APPEND WATAB TO I_DATA.
CLEAR WATAB.
ENDCASE.
ENDDO.
REPORT_ID = SY-REPID.
CLEAR WA_FLD.
WA_FLD-FIELDNAME = 'EMP'.

WA_FLD-REF_TABNAME = 'I_DATA'.
WA_FLD-SELTEXT_L = 'EMP. ID'.
WA_FLD-REF_FIELDNAME = 'EMP'.
APPEND WA_FLD TO IT_FLD.
CLEAR WA_FLD.
WA_FLD-FIELDNAME = 'EMP_NAME1'.
WA_FLD-INTTYPE = 'CHAR'.
WA_FLD-OUTPUTLEN = 20.
WA_FLD-INTLEN = 20.
WA_FLD-SELTEXT_L = 'EMP. NAME'.
WA_FLD-DDICTXT = 'L'.
APPEND WA_FLD TO IT_FLD.
* Word Wrap the text in multiple lines
LOOP AT I_DATA INTO WATAB.
V_TABIX = SY-TABIX.
CLEAR: IT_SENTENCE [].
CALL FUNCTION 'RKD_WORD_WRAP'
EXPORTING
TEXTLINE
OUTPUTLEN

= WATAB-EMP_NAME
= C_LEN

TABLES
OUT_LINES

= IT_SENTENCE

EXCEPTIONS
OUTPUTLEN_TOO_LARGE
OTHERS
.

=2

=1

IF SY-SUBRC eq 0.
IF NOT IT_SENTENCE IS INITIAL.
READ TABLE IT_SENTENCE INTO WA_WORD INDEX 1.
WATAB-EMP_NAME1 = WA_WORD-EMP.
MODIFY I_DATA FROM WATAB INDEX V_TABIX.
ENDIF.
ENDIF.
ENDLOOP.
* Get event. We will handle BEFORE and AFTER line output
CALL FUNCTION 'REUSE_ALV_EVENTS_GET'
IMPORTING
ET_EVENTS = IT_EVT.
READ TABLE IT_EVT INTO WA_EVT WITH KEY NAME = SLIS_EV_AFTER_LINE_OUTPUT.
WA_EVT-FORM = SLIS_EV_AFTER_LINE_OUTPUT.
MODIFY IT_EVT FROM WA_EVT INDEX SY-TABIX.
WA_LAY-EDIT = 'X'.
CALL FUNCTION 'REUSE_ALV_LIST_DISPLAY'
EXPORTING
I_CALLBACK_PROGRAM = REPORT_ID
IS_LAYOUT
IT_FIELDCAT
IT_EVENTS

= WA_LAY
= IT_FLD
= IT_EVT

TABLES
T_OUTTAB

= I_DATA

EXCEPTIONS
PROGRAM_ERROR

=1

OTHERS

= 2.

IF SY-SUBRC <> 0.
* Exceptions will be handled as per requirement
ENDIF.
*---------------------------------------------------------------------*
* FORM AFTER_LINE_OUTPUT

*---------------------------------------------------------------------*
FORM AFTER_LINE_OUTPUT USING RS_LINEINFO TYPE SLIS_LINEINFO.
CLEAR: IT_SENTENCE, WATAB.
READ TABLE I_DATA INTO WATAB INDEX RS_LINEINFO-TABINDEX.
CHECK SY-SUBRC = 0.
CALL FUNCTION 'RKD_WORD_WRAP'
EXPORTING
TEXTLINE = WATAB-EMP_NAME
OUTPUTLEN = C_LEN
TABLES
OUT_LINES = IT_SENTENCE.
DESCRIBE TABLE IT_SENTENCE LINES V_TABIX.
CHECK V_TABIX > 1.
LOOP AT IT_SENTENCE INTO WA_WORD FROM 2.
WRITE: / SY-VLINE,
12 SY-VLINE,
13 WA_WORD-EMP,
33 SY-VLINE.
ENDLOOP.
ENDFORM. "after_line_output

Modified Output of ALV after Wrap functionality


For demonstration purpose, I have used 2 columns Employee ID (width of Character
10) and Employee Name (width of Character 20)
String of length 95 is passed to Employee name field which is of CHAR20 and this column
is wrapped so that whole string is shown in multiple lines

Go to SP01 transaction to check Print output.

Benefits
Desired Columns can be wrapped and shown in the same report output.
Multiple columns can be wrapped as per requirement. Also, different columns can be
wrapped to different lengths, and hence logic needs to be modified accordingly.

Wrap length can be set dynamically and changed during run time as per desired
functionality.
Even though string gets wrapped into multiple lines, however its treated as one line
item. This provides an advantage incase HOTSPOT functionality is required over line
items.
No hassles or logic is required to ensure that line gets split at words instead of in
between the words as standard SAP function modules are used to achieve the same. Also,
we can set custom delimiters for wrap functionality. Hence, CSV file can be wrapped
at comma.
Print output will also show wrapped output and hence, word wrap functionality is quite
helpful in scenarios like Invoice printing, Delivery Document.
Conclusion
Though SAP doesnt provide Wrap functionality along with ALV standard features,
however, we can achieve this though custom logic and coding.
This document can be referred by technical consultants to understand how wrap
functionality can be implemented for ALV. This document doesnt explain about ALV
Events functionality.
The code can be reused with minimal changes as per requirement.
ALV - Consistency Check
Problem:
Often for our ALV Programs output, we initially get a short dump or no data display or dump
during sorting and rearrangement of columns. These things generally results from wrong design
of Field catalog, Layout or Sorting Tables.
Tracking the error and rectifying the code definitely takes some good amount of efforts incase its
a complex ALV Report.
Solution:
SAP provides quite a simple and convenient way to trace out these errors and eases the task of
debugging by providing a detailed description of the errors and probable solutions.
To use this, press SHIFT + (Double Right Click) on any empty section of the ALV output screen of
the Report as shown below in the output of the sample Report:

The below SAP standard analysis screen opens up. This screen gives a detailed technical report
of the ALV Grid control used in the Report program.

It has different tabs for FieldCatalog, Layout, Sort etc. which when clicked gives individual
analysis report.
Example sample:
The example here displays 3 errors in the Report program in the initial display. This screen
basically shows a summary of errors initially. When the buttons for Fcat or Layout are clicked
above, individual errors are displayed then.

The short description of these errors is available along with a long description of selected errors.
Also it displays in tabular format the detailed design of the Fcat, Layout and other tables used to
build up the ALV display. For example, below table shows how I have build my layout in the
program.

This can be verified from the code also as shown below:

Correction of errors:
This analysis report helps us to easily correct any kind of errors as done below.
Let us look into the 2nd displayed error:

From the error description it is quite evident that we have used a Data Element in the Fcat, which
basically doesnt exist in the Data Dictionary. When we look into the Fcat code in the program, we
find:

We change WERKS to WERKS_D.

Activate and run the Report and submit for analysis by using SHIFT+Double Right click. Below is
the result. The errors got reduced to 2.

Since it points out that field PLANT has a wrong value which means that the declaration of PLANT
in the main Internal table, where values of PLANT are stored, might be wrong. We look into the
declaration part of the Internal table. The PLANT field is declared with a wrong Data Type
WERKS in the Table. It should be WERKS_D.
Correction of Error:
Before change:

After change

Activate and run the Report and submit for analysis again. This is what we get as the analysis
result now:

Apart from this a lot of other information can be collected from this SAP standard analysis result.
Hence we can conclude that this is quite a handy tool for analyzing the consistency of our ALV
report generated. We can declare the ALV Grid error free only if no inconsistencies are found in
this result.
Displaying ALV using a Docking Container
Objective
This program is used to display Docking Container. Sometimes we get the requirement to place
the container at any corner as resizable Ex: ALV and selection parameters in same Screen. To
serve purpose we will go for docking container.
Introduction
As we know that a SAP Container is a control that accommodates other controls, such as the SAP
Tree Control, SAP Picture Control, SAP Text edit Control, SAP Splitter Control, and so on. It
manages these controls logically in a collection, and provides a physical area in which they are
displayed. All controls live in a container. Since containers are themselves controls, you can nest
them. There are five kinds of SAP Containers:
SAP Custom Container: display controls in an area defined on a normal screen using the Screen
Painter. Class: CL_GUI_CUSTOM_CONTAINER
SAP Dialog Box Container: display controls in a modal dialog box or full screen.
Class: CL_GUI_DIALOGBOX_CONTAINER
SAP Docking Container: The SAP Docking Container allows you to attach a control to any of the
four edges of a screen as a resizable screen area. You can also detach it so that it becomes an
independent modal dialog box. Class: CL_GUI_DOCKING_CONTAINER

SAP Splitter Container: to display more than one control in a given area by dividing it into cells.
Class: CL_GUI_SPLITTER_CONTAINER
SAP Easy Splitter Container: this container allows us to divide an area into two cells with a
control in each. The cells are separated by a moveable splitter bar.
Class: CL_GUI_EASY_SPLITTER_CONTAINER.
Here we display the functionality of docking container.
SAP Docking container can be attached on or more areas of screen. To reattach the docking
container to a different edge of the screen we use DOCK_AT method. To switch the status of the
Docking Container between fixed (docking) and free (floating) we use the method FLOAT.
We can use docking container in web Dynpro and sub screen also.
Below is the Example of Docking Container.
Code Snippet
REPORT z75_docking MESSAGE-ID yxnmc.
TABLES: mara.
*---------------------------------------------------------------------*
*

WORK AREAS

*---------------------------------------------------------------------*
DATA:
* Material Data
BEGIN OF wa_mara,
matnr TYPE mara-matnr,

" Material No.

mtart TYPE mara-mtart,

" Material Type

bismt TYPE mara-bismt,

" Old material No.

matkl TYPE mara-matkl,

" Material group

meins TYPE mara-meins,

" Base Unit of Measure

brgew TYPE mara-brgew,

" Gross Weight

ntgew TYPE mara-ntgew,

" Net Weight

gewei TYPE mara-gewei,

" Weight Unit

END OF wa_mara,

* Field Catalog

wa_fieldcat TYPE lvc_s_fcat.

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

INTERNAL TABLES

*---------------------------------------------------------------------*
DATA:
* For Material Data
t_mara LIKE STANDARD TABLE OF wa_mara,
* For Field Catalog
t_fieldcat TYPE lvc_t_fcat.

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

WORK VARIABLES

*---------------------------------------------------------------------*
DATA:
* User Command
ok_code TYPE sy-ucomm,
* Reference Variable for Docking Container
r_dock_container TYPE REF TO cl_gui_docking_container,
* Reference Variable for alv grid
r_grid TYPE REF TO cl_gui_alv_grid.

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

START OF SELECTION

*---------------------------------------------------------------------*
START-OF-SELECTION.
* To Display the Data
PERFORM display_output.
*&--------------------------------------------------------------------*
*&

Form display_output

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

To Call the screen & display the output

*---------------------------------------------------------------------*
* There are no interface parameters to be passed to this subroutine.*
*---------------------------------------------------------------------*
FORM display_output .
* To fill the Field Catalog
PERFORM fill_fieldcat USING :
'MATNR' 'T_MARA'

'Material No.',

'MTART' 'T_MARA'

'Material Type',

'BISMT' 'T_MARA'

'Old Material No.',

'MATKL' 'T_MARA'

'Material Group',

'MEINS' 'T_MARA'

'Base Unit of Measure',

'BRGEW' 'T_MARA'

'Gross Weight',

'NTGEW' 'T_MARA'

'Net Weight',

'GEWEI' 'T_MARA'

'Weight Unit'.

CALL SCREEN 500.


ENDFORM.

" Display_output

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

Form FILL_FIELDCAT

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

To Fill the Field Catalog

*---------------------------------------------------------------*
* Three Parameters are passed
* pv_field TYPE any for Field

*
*

* pv_tabname TYPE any for Table Name


* pv_coltext TYPE any for Header Text
*---------------------------------------------------------------*
FORM fill_fieldcat USING pv_field TYPE any
pv_tabname TYPE any
pv_coltext TYPE any .

wa_fieldcat-fieldname = pv_field.
wa_fieldcat-tabname = pv_tabname.
wa_fieldcat-coltext = pv_coltext.

APPEND wa_fieldcat TO t_fieldcat.


CLEAR wa_fieldcat.
ENDFORM.

Create the Screen 0500.

Flow Logic for Screen 500.

" FILL_FIELDCAT

*
*

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

Module STATUS_0500 OUTPUT

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

To Set GUI Status & Title

*----------------------------------------------------------------------*
MODULE status_0500 OUTPUT.

SET PF-STATUS 'STATUS'.


SET TITLEBAR 'TITLE'.

ENDMODULE.

" STATUS_0500 OUTPUT

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

Module CREATE_OBJECTS OUTPUT

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

To Call the Docking Container & Display Method

*----------------------------------------------------------------------*
MODULE create_objects OUTPUT.
* Create a Docking container and dock the control at right side of screen
CHECK r_dock_container IS INITIAL.
CREATE OBJECT r_dock_container
EXPORTING
side
extension

= cl_gui_docking_container=>dock_at_right
= 780

caption

= 'Materials'

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.

" IF sy-subrc <> 0.

* To Create the Grid Instance


CREATE OBJECT r_grid
EXPORTING
i_parent

= r_dock_container

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.

" IF sy-subrc <> 0.

* Formatted Output Table is Sent to Control


CALL METHOD r_grid->set_table_for_first_display
CHANGING
it_outtab

= t_mara

it_fieldcatalog

= t_fieldcat

it_sort

it_filter

EXCEPTIONS
invalid_parameter_combination = 1
program_error

=2

too_many_lines

=3

OTHERS

=4

.
IF sy-subrc <> 0.
MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
ENDIF.
ENDMODULE.

" IF sy-subrc <> 0.


" CREATE_OBJECTS OUTPUT

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

Module USER_COMMAND_0500 INPUT

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

To Fetch the Material Data & Refresh Table after get User Command*

*----------------------------------------------------------------------*
MODULE user_command_0500 INPUT.

CASE ok_code.
WHEN 'EXECUTE'.
SELECT matnr

" material no.

mtart

" material type

bismt

" old material no.

matkl

" material group

meins

" base unit of measure

brgew

" gross weight

ntgew

" net weight

gewei

" weight unit

FROM mara
INTO TABLE t_mara
WHERE mtart = mara-mtart.
IF sy-subrc <> 0.
ENDIF.

" IF sy-subrc EQ 0.

CALL METHOD r_grid->refresh_table_display.


WHEN 'BACK' OR 'CANCEL' OR 'EXIT'.
LEAVE TO SCREEN 0.
ENDCASE.
ENDMODULE.

" CASE ok_code.


" USER_COMMAND_0500 INPUT

Set GUI Status as shown in figure

Application Toolbar

Screen 500.

Output

Creating dynamic ALV with dynamic editable columns and dynamic colors to the columns based
on condition
This Program will help to create dynamic ALV with dynamic editable columns and dynamic
colors to the columns based on condition.

*&---------------------------------------------------------------------*
*& Report YSHU_SAI_ALV_DYNAMIC
*&---------------------------------------------------------------------*
REPORT YSHU_SAI_ALV_DYNAMIC.
TYPE-POOLS : ABAP.
FIELD-SYMBOLS: <LINE> TYPE STANDARD TABLE,
<WA> TYPE ANY.
DATA: DYN_TABLE
DYN_LINE

TYPE REF TO DATA,

TYPE REF TO DATA,

WA_FIELDCAT TYPE LVC_S_FCAT,


IT_FIELDCAT TYPE LVC_T_FCAT,
IT_FIELDCAT_1 TYPE LVC_T_FCAT,
OK_CODE

TYPE SY-UCOMM.

TYPES: BEGIN OF TY_1,


TIME TYPE T,
REMARKS TYPE CHAR50,
END OF TY_1.
DATA: LV_FLD_1 TYPE TY_1.

START-OF-SELECTION.
* Create dynamic table stricture..
PERFORM GET_TABLE_STRUCTURE.

*Create dynamic internale table..


PERFORM CREATE_ITAB_DYNAMICALLY.
* filling the data into dynamic internal table..
PERFORM GET_DATA.
END-OF-SELECTION.
* display dynamic interal data.
PERFORM DISPLAY_ALV_REPORT.

CALL SCREEN 2000.

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

Form get_table_structure

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

Get structure of an SAP table

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

FORM GET_TABLE_STRUCTURE.
DATA : REF_TABLE_DESCR TYPE REF TO CL_ABAP_STRUCTDESCR,
IT_TABDESCR TYPE ABAP_COMPDESCR_TAB,
WA_TABDESCR TYPE ABAP_COMPDESCR,
LV_NO_DAYS TYPE P,
LV_DATE

TYPE SY-DATUM,

LV_DATUM TYPE SY-DATUM,


LV_DAY

TYPE TEXT40,

LV_WEEK_DAY TYPE TEXT10,


LIN

TYPE SY-TFILL,

C_REM

TYPE STRING VALUE 'Remarks' .

* fill the data into fieldcatlog of static field


PERFORM BUILD_FIELDCATALOG USING IT_FIELDCAT.
* Return structure of the table.
REF_TABLE_DESCR ?= CL_ABAP_TYPEDESCR=>DESCRIBE_BY_DATA( LV_FLD_1 ).
IT_TABDESCR[] = REF_TABLE_DESCR->COMPONENTS[].

* Get no of days in a month..


CALL FUNCTION 'HR_E_NUM_OF_DAYS_OF_MONTH'
EXPORTING
P_FECHA

= SY-DATUM

IMPORTING
NUMBER_OF_DAYS = LV_NO_DAYS.

* get no of lines..
DESCRIBE TABLE IT_FIELDCAT LINES LIN .

LV_DATUM

= SY-DATUM.

LV_DATUM+6(2)

= 1.

WA_FIELDCAT-COL_POS = LIN.
* fill dynamic field into fields cate..
DO LV_NO_DAYS TIMES.
LV_DATE = LV_DATUM + SY-INDEX - 1.
CALL FUNCTION 'DATE_TO_DAY'
EXPORTING
DATE = LV_DATE
IMPORTING
WEEKDAY = LV_WEEK_DAY.

CONCATENATE LV_DATE+6(2) ',' LV_WEEK_DAY INTO LV_DAY.

LOOP AT IT_TABDESCR INTO WA_TABDESCR.


WA_FIELDCAT-COL_POS = WA_FIELDCAT-COL_POS + 1 .
IF SY-TABIX = 1.
WA_FIELDCAT-FIELDNAME = LV_DATE .
WA_FIELDCAT-COLTEXT = LV_DAY.
ELSE.
CONCATENATE LV_DATE '_R' INTO WA_FIELDCAT-FIELDNAME.
WA_FIELDCAT-COLTEXT = C_REM."'Remarks'.
ENDIF.
WA_FIELDCAT-INTTYPE = WA_TABDESCR-TYPE_KIND.
WA_FIELDCAT-INTLEN = WA_TABDESCR-LENGTH / 2.

APPEND WA_FIELDCAT TO IT_FIELDCAT.


ENDLOOP.
ENDDO.

ENDFORM.

"get_table_structure

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

Form create_itab_dynamically

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

Create internal table dynamically

*----------------------------------------------------------------------*
FORM CREATE_ITAB_DYNAMICALLY.
* Create dynamic internal table and assign to Field-Symbol
CLEAR WA_FIELDCAT.
*move fields from IT_FIELDCAT into IT_FIELDCAT_1.
IT_FIELDCAT_1 = IT_FIELDCAT.
* Use ref table CALENDAR_TYPE and ref field 'COLTAB'

WA_FIELDCAT-FIELDNAME = 'CELLCOLOR'.
WA_FIELDCAT-REF_TABLE = 'CALENDAR_TYPE'.
WA_FIELDCAT-REF_FIELD = 'COLTAB'.
APPEND WA_FIELDCAT TO IT_FIELDCAT.
CLEAR WA_FIELDCAT.

* create a table type 'ZCELLSTYL' with field 'CELLSTYLE' of type LVC_T_STYL


WA_FIELDCAT-FIELDNAME = 'CELLSTYLE'.
WA_FIELDCAT-REF_TABLE = 'ZCELLSTYL'.
WA_FIELDCAT-REF_FIELD = 'CELLSTYLE'.
APPEND WA_FIELDCAT TO IT_FIELDCAT.

*Create a dynamic table with IT_FIELDCAT.


* and Use IT_FIELDCAT_1 to display ALV.
CALL METHOD CL_ALV_TABLE_CREATE=>CREATE_DYNAMIC_TABLE
EXPORTING
IT_FIELDCATALOG = IT_FIELDCAT
IMPORTING
EP_TABLE

= DYN_TABLE.

ASSIGN DYN_TABLE->* TO <LINE>.


* Create dynamic work area and assign to Field Symbol
CREATE DATA DYN_LINE LIKE LINE OF <LINE> .
ASSIGN DYN_LINE->* TO <WA> .

ENDFORM.

"create_itab_dynamically

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

Form get_data

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

Populate dynamic itab

*----------------------------------------------------------------------*
FORM GET_DATA.

FIELD-SYMBOLS: <FS_PERNR> TYPE ANY,


<FS_FIELD> TYPE ANY,
<FS_COLOR> TYPE LVC_T_SCOL,
<FS_STYLE> TYPE LVC_T_STYL,
<WA_FIELDCAT> TYPE LVC_S_FCAT.

DATA: IT_CELLCOLOR TYPE LVC_T_SCOL,


IT_CELLSTYLE TYPE LVC_T_STYL,
L_DAY_P

TYPE P,

LV_MOD_DATE TYPE DATUM,


C_HOL

TYPE CHAR10 VALUE ' Holiday '.

ASSIGN COMPONENT 'PERNR' OF STRUCTURE <WA> TO <FS_PERNR>.


<FS_PERNR> = 1 .

LOOP AT IT_FIELDCAT ASSIGNING <WA_FIELDCAT> ."WHERE


* adde color to dynamci fields..
ASSIGN COMPONENT 'CELLCOLOR' OF STRUCTURE <WA> TO <FS_COLOR>.
IF <WA_FIELDCAT>-INTTYPE = 'T'.
PERFORM MODIFY_CELL_COLOR USING <WA_FIELDCAT>-FIELDNAME
CHANGING IT_CELLCOLOR.
<FS_COLOR> = IT_CELLCOLOR.
ELSE.

IF <WA_FIELDCAT>-FIELDNAME+8(2) = '_R'.
LV_MOD_DATE = <WA_FIELDCAT>-FIELDNAME+0(8).
L_DAY_P = LV_MOD_DATE MOD 7.
IF L_DAY_P > 1.
L_DAY_P = L_DAY_P - 1.
ELSE.
L_DAY_P = L_DAY_P + 6.
ENDIF.

IF L_DAY_P = 6 OR L_DAY_P = 7.
PERFORM MODIFY_CELL_COLOR USING <WA_FIELDCAT>-FIELDNAME
CHANGING IT_CELLCOLOR.
<FS_COLOR> = IT_CELLCOLOR.

ASSIGN COMPONENT <WA_FIELDCAT>-FIELDNAME OF STRUCTURE <WA> TO


<FS_FIELD>.
<FS_FIELD> = C_HOL."' Holiday '.
ENDIF.
ENDIF.
ENDIF.
*Dynamic editable
IF <WA_FIELDCAT>-FIELDNAME+0(8) = SY-DATUM.
ASSIGN COMPONENT 'CELLSTYLE' OF STRUCTURE <WA> TO <FS_STYLE>.
PERFORM EDITABLE_CELL USING <WA_FIELDCAT>-FIELDNAME
CHANGING IT_CELLSTYLE.
<FS_STYLE> = IT_CELLSTYLE.
ENDIF.
ENDLOOP.
APPEND <WA> TO <LINE>.

ENDFORM.

"get_data

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

Form modify_cell_color

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

-->P_FIELDNAME text

-->PT_CELLCOLOR text

*----------------------------------------------------------------------*
FORM MODIFY_CELL_COLOR USING P_FIELDNAME TYPE LVC_FNAME
CHANGING PT_CELLCOLOR TYPE TABLE.

DATA L_CELLCOLOR TYPE LVC_S_SCOL.


DATA : LV_DATE TYPE DATUM.
DATA: DAY_P TYPE P.
CLEAR L_CELLCOLOR.
IF P_FIELDNAME+8(2) = '_R'.
LV_DATE = P_FIELDNAME+8(2).
ELSE.
LV_DATE = P_FIELDNAME.
ENDIF.

DAY_P = LV_DATE MOD 7.

IF DAY_P > 1.
DAY_P = DAY_P - 1.
ELSE.
DAY_P = DAY_P + 6.
ENDIF.

IF DAY_P = 6 OR DAY_P = 7.
IF P_FIELDNAME+8(2) = '_R'.
L_CELLCOLOR-COLOR-COL = 7.

" Red.

L_CELLCOLOR-COLOR-INT = 0.
L_CELLCOLOR-COLOR-INV = 0.
ELSE.
L_CELLCOLOR-COLOR-COL = 7.

" Red.

L_CELLCOLOR-COLOR-INT = 1.
L_CELLCOLOR-COLOR-INV = 1.
ENDIF.
L_CELLCOLOR-FNAME = P_FIELDNAME.
APPEND L_CELLCOLOR TO PT_CELLCOLOR.

ELSE.
CLEAR L_CELLCOLOR.
ENDIF.

IF P_FIELDNAME+0(8) = SY-DATUM.

L_CELLCOLOR-FNAME = P_FIELDNAME.
L_CELLCOLOR-COLOR-COL = 3.

" Red.

L_CELLCOLOR-COLOR-INT = 1.
L_CELLCOLOR-COLOR-INV = 1.
APPEND L_CELLCOLOR TO PT_CELLCOLOR.
CONCATENATE P_FIELDNAME '_R' INTO L_CELLCOLOR-FNAME .
L_CELLCOLOR-COLOR-COL = 3.

" Red.

L_CELLCOLOR-COLOR-INT = 1.
L_CELLCOLOR-COLOR-INV = 1.
APPEND L_CELLCOLOR TO PT_CELLCOLOR.
ENDIF.

ENDFORM.

" MODIFY_CELL_COLOR

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

Form BUILD_FIELDCATALOG

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

Build Fieldcatalog for ALV Report, using SAP table structure

*----------------------------------------------------------------------*
FORM BUILD_FIELDCATALOG USING P_IT_FIELDCAT TYPE
LVC_T_FCAT."SLIS_T_FIELDCAT_ALV .
** ALV Function module to build field catalog from SAP table structure

CALL FUNCTION 'LVC_FIELDCATALOG_MERGE'


EXPORTING
* I_BUFFER_ACTIVE

I_STRUCTURE_NAME

= 'ZDYNAMIC_ALV_STR'

* I_CLIENT_NEVER_DISPLAY
* I_BYPASSING_BUFFER

= 'X'
=

* I_INTERNAL_TABNAME

CHANGING
CT_FIELDCAT

= P_IT_FIELDCAT

EXCEPTIONS
INCONSISTENT_INTERFACE
PROGRAM_ERROR
OTHERS

=1

=2
=3

.
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.

" BUILD_FIELDCATALOG

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

Form DISPLAY_ALV_REPORT

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

Display report using ALV grid

*----------------------------------------------------------------------*
FORM DISPLAY_ALV_REPORT.
DATA :G_GRID_I

TYPE REF TO CL_GUI_ALV_GRID,

G_CUSTOM_CONTAINER_I TYPE REF TO CL_GUI_CUSTOM_CONTAINER,


I_CONTAINER
GS_LAYOUT

TYPE SCRFNAME VALUE 'ALV',


TYPE LVC_S_LAYO.

CREATE OBJECT G_CUSTOM_CONTAINER_I


EXPORTING
CONTAINER_NAME = I_CONTAINER.

CREATE OBJECT G_GRID_I


EXPORTING
I_PARENT = G_CUSTOM_CONTAINER_I.

GS_LAYOUT-COL_OPT

= 'X'.

GS_LAYOUT-CWIDTH_OPT = 'X'.
GS_LAYOUT-CTAB_FNAME = 'CELLCOLOR'.

GS_LAYOUT-STYLEFNAME = 'CELLSTYLE'.

CALL METHOD G_GRID_I->SET_TABLE_FOR_FIRST_DISPLAY


EXPORTING
IS_LAYOUT
*

= GS_LAYOUT

IT_TOOLBAR_EXCLUDING = LT_EXCLUDE[]
CHANGING
IT_FIELDCATALOG
IT_OUTTAB

= IT_FIELDCAT_1

= <LINE>.

*For Editable alv...


CALL METHOD G_GRID_I->SET_READY_FOR_INPUT
EXPORTING
I_READY_FOR_INPUT = 1.

ENDFORM.

" DISPLAY_ALV_REPORT

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

Module USER_COMMAND_2000 INPUT

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

text

*----------------------------------------------------------------------*
MODULE USER_COMMAND_2000 INPUT.
CASE OK_CODE.
WHEN 'BACK'.
LEAVE TO SCREEN 0.
ENDCASE.
ENDMODULE.

" USER_COMMAND_2000 INPUT

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

Module STATUS_2000 OUTPUT

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

text

*----------------------------------------------------------------------*
MODULE STATUS_2000 OUTPUT.
SET PF-STATUS 'STATUS_2000'.
* SET TITLEBAR 'xxx'.

ENDMODULE.

" STATUS_2000 OUTPUT

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

Form EDITABLE_CELL

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

-->P_IT_CELLSTYLE text

*----------------------------------------------------------------------*
FORM EDITABLE_CELL USING

P_FIELDNAME TYPE LVC_FNAME

CHANGING P_IT_CELLSTYLE TYPE LVC_T_STYL.

DATA : WA_CELLSTYLE TYPE LVC_S_STYL.


WA_CELLSTYLE-FIELDNAME = P_FIELDNAME .
WA_CELLSTYLE-STYLE

= CL_GUI_ALV_GRID=>MC_STYLE_ENABLED.

INSERT WA_CELLSTYLE INTO TABLE P_IT_CELLSTYLE.

ENDFORM.

Output:
Output:

" EDITABLE_CELL

The output shows current month calendar with all the Saturday and Sundays dynamically red
color will apply and for the current date yellow color with Editable fields.
Printing a line after Subtotaling in an ALV
REPORT ztest_alv.
*---type pools
TYPE-POOLS: slis.
*---internal tables
DATA: BEGIN OF it_flight OCCURS 0,
carrid LIKE sflight-carrid,
connid LIKE sflight-connid,
fldate LIKE sflight-fldate,
seatsmax LIKE sflight-seatsmax,
seatsocc LIKE sflight-seatsocc,
END OF it_flight,
*--internal tables for alv
it_fieldcat TYPE slis_t_fieldcat_alv,
wa_fcat LIKE LINE OF it_fieldcat,
layout TYPE slis_layout_alv,
it_sort type slis_t_sortinfo_alv,
wa_sort like line of it_sort.
*---start-of-selection .
START-OF-SELECTION.

CALL FUNCTION 'REUSE_ALV_FIELDCATALOG_MERGE'


EXPORTING
i_program_name

= sy-repid

i_internal_tabname

= 'IT_FLIGHT'

i_inclname

= sy-repid

CHANGING
ct_fieldcat

= it_fieldcat

EXCEPTIONS
inconsistent_interface = 1
program_error

= 2.

*----get data
SELECT carrid
connid
fldate
seatsmax
seatsocc
FROM sflight
INTO CORRESPONDING FIELDS OF TABLE it_flight
UP TO 20 ROWS.
.
wa_fcat-do_sum = 'X'.
MODIFY it_fieldcat FROM wa_fcat TRANSPORTING do_sum
WHERE fieldname = 'SEATSOCC' .
wa_sort-fieldname = 'CARRID'.
wa_sort-group = 'UL'.
wa_sort-up = 'X'.

APPEND wa_sort TO it_sort.


wa_sort-fieldname = 'CONNID'.
wa_sort-subtot = 'X'.
wa_sort-up = 'X'.
APPEND wa_sort TO it_sort.
CALL FUNCTION 'REUSE_ALV_LIST_DISPLAY'
EXPORTING
i_callback_program = sy-repid
is_layout

= layout

it_fieldcat

= it_fieldcat

it_sort

= it_sort

TABLES
t_outtab

= it_flight

EXCEPTIONS
program_error

= 1.

The output would be similar to this:

Simple interactive ALV Tree calling ALV list display


*&----------------------------------------------------------------*
*& Simple Interactive alv tree, calling ALV List display
*& Author:Banupriya
*& Published at SAPTechnical.COM
*&----------------------------------------------------------------*
REPORT yh_alvtreedemo1.
TYPE-POOLS : fibs,stree.

TYPE-POOLS:slis.
DATA : t_node TYPE snodetext.
DATA : it_node LIKE TABLE OF t_node,
wa_node LIKE t_node.
DATA: t_fieldcat TYPE slis_t_fieldcat_alv,
fs_fieldcat TYPE slis_fieldcat_alv.
DATA:w_repid LIKE sy-repid.
*Internal Table declarations
DATA: BEGIN OF fs_scarr,
carrid LIKE scarr-carrid,
END OF fs_scarr.
DATA:BEGIN OF fs_spfli,
carrid LIKE spfli-carrid,
connid LIKE spfli-connid,
END OF fs_spfli.
DATA:BEGIN OF fs_sflight,
carrid LIKE sflight-carrid,
connid LIKE sflight-connid,
fldate LIKE sflight-fldate,
END OF fs_sflight.
DATA:BEGIN OF fs_sbook,
carrid LIKE sbook-carrid,
connid LIKE sbook-connid,
fldate LIKE sbook-fldate,
bookid LIKE sbook-bookid,
END OF fs_sbook.

DATA:t_scarr LIKE TABLE OF fs_scarr,


t_spfli LIKE TABLE OF fs_spfli,
t_sflight LIKE TABLE OF fs_sflight,
t_sbook LIKE TABLE OF fs_sbook.
START-OF-SELECTION.
PERFORM get_data.
PERFORM build_tree.
PERFORM display_tree.
*&----------------------------------------------------------------*
*&

Form get_data

*&----------------------------------------------------------------*
FORM get_data .
SELECT carrid
FROM scarr
INTO TABLE t_scarr.
SELECT carrid
connid
FROM spfli
INTO TABLE t_spfli
FOR ALL ENTRIES IN t_scarr
WHERE carrid EQ t_scarr-carrid.
ENDFORM.

" get_data

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

Form build_tree

*&----------------------------------------------------------------*
FORM build_tree .

CLEAR: it_node,
wa_node.
SORT: t_scarr BY carrid,
t_spfli BY carrid connid,
t_sflight BY carrid connid fldate,
t_sbook BY carrid connid fldate bookid.
wa_node-type = 'T'.
wa_node-name = 'Flight Details'.
wa_node-tlevel = '01'.
wa_node-nlength = '15'.
wa_node-color = '4'.
wa_node-text = 'Flight'.
wa_node-tlength ='20'.
wa_node-tcolor = 3.
APPEND wa_node TO it_node.
CLEAR wa_node.
LOOP AT t_scarr INTO fs_scarr.
wa_node-type = 'P'.
wa_node-name = 'CARRID'.
wa_node-tlevel = '02'.
wa_node-nlength = '8'.
wa_node-color = '1'.
wa_node-text = fs_scarr-carrid.
wa_node-tlength ='20'.
wa_node-tcolor = 4.
APPEND wa_node TO it_node.

CLEAR wa_node.
LOOP AT t_spfli INTO fs_spfli WHERE carrid EQ fs_scarr-carrid.
wa_node-type = 'P'.
wa_node-name = 'CONNID'.
wa_node-tlevel = '03'.
wa_node-nlength = '8'.
wa_node-color = '1'.
wa_node-text = fs_spfli-connid.
wa_node-tlength ='20'.
wa_node-tcolor = 4.
APPEND wa_node TO it_node.
CLEAR wa_node.
ENDLOOP.
ENDLOOP.
ENDFORM.

" build_tree

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

Form display_tree

*&----------------------------------------------------------------*
FORM display_tree .
CALL FUNCTION 'RS_TREE_CONSTRUCT'
TABLES
nodetab = it_node.
w_repid = sy-repid.
CALL FUNCTION 'RS_TREE_LIST_DISPLAY'
EXPORTING
callback_program

= w_repid

callback_user_command = 'USER_COMMAND'
callback_gui_status = 'SET_PF'.
ENDFORM.

" display_tree

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

Form pick

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

-->COMMAND text

-->NODE

text

*-----------------------------------------------------------------*
FORM user_command TABLES pt_nodes

STRUCTURE seucomm

USING pv_command
CHANGING pv_exit

TYPE c
TYPE c

pv_list_refresh TYPE c.
pv_list_refresh = 'X'.
IF pt_nodes-tlevel = '03'.
CLEAR t_fieldcat[].
SELECT carrid
connid
fldate
FROM sflight
INTO TABLE t_sflight
WHERE connid EQ pt_nodes-text.
fs_fieldcat-col_pos = 1.
fs_fieldcat-fieldname = 'CARRID'.
fs_fieldcat-seltext_m = 'Airlinecarrier'.
fs_fieldcat-key = 'X'.

fs_fieldcat-hotspot = 'X'.
APPEND fs_fieldcat TO t_fieldcat.
CLEAR fs_fieldcat.
fs_fieldcat-col_pos = 2.
fs_fieldcat-fieldname = 'CONNID'.
fs_fieldcat-seltext_m = 'Connection No'.
fs_fieldcat-key = 'X'.
fs_fieldcat-hotspot = 'X'.
APPEND fs_fieldcat TO t_fieldcat.
CLEAR fs_fieldcat.
fs_fieldcat-col_pos = 3.
fs_fieldcat-fieldname = 'FLDATE'.
fs_fieldcat-seltext_m = 'Flight Date'.
fs_fieldcat-key = 'X'.
fs_fieldcat-hotspot = 'X'.
APPEND fs_fieldcat TO t_fieldcat.
CLEAR fs_fieldcat.
w_repid = sy-repid.
CALL FUNCTION 'REUSE_ALV_LIST_DISPLAY'
EXPORTING
i_callback_program = w_repid
it_fieldcat

= t_fieldcat[]

TABLES
t_outtab

= t_sflight.

ENDIF.
ENDFORM.

"pick

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

Form set_pf

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

text

*-----------------------------------------------------------------*
FORM set_pf.
SET PF-STATUS 'MYPF'.
ENDFORM.

Primary Screen:

"set_pf

OUTPUT:
Secondary screen:

Display text 'Total' using OO-ALV


We had one requirement where we need to display Total text in ALV grid using oops concept. To
achieve it we did following things:
1. Added one extra field in the ALV output table and populated it with a constant value (This
is nothing but the text that need to be displayed as Total text) for all the records.
2. Hide this field in field catalog level
3. Populated the sort table for ALV with this field and enable subtotal display for it.
4. Hide total line display in ALV by passing appropriate value in the Layout structure.
As this extra field contains constant value for all the records so subtotal on it is equivalent to total
for rest of the fields.
Ex:
Output table
Extra Field
Total:
Total:
Total:
Total:

Field1
A
A
B
C

Field2
B
A
C
D

Field3
10
10
10
10

Field4
10
10
10
10

Final Output in ALV after subtotal on Extra Field


Field1
A
A
B
C
Total:

Field2
B
A
C
D

Field3
10
10
10
10
40

Code:
*&----------------------------------------------------------------*
*& Report Z_ALV_DEMO_TOTAL_TEXT
*&
*&----------------------------------------------------------------*
*&
*&
*&----------------------------------------------------------------*
REPORT z_alv_demo_total_text.
* Type declaration for final table to display the output
TYPES: BEGIN OF ty_mara,
srno TYPE char40, " Storing the total text
matnr TYPE matnr, " Material
ersda TYPE ersda, " Creation date
ernam TYPE ernam, " Created by
laeda TYPE laeda, " Last change date
aenam TYPE aenam, " Last change by
vpsta TYPE vpsta, " Maintenance status
brgew TYPE brgew, " Gross weight
ntgew TYPE ntgew, " Net weight
gewei TYPE gewei, " Weight Unit

Field4
10
10
10
10
40

END OF ty_mara.
* Type declaration for table storing temp. data
TYPES: BEGIN OF ty_mara_tmp,
matnr TYPE matnr, " Material
ersda TYPE ersda, " Creation date
ernam TYPE ernam, " Created by
laeda TYPE laeda, " Last change date
aenam TYPE aenam, " Last change by
vpsta TYPE vpsta, " Maintenance status
brgew TYPE brgew, " Gross weight
ntgew TYPE ntgew, " Net weight
gewei TYPE gewei, " Weight Unit
END OF ty_mara_tmp.
* Internal table for storing final data
DATA: i_mara TYPE STANDARD TABLE OF ty_mara INITIAL SIZE 0.
* Work area for final table
DATA: w_mara TYPE ty_mara.
* Internal table for storing temp. data
DATA: i_mara_tmp TYPE STANDARD TABLE OF ty_mara_tmp INITIAL SIZE 0.
* Work area for temp. table
DATA: w_mara_tmp TYPE ty_mara_tmp.
* Object variable for ALV grid
DATA: oref1 TYPE REF TO cl_gui_alv_grid.
* Field catalog table for ALV grid
DATA: fieldcat TYPE lvc_t_fcat.
* Workarea for field catalog table

DATA: w_field TYPE lvc_s_fcat.


* Internal table for storing info. for ALV grid
data: i_sort2 TYPE STANDARD TABLE OF lvc_s_sort INITIAL SIZE 0.
* Workarea for sort table
DATA: wa_sort2

TYPE lvc_s_sort.

* Workarea for ALV layout


data: wa_layout

TYPE lvc_s_layo.

START-OF-SELECTION.
* Fetch data
SELECT matnr " Material
ersda " Creation date
ernam " Created by
laeda " Last change date
aenam " Last change by
vpsta " Maintenance status
brgew " Gross weight
ntgew " Net weight
gewei " Weight Unit
FROM mara
INTO TABLE i_mara_tmp
UP TO 100 ROWS.
CHECK sy-subrc = 0.
* Populate final table
LOOP AT i_mara_tmp INTO w_mara_tmp.
* Storing the Total text need to be displayed in
* ALV

w_mara-srno = 'Total weight (Gross & Net)'.


w_mara-matnr = w_mara_tmp-matnr.
w_mara-ersda = w_mara_tmp-ersda.
w_mara-ernam = w_mara_tmp-ernam .
w_mara-laeda = w_mara_tmp-laeda.
w_mara-aenam = w_mara_tmp-aenam.
w_mara-vpsta = w_mara_tmp-vpsta.
w_mara-brgew = w_mara_tmp-brgew.
w_mara-ntgew = w_mara_tmp-ntgew.
w_mara-gewei = w_mara_tmp-gewei.
APPEND w_mara TO i_mara.
ENDLOOP.
* Calling the screen to display ALV
CALL SCREEN 100.
*&----------------------------------------------------------------*
*&

Module STATUS_0100 OUTPUT

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

Display ALV report

*-----------------------------------------------------------------*
MODULE status_0100 OUTPUT.
IF oref1 IS INITIAL.
* Create ALV grid object
* In this case we have not created any custom container in the screen,
* Instead of that dummy container name is passed
* ADVANTAGE: we can run this report in background without any problem
CREATE OBJECT oref1

EXPORTING
i_parent

= cl_gui_custom_container=>screen0

EXCEPTIONS
error_cntl_create = 1
error_cntl_init = 2
error_cntl_link = 3
error_dp_create = 4
OTHERS

=5

.
CHECK sy-subrc = 0.
* Preparing the field catalog
* ZDEMO: Defined in DDIC, it's structure is same as TYPE ty_mara
* defined in the program
CALL FUNCTION 'LVC_FIELDCATALOG_MERGE'
EXPORTING
i_structure_name

= 'ZDEMO'

CHANGING
ct_fieldcat

= fieldcat

EXCEPTIONS
inconsistent_interface = 1
program_error
OTHERS

=2
= 3.

IF sy-subrc = 0.
LOOP AT fieldcat INTO w_field.
IF w_field-fieldname = 'BRGEW' OR
w_field-fieldname = 'NTGEW'.

Summation for Gross & Net weight


w_field-do_sum = 'X'.
MODIFY fieldcat FROM w_field TRANSPORTING do_sum.
ENDIF.
IF w_field-fieldname = 'SRNO'.

Hide this field so that it can display it's content i.e.

Total text in Subtotal level


w_field-tech = 'X'.
w_field-no_out = 'X'.
MODIFY fieldcat FROM w_field TRANSPORTING tech no_out.
ENDIF.
CLEAR w_field.
ENDLOOP.
ENDIF.

* Populate Sort table with SRNO field so that we can display the total
* text in it's subtotal level
wa_sort2-spos = 1.
wa_sort2-fieldname = 'SRNO'.
wa_sort2-up = 'X'.
wa_sort2-subtot = 'X'.
APPEND wa_sort2 TO i_sort2.
* Hide the total line
wa_layout-no_totline = 'X'.
* Display the ALV grid
CALL METHOD oref1->set_table_for_first_display
EXPORTING

is_layout

= wa_layout

CHANGING
it_outtab

= i_mara[]

it_fieldcatalog

= fieldcat

it_sort

= i_sort2

EXCEPTIONS
invalid_parameter_combination = 1
program_error

=2

too_many_lines

=3

OTHERS

= 4.

IF sy-subrc <> 0.
ENDIF.
* Set the focus on the grid
CALL METHOD cl_gui_alv_grid=>set_focus
EXPORTING
control

= oref1

EXCEPTIONS
cntl_error

=1

cntl_system_error = 2
OTHERS

= 3.

IF sy-subrc <> 0.
ENDIF.
ENDIF.
ENDMODULE.

Output:

" ST

ALV with EDIT and SAVE functionality


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: <dyn_table> TYPE STANDARD TABLE,
<dyn_wa> TYPE ANY,
<dyn_field> TYPE ANY,
<dyn_tab_temp> 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 <dyn_table>.
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 <dyn_table>.
ASSIGN dy_line->* TO <dyn_wa>.
* Create another temp. table
CREATE DATA dy_tab TYPE STANDARD TABLE OF (p_table).
ASSIGN dy_tab->* TO <dyn_tab_temp>.
SORT i_fieldcat BY col_pos.
* Select data from table
SELECT * FROM (p_table)
INTO TABLE <dyn_table>
UP TO lines ROWS.
REFRESH <dyn_tab_temp>.
* Display report
CALL FUNCTION 'REUSE_ALV_LIST_DISPLAY'
EXPORTING
i_callback_program
i_structure_name

= sy-repid
= p_table

i_callback_user_command = 'USER_COMMAND'
i_callback_pf_status_set = 'SET_PF_STATUS'
TABLES
t_outtab

= <dyn_table>

EXCEPTIONS
program_error
OTHERS
IF sy-subrc <> 0.

=1
= 2.

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:<l_tab> TYPE table,

<l_wa> TYPE ANY.


* Create table
CREATE DATA li_tab TYPE STANDARD TABLE OF (p_table).
ASSIGN li_tab->* TO <l_tab>.
* Create workarea
CREATE DATA l_line LIKE LINE OF <l_tab>.
ASSIGN l_line->* TO <l_wa>.
CASE r_ucomm.
* When a record is selected
WHEN '&IC1'.
*

Read the selected record


READ TABLE <dyn_table> ASSIGNING <dyn_wa> INDEX
rs_selfield-tabindex.
IF sy-subrc = 0.

Store the record in an internal table


APPEND <dyn_wa> TO <l_tab>.

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
it_fieldcat

= p_table

= 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

= <l_tab>

EXCEPTIONS
program_error
OTHERS

=1
= 2.

IF sy-subrc = 0.
*

Read the modified data


READ TABLE <l_tab> INDEX 1 INTO <l_wa>.

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 <dyn_wa> <> <l_wa>.
<dyn_wa> = <l_wa>.
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 <dyn_table> ASSIGNING <dyn_wa> INDEX i_index.
IF sy-subrc = 0.
APPEND <dyn_wa> TO <dyn_tab_temp>.
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 <dyn_tab_temp>.
REFRESH <dyn_tab_temp>.

Unlock the table


CALL FUNCTION 'DEQUEUE_E_TABLE'
EXPORTING
mode_rstable = 'E'
tabname

= p_table.

ENDIF.
ENDCASE.
rs_selfield-refresh = 'X'.
ENDFORM.
Selection screen:

Selection screen:

"user_command

Output:

Anda mungkin juga menyukai