ABAP Reports

ABAP(Advance Business Application Programming) is a language that is basically used in SAP developmnet. ABAP is used for both data entry progamming and Report generation. There are a number of reports type which is developed by using ABAP. List and important aspect of those reports is given below:-
1. Classical Reports
2. ALV(Active List Viewer) Reports
3. Smart Forms
4. Hierarchical Reports

1. Classical Report:
Classical reports are basically used for developing a such kind of reports which is used on daily Basis. Some important factors of classical reports is as follows:-

a. AT Line Selection:

DATA : BEGIN OF itab OCCURS 0.
INCLUDE STRUCTURE zqmrr.
DATA: END OF itab.
START-OF-SELECTION.
SELECT * INTO TABLE itab FROM zqmrr.
LOOP AT itab.
WRITE :/10 itab-werks,
15 itab-mkmnr,
25 itab-charg.
ENDLOOP.
AT LINE-SELECTION.
WRITE:/ 'Rajasthan Textile Mills Bhawanimandi'.
TOP-OF-PAGE DURING LINE-SELECTION.
WRITE:/'hi'.

b. AT User Command:
At use command also can be acomplish by two way. First, Perform any action by Clicking any button(PF Status), Second Leaving Screen.
I
START-OF-SELECTION.
SET PF-STATUS 'MYLIST'. // MYLIST is the GUI Status(PF Status)
WRITE 'List line'.
AT USER-COMMAND.
CASE sy-ucomm.
WHEN 'SAV'.
WRITE: / 'You worked on list', sy-listi,
/ 'You are on list', sy-lsind.
ENDCASE.
II
form user_command using r_ucomm like sy-ucomm rs_selfield type slis_selfield.
read table i_vbap index rs_selfield-tabindex.
if sy-subrc = 0.
set parameter id 'AUN' field i_vbap-vbeln.
call transaction 'VA03'." and skip first screen.
endif.
endform.
Note:in second method we have to add this user command in REUSE_ALV_GRID_DISPLAY as follows -
i_callback_user_command = 'USER_COMMAND'

c. F4 Key Press:
This is used for display help at selection screen i.e.when we pressed F4 key to display value at selection screen.

at selection-screen on value-request for s_docno-low.
select trntyp
docno
from j_1iexchdr
into table i_are
where trntyp eq 'ARE1'.
call function 'F4IF_INT_TABLE_VALUE_REQUEST'
exporting
retfield = 'DOCNO'
dynpprog = v_repid
dynpnr = v_dynnr
dynprofield = 'S_DOCNO-LOW'
window_title = 'F4 Help For the ARE1 Document'
value_org = 'S'
tables
value_tab = i_are.
if sy-subrc <> 0.
endif.

d. Convert Output to PDF:
This can be done in two different ways-
I-Saving Output without dialog box:
data : gs_print_parms type pri_params,
l_valid type string,
gs_tsp01 type tsp01,
gv_rq2name type tsp01-rq2name.
concatenate sy-repid+0(9) sy-uname+0(3) into gv_rq2name.
call function 'GET_PRINT_PARAMETERS'
exporting
destination = 'LP02'
layout = 'X_65_132'
line_size = '132'
no_dialog = 'X'
importing
out_parameters = gs_print_parms
valid = l_valid
exceptions
archive_info_not_found = 1
invalid_print_params = 2
invalid_archive_params = 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.
gs_print_parms-primm = ' '.
gs_print_parms-prcop = '1'.
submit y_chk_pmt_advs with pzbukr = pzbukr
with pgjahr = pgjahr
with phbkid = phbkid
with phktid = phktid
with pchecf in pchecf
with pvblnr in pvblnr
with ppridt in ppridt
with pzaldt in pzaldt
with opt2 = opt2
with opt1 = space
with opt3 = space
with opt4 = space
with opt5 = space
to sap-spool without spool dynpro
spool parameters gs_print_parms
and return.
select single *
from tsp01
into gs_tsp01
where rq2name eq gv_rq2name
and rqowner eq sy-uname.
"ORDER BY rqcretime ASCENDING.
if sy-subrc eq 0.
submit rstxpdft4 with spoolno eq gs_tsp01-rqident
with p_file eq 'CHKLST.PDF'
and return.
endif.
delete from tsp01
where rqident = gs_tsp01-rqident and
rq2name eq gv_rq2name.
exit.
II-Display Output on screen:
data : gs_print_parms type pri_params,
l_valid type string,
gs_tsp01 type tsp01,
gv_rq2name type tsp01-rq2name.
CONCATENATE sy-repid+0(9) sy-uname+0(3) INTO gv_rq2name.
CALL FUNCTION 'GET_PRINT_PARAMETERS'
EXPORTING
destination = 'LP02'
layout = 'X_65_80'
line_size = '80'
no_dialog = 'X'
IMPORTING
out_parameters = gs_print_parms
valid = l_valid
EXCEPTIONS
archive_info_not_found = 1
invalid_print_params = 2
invalid_archive_params = 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.
gs_print_parms-primm = ' '.
gs_print_parms-prcop = '1'.

SUBMIT y_qm_lot_test WITH vwerk IN vwerk
WITH vcharg IN vcharg
WITH verste IN verste
TO SAP-SPOOL WITHOUT SPOOL DYNPRO
SPOOL PARAMETERS gs_print_parms
AND RETURN.
SELECT SINGLE *
FROM tsp01
INTO gs_tsp01
WHERE rq2name EQ gv_rq2name
AND rqowner EQ sy-uname.
IF sy-subrc EQ 0.
CALL FUNCTION 'Z_FM_PDF_DATADISPLAY'
EXPORTING
spool = gs_tsp01-rqident
delete = 'X'.
DELETE tsp01 FROM gs_tsp01.
COMMIT WORK AND WAIT.
ENDIF.

DELETE FROM tsp01
WHERE rqident EQ gs_tsp01-rqident AND
rq2name EQ gv_rq2name.
EXIT.
e. Color Printing:
For printing color use following syntax:
FORMAT COLOR 3

2. ALV Report:
ALV report is basically used for display data in grid form. This is more flexible report type in which user can re-arrange, sort or hide the coloumn. Main function used in grid is as follows-
a. Declaring Data:
data: rptname like sy-repid,
w_shift type i.
type-pools: slis.
data: fcat type table of slis_fieldcat_alv with non-unique default key
with header line initial size 0,
layout type slis_layout_alv,
fevents type table of slis_alv_event with non-unique default key
with header line initial size 0,
fheader type table of slis_listheader with non-unique default key
with header line initial size 0,
fexclude type table of slis_extab with non-unique default key
with header line initial size 0,
gt_sort type slis_t_sortinfo_alv.
data : alv_variant like disvariant.
b. Progress Indicator:
call function 'SAPGUI_PROGRESS_INDICATOR'
exporting
text = 'Hi this is durgesh'.
c.Display Variant:
data : alv_variant like disvariant.
parameter : variant type disvariant-variant.
at selection-screen on value-request for variant.
alv_variant-report = sy-repid.
call function 'REUSE_ALV_VARIANT_F4'
exporting
is_variant = alv_variant
i_save = 'A'
importing
es_variant = alv_variant
exceptions
not_found = 2.
if sy-subrc = 2.
message id sy-msgid type 'S' number sy-msgno
with sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
else.
variant = alv_variant-variant.
endif.
Note:
1.Below statement will write just above Display Function-
alv_variant-report = rptname.
alv_variant-variant = variant.

2.Add Following line in display function-
is_variant = alv_variant
3.i_save = 'A' for user specific
i_save = 'X' for Not user specific i.e. default
i_save = 'U' for Not user specific i.e. default

d. Page Heading:
form topofpage.
call function 'REUSE_ALV_COMMENTARY_WRITE'
exporting
it_list_commentary = fheader[].
* I_LOGO =
* I_END_OF_LIST_GRID =
endform. "TOPOFPAGE
fheader-typ = 'H'.
fheader-info = 'RAJASTHAN TEXTILE MILLS, BHAWANIMANDI'.
append fheader.
clear fheader.
* To show in Italic Style
fheader-typ = 'A' .
fheader-info = 'Pachpahar Road'.
append fheader.
clear fheader.
* To Show in small character
* Info is Char 60 Colunm
* typ is Char 1 Column
fheader-typ = 'S'.
fheader-info = ' List of Sale Orders for Testing in ALV Report'.
append fheader.
clear fheader.
fheader-typ = 'S'.
data : trptdt(10) type c.
data : xdt type string.
write : sy-datum to trptdt.
concatenate 'Report run date : ' trptdt into xdt separated by space.
fheader-typ = 'S'.
fheader-info = xdt.
append fheader.
clear fheader.
call function 'REUSE_ALV_EVENTS_GET'
exporting
i_list_type = 0
importing
et_events = fevents[]
exceptions
list_type_wrong = 1
others = 2.
read table fevents with key name = 'TOP_OF_PAGE'.
if sy-subrc = 0.
fevents-form = 'TOPOFPAGE'.
modify fevents index sy-tabix.
clear fevents.
endif.
e. Display Layout:
Data :layout type slis_layout_alv.
layout-zebra = 'X'.
layout-no_vline = 'X'."Vertical Lines between columns will not shown
layout-no_hline = 'X'."Horizontal Lines between columns will not shown
layout-edit = 'X'. "Columns can be edit in Grid at run time
layout-no_colhead = 'X'. "Columns Heading will not be shown
In Display function add following lines-
is_layout = layout
f. Field Catalog:
fcat-fieldname = 'VBELN'."Field name of Final Table
fcat-outputlen = 11. " Size of Field in Grid
fcat-seltext_s = 'Sales '." For Name of Colunm in Grid
fcat-seltext_m = 'Sales Order No'.
fcat-seltext_l = 'Sales Order No'. " For Tool Tip
fcat-no_zero = 'X'."All Zero in start of Sales Order Number wll be suppressed
fcat-checkbox = 'X'. "Field will be shown as Checkboxx
fcat-no_out = 'X'. "This field will not be show but
"can be select from select layout
fcat-input = 'X'. "Not Clear
fcat-icon = 'X'. "Not Clear
fcat-ddictxt = 'M'. "M= fcat-seltext_m, S=fcat-seltext_s,
"L=fcat-seltext_l
fcat-inttype = 'C'. "Alignment N for Right C For Left
fcat-key = 'X'. "Fix the Column.(Col.will not drag
fcat-datatype = 'CHAR'. "Type of field in table
fcat-emphasize = 'C200'. "For Show in Color
fcat-do_sum = 'X'. "will display sum of this column
g. Display Function:
call function 'REUSE_ALV_GRID_DISPLAY'
exporting
i_callback_program = rptname "program name
i_callback_user_command = 'USER_COMMAND'
i_callback_pf_status_set = 'X'"'SM04_SET_STATUS'
"disable menubar
is_layout = layout"for promt message to leave scrren
it_fieldcat = fcat[]"field catalog
it_excluding = fexclude[]"remove icon from toolbar
it_sort = gt_sort[]
i_default = 'X'
i_save = 'A'"user specific
is_variant = alv_variant" variant of grid
i_grid_title = 'DURGESH' "Display Title of grid
it_events = fevents[]"top of page
tables
t_outtab = i_vbap[]"final table name
exceptions
program_error = 1
others = 2.
h. Field Catalog Function:
FORM build_field_catalog .
CALL FUNCTION 'REUSE_ALV_FIELDCATALOG_MERGE'
EXPORTING
i_program_name = sy-repid
i_internal_tabname = 'I_FINAL'
i_inclname = sy-repid
CHANGING
ct_fieldcat = fcat[]
EXCEPTIONS
inconsistent_interface = 1
program_error = 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.
fheader-typ = 'H'.
fheader-info = 'RAJASTHAN TEXTILE MILLS, BHAWANIMANDI'.
APPEND fheader.
CLEAR fheader.
fheader-typ = 'A' .
fheader-info = 'Pachpahar Road'.
APPEND fheader.
CLEAR fheader.
fheader-typ = 'S'.
fheader-info = ' List of Customer for Testing of ALV Report'.
APPEND fheader.
CLEAR fheader.
fheader-typ = 'S'.
DATA : trptdt(10) TYPE c.
DATA : xdt TYPE string.
WRITE : sy-datum TO trptdt.
CONCATENATE 'Report run date : ' trptdt INTO xdt SEPARATED BY space.
fheader-typ = 'S'.
fheader-info = xdt.
APPEND fheader.
CLEAR fheader.
fexclude-fcode = '&ABC'.
append fexclude.
clear fexclude.
ENDFORM.
i. Remove icon from toolbar:
To remove icons from toolbar add following lines at end of field catalog. i.e just above ENDFORM of field catalog.
fexclude-fcode = '&ABC'.
append fexclude.
clear fexclude.
NOTE: if we have to remove icon from toolbar, then write above code and add following lines in DISPLAY FUNCTION
it_excluding = fexclude[]

3. Smart Forms:
Smart forms is basically used for much attractive report.
TABLES : bkpf, bseg.
DATA: fm_name TYPE rs38l_fnam.
DATA : BEGIN OF itab OCCURS 0,
bukrs TYPE bkpf-bukrs,"company code
gjahr TYPE bkpf-gjahr," Fisical year
budat TYPE bkpf-budat," Posting date
belnr TYPE bkpf-belnr." Document No.
DATA : END OF itab.
DATA : tbudt1 TYPE bkpf-budat.
DATA : tbudt2 TYPE bkpf-budat.
SELECTION-SCREEN BEGIN OF BLOCK blk WITH FRAME TITLE text-t01.
SELECT-OPTIONS:
pbukrs FOR bkpf-bukrs NO INTERVALS NO-EXTENSION
OBLIGATORY DEFAULT '1000',
pgjahr FOR bkpf-gjahr NO INTERVALS NO-EXTENSION
OBLIGATORY DEFAULT sy-datum,
pbudat FOR bkpf-budat DEFAULT sy-datum to sy-datum.
SELECTION-SCREEN END OF BLOCK blk.

INITIALIZATION.
IF pbudat-high IS INITIAL.
pbudat-high = pbudat-low.
ENDIF.

START-OF-SELECTION.
SELECT bukrs
gjahr
budat
belnr
INTO TABLE itab
FROM bkpf
WHERE bukrs IN pbukrs AND
gjahr IN pgjahr AND
budat IN pbudat.
IF itab[] IS INITIAL.
MESSAGE 'No Data Found...' TYPE 'S'.
ELSE.
CLEAR : itab[].
CALL FUNCTION 'SSF_FUNCTION_MODULE_NAME'
EXPORTING
formname = 'Y_SMART' "smart form name
IMPORTING
fm_name = fm_name.

CALL FUNCTION fm_name
EXPORTING
"smart form field driver program field
vbukrs = pbukrs-low
vgjahr = pgjahr-low
vbudat1 = pbudat-low
vbudat2 = pbudat-high.
ENDIF.
Note:
if we have to export table from driver program to smartforms, then do following steps-
1.Define structure for table in driver program that has to be exported in smartforms.

DATA : BEGIN OF i_final OCCURS 0.
INCLUDE STRUCTURE zqmrr.
DATA : END OF i_final.

2.We have to export i_final table, so we have define structure for it. If we will define table then it will generate error.

CALL FUNCTION 'SSF_FUNCTION_MODULE_NAME'
EXPORTING
formname = 'Y_SMT'
IMPORTING
fm_name = fm_name.
CALL FUNCTION fm_name
EXPORTING
swerks = vwerks-low == variable exporting
TABLES
ifinal = i_final[]. == Table exporting

Now go to forminterface tables tab abd define exported internal table.

PDF Conversion of Smartforms:

tables : zqmrr.
data : begin of i_tab occurs 0,
werks type zqmrr-werks,
erdat type zqmrr-erdat,
charg type zqmrr-charg,
mkmnr type zqmrr-mkmnr,
sumplus type zqmrr-sumplus,
prueflos type zqmrr-prueflos.
data : end of i_tab.
data: fm_name type rs38l_fnam.
data: wa_ctrlop type ssfctrlop,
wa_outopt type ssfcompop.
data: t_otfdata type ssfcrescl,
t_pdf_tab like tline occurs 0 with header line.
data: t_otf type itcoo occurs 0 with header line.
data: w_filesize type string.
data: w_bin_filesize type i.
selection-screen begin of block blk01 with frame title text-001.
parameters :
vwerks type zqmrr-werks obligatory,
verdat type zqmrr-erdat obligatory default sy-datum.
selection-screen end of block blk01.
selection-screen begin of block blk02 with frame title text-002.
parameters :
r1 as checkbox.
selection-screen end of block blk02.
start-of-selection.
select werks
erdat
charg
mkmnr
sumplus
prueflos
from zqmrr
into table i_tab
where werks eq vwerks and
erdat eq verdat.
if sy-subrc ne 0.
message 'No Record Found' type 'S'.
exit.
else.
if r1 ne 'X'.
call function 'SSF_FUNCTION_MODULE_NAME'
exporting
formname = 'Y_QM_DAILY_RPT1'
importing
fm_name = fm_name
exceptions
no_form = 1
no_function_module = 2
others = 3.
call function fm_name
exporting
swerks = vwerks
serdat = verdat.
else.
call function 'SSF_FUNCTION_MODULE_NAME'
exporting
formname = 'Y_QM_DAILY_RPT'
importing
fm_name = fm_name
exceptions
no_form = 1
no_function_module = 2
others = 3.
wa_ctrlop-getotf = 'X'.
wa_ctrlop-no_dialog = 'X'.
wa_outopt-tdnoprev = 'X'.
call function fm_name
exporting
swerks = vwerks
serdat = verdat
control_parameters = wa_ctrlop
output_options = wa_outopt
user_settings = 'X'
importing
job_output_info = t_otfdata
exceptions
formatting_error = 1
internal_error = 2
send_error = 3
user_canceled = 4
others = 5.

t_otf[] = t_otfdata-otfdata[].
call function 'CONVERT_OTF'
exporting
format = 'PDF'
max_linewidth = 132
importing
bin_filesize = w_bin_filesize
tables
otf = t_otf
lines = t_pdf_tab
exceptions
err_max_linewidth = 1
err_format = 2
err_conv_not_possible = 3
others = 4.
call function 'WS_DOWNLOAD'
exporting
bin_filesize = w_bin_filesize
filename = 'D:\sqc_rpt.PDF'
filetype = 'BIN'
importing
filelength = w_filesize
tables
data_tab = t_pdf_tab
exceptions
file_open_error = 1
file_write_error = 2
invalid_filesize = 3
invalid_type = 4
no_batch = 5
unknown_error = 6
invalid_table_width = 7
gui_refuse_filetransfer = 8
customer_error = 9
others = 10.
if sy-subrc <> 0.
message 'Report has not been saved succesully.' type 'S'.
else.
message 'Report has been saved in D drive succesully.' type 'S'.
endif.
endif.
endif.

4. Hierarchical Report:

Hierarchical Report is used for diaplay report in tree format.
DATA : node_tab LIKE snodetext OCCURS 0 WITH HEADER LINE.
DATA : BEGIN OF i_qals OCCURS 0,
prueflos TYPE qals-prueflos,
werk TYPE qals-werk,
enstehdat TYPE qals-enstehdat,
selmatnr TYPE qals-selmatnr,
charg TYPE qals-charg,
ktextmat TYPE qals-ktextmat.
DATA : END OF i_qals.
CLEAR : node_tab, node_tab[].
SELECT prueflos
werk
enstehdat
selmatnr
charg
ktextmat
INTO TABLE i_qals FROM qals UP TO 40 ROWS WHERE selmatnr NE '' AND charg NE ''.
node_tab-type = 'T'.
node_tab-name = 'Lot Description'.
node_tab-tlevel = '01'.
node_tab-nlength = '15'.
APPEND node_tab.
CLEAR node_tab.

LOOP AT i_qals.
node_tab-type = 'P'.
node_tab-name = i_qals-prueflos.
node_tab-tlevel = '02'.
node_tab-nlength = '15'.
APPEND node_tab.
CLEAR node_tab.

node_tab-type = 'P'.
node_tab-name = i_qals-werk.
node_tab-tlevel = '03'.
node_tab-nlength = '06'.
APPEND node_tab.
CLEAR node_tab.

node_tab-type = 'P'.
node_tab-name = i_qals-enstehdat.
node_tab-tlevel = '04'.
node_tab-nlength = '10'.
APPEND node_tab.
CLEAR node_tab.

node_tab-type = 'P'.
node_tab-name = i_qals-selmatnr.
node_tab-tlevel = '05'.
node_tab-nlength = '10'.
APPEND node_tab.
CLEAR node_tab.


node_tab-type = 'P'.
node_tab-name = i_qals-charg.
node_tab-tlevel = '06'.
node_tab-nlength = '08'.
APPEND node_tab.
CLEAR node_tab.

node_tab-type = 'P'.
node_tab-name = i_qals-ktextmat.
node_tab-tlevel = '07'.
node_tab-nlength = '30'.
APPEND node_tab.
CLEAR node_tab.
ENDLOOP.


*node_tab-type = 'T'. "Node type
*node_tab-name = 'Earth'. "Object name
*node_tab-tlevel = '01'. "Node level
*node_tab-nlength = '5'. "Output length of a hierarchy node
*node_tab-color = '4'. "Color display of a hierarchy node
*node_tab-text = 'Hello'. "Text/Icon/Symbol
*node_tab-tlength ='5'. "Output length of a hierarchy node
*node_tab-tcolor = 3. "Color display of a hierarchy node
*APPEND node_tab.
*CLEAR node_tab.
*
*node_tab-type = 'P'.
*node_tab-name = 'Europe'.
*node_tab-tlevel = '02'.
*node_tab-nlength = '6'.
*node_tab-color = '1'.
*node_tab-text = 'Hello'.
*node_tab-tlength ='5'.
*node_tab-tcolor = 4.
*APPEND node_tab.
*CLEAR node_tab.

*node_tab-type = 'P'.
*node_tab-name = 'Germany'.
*node_tab-tlevel = '03'.
*node_tab-nlength = '7'.
*node_tab-color = '4'.
*node_tab-text = 'Hello'.
*node_tab-tlength ='5'.
*node_tab-tcolor = 4.
*APPEND node_tab.
*CLEAR node_tab.
*
*node_tab-type = 'P'.
*node_tab-name = 'Berlin'.
*node_tab-tlevel = '04'.
*node_tab-nlength = '6'.
*node_tab-color = '4'.
*node_tab-text = 'Hello'.
*node_tab-tlength ='5'.
*node_tab-tcolor = 3.
*APPEND node_tab.
*CLEAR node_tab.
*
*node_tab-type = 'P'.
*node_tab-name = 'Asia'.
*node_tab-tlevel = '02'.
*node_tab-nlength = '4'.
*node_tab-color = '1'.
*node_tab-text = 'Hello'.
*node_tab-tlength ='5'.
*node_tab-tcolor = 3.
*APPEND node_tab.
*CLEAR node_tab.
*
*node_tab-type = 'P'.
*node_tab-name = 'India'.
*node_tab-tlevel = '03'.
*node_tab-nlength = '5'.
*node_tab-color = '1'.
*node_tab-text = 'Hello'.
*node_tab-tlength ='5'.
*node_tab-tcolor = 3.
*APPEND node_tab.
*CLEAR node_tab.
*
*node_tab-type = 'P'.
*node_tab-name = 'Bombay'.
*node_tab-tlevel = '04'.
*node_tab-nlength = '6'.
*node_tab-color = '1'.
*node_tab-text = 'Hello'.
*node_tab-tlength ='5'.
*node_tab-tcolor = 3.
*APPEND node_tab.
*CLEAR node_tab.


CALL FUNCTION 'RS_TREE_CONSTRUCT'
TABLES
nodetab = node_tab.

CALL FUNCTION 'RS_TREE_LIST_DISPLAY'
EXPORTING
use_control = 'L'.