In a previous article on SAP ABAP Internal tables, we studied the importance of Internal tables in ABAP.
In this fourth blog of a 5 part series on SAP ABAP, I am trying to convey about SAP Report Programming.
From this blog, you'll learn:
In this fourth blog of a 5 part series on SAP ABAP, I am trying to convey about SAP Report Programming.
From this blog, you'll learn:
A report in SAP ABAP is presenting the data in an organized manner. Reports are used when we need to display the bulk of data in a systematic way.
Below are a few points which you should learn before starting with reports in SAP ABAP.
- Reports are executable program i.e Type 1 or Type E
- The first line of every report should start with the keyword as REPORT followed by the report name (REPORT zrds). Here 'zrds' is the report name.
- The naming convention of all the customized reports should start with 'Z' or 'Y'
- If we want to hide the heading, use REPORT zrds No Standard Page Heading
- The line-count for the particular page can be set by using REPORT zrds line-count n(n1). Here n is the number of lines to be displayed for the particular page and n1 is the number of lines reserved for the page footer.
Apart from the above key features, there is one important concept of Reports. ABAP is event-oriented. It does not occur line by line, but by the events. Below is the complete explanation for the same.
Events in ABAP Report
SAP offers some events which take place in a particular order.
Let's look at each event in some more depth.
- Top of page: This is triggered by the first 'write' statement in the report. Acts as the header of the page. If there is no 'write' statement in the report, by default, the first event to be triggered is the 'load-of-program'.
- Load-of-program: This event gets triggered when the program of type E, M, F or S gets loaded. It exists only once per program.
- Initialization: As the name suggests, this event is used to initialize the values. This event gets triggered after the 'load-of -program' and before any selection screen gets displayed.
INITIALIZATION.
p_carrid = 'AC'.
p_carrid = 'AC'.
- At selection-screen output: This event gets triggered just before the selection screen gets displayed. This event is used when you want to change the screen fields at runtime. This will hide the fields, make them visible, invisible, or make them intensified.
if p_check = 'X'.
Loop at screen.
if screen-name = 'p_carrid'.
screen-input = '0'.
endif.
modify screen.
endloop.
Loop at screen.
if screen-name = 'p_carrid'.
screen-input = '0'.
endif.
modify screen.
endloop.
NOTE: Here modify screen plays a very important role. With the help of 'modify screen', the screen gets changed accordingly.
- At selection-screen: This event gets triggered when the user processes the selection-screen. The role of this event is to check and validate the inputted values.
if r_button = 'X'.
MESSAGE 'HI' type 'I'.
endif.
MESSAGE 'HI' type 'I'.
endif.
NOTE: To know more about message types refer 'Message types in SAP ABAP'.
- Start-of-selection: This event gets triggered when the user clicks on the execute button or hits F8. It is very good practice to mention this event in the code.
if r_button = 'X'.
Perform fetch_data.
endif.
Perform fetch_data.
endif.
NOTE: Perform <subroutine-name> is the subroutine that is local to the program. The block of code is written inside FORM <subroutine-name>...........ENDFORM.
- End-of-selection: This event gets triggered when the last statement of the start-of-selection event gets executed.
Below is the code snippet of events in SAP ABAP.
LOGIC:
*&---------------------------------------------------------------------*
*& Report ZRDS_EVENTS_FLOW
*&---------------------------------------------------------------------*
*&
*&---------------------------------------------------------------------*
REPORT zrds_events_flow LINE-COUNT 10(3).
LOAD-OF-PROGRAM.
WRITE : / 'This is load of program'.
INITIALIZATION.
WRITE: / 'This is initialization'.
START-OF-SELECTION.
WRITE: / 'This is start of selection'.
END-OF-SELECTION.
WRITE: / 'This is end of selection'.
TOP-OF-PAGE.
WRITE: / 'This is top of page'.
END-OF-PAGE.
WRITE: / 'This is end of page'.
*& Report ZRDS_EVENTS_FLOW
*&---------------------------------------------------------------------*
*&
*&---------------------------------------------------------------------*
REPORT zrds_events_flow LINE-COUNT 10(3).
LOAD-OF-PROGRAM.
WRITE : / 'This is load of program'.
INITIALIZATION.
WRITE: / 'This is initialization'.
START-OF-SELECTION.
WRITE: / 'This is start of selection'.
END-OF-SELECTION.
WRITE: / 'This is end of selection'.
TOP-OF-PAGE.
WRITE: / 'This is top of page'.
END-OF-PAGE.
WRITE: / 'This is end of page'.
OUTPUT:
Message types in SAP ABAP
Message types are very important in any programming language to give the status of the code. In ABAP, there are 6 message types provided by SAP.
Type I
Type 'I' stands for Infomation. As the name suggests, this type provides information to the user. When the user clicks on the popup message of Information, the further program is continued.
TYPE E
Type 'E' stands for Error. This type is used to prevent the user from entering the wrong details or to fetch incorrect records from the database. When the error message pops up, the user is no longer able to proceed with the further output until he rectifies his mistakes.
TYPE W
Type 'W' stands for Warning. This type of message provides just a warning to the user of specific problems. The user is still able to proceed with the further output when he clicks on the warning pop-up message.
TYPE A
Type 'A' stands for Abort. The pop-up message appears on the screen indicating Abort and the user is no longer able to make any changes. Here system will terminate the program and go back to the main screen. This type of message should be used only in extreme conditions.
TYPE X
Type 'X' stands for Exit. The system terminates the program and provides a short dump.
TYPE S
Type 'S' stands for Success. This type of message displays the status of the program.
The following are the two ways of writing the message.
MESSAGE type message-id (message class) with 'message text'.
For example:
MESSAGE I000(ZRDS) with 'ZRDS is my message class'.
NOTE: We can create our own message class in transaction SE91.
OR.
MESSAGE 'Message text' type 'MESSAGE-TYPE'.
NOTE: Message type should always be in caps.
Further, SAP ABAP provides segregation in reports. Below is a detailed explanation for the same.
Types of Reports
Report programming is divided into two types:
- Classical Report programming: Classical report programming is just meant to display the data of the tables. It's a single list. Classical Report programming does not include any sub-reports.
- Interactive Report programming: Interactive Report includes sub-reports. Interactive Reports are the ones in which users can interact. When we click on the data of the first report based on the user command, the second report gets generated.
Before jumping to the code, let's look at the case study.
This is an example of the Classical Report Program.
We have to make the selection-screen that will provide us two options:
To download the flight details from the 'SFLIGHT' database table to our local presentation server.
To upload the flight details from the presentation server(local machine) to the Application layer (SAP server).
Now, let us look at the below screenshots to implement classical report programming.
OUTPUT:
LOGIC:
*&---------------------------------------------------------------------*
*& Report ZRDS_CLASSICAL_REPORT
*&---------------------------------------------------------------------*
*&
*&---------------------------------------------------------------------*
REPORT zrds_classical_report.
TYPES : BEGIN OF ty_sflight,
carrid TYPE s_carr_id,
connid TYPE s_conn_id,
fldate TYPE s_date,
price TYPE s_price,
planetype TYPE s_planetye,
seatsmax TYPE s_seatsmax,
seatsocc TYPE s_seatsocc,
END OF ty_sflight.
DATA: it_sflight TYPE TABLE OF ty_sflight,
it_sflight_upload type TABLE of sflight,
wa_sflight TYPE ty_sflight.
SELECTION-SCREEN BEGIN OF BLOCK b1 WITH FRAME TITLE TEXT-001.
PARAMETERS : p_dload AS CHECKBOX USER-COMMAND flag, "to select download option
p_uload AS CHECKBOX USER-COMMAND flag . "to select upload option
SELECTION-SCREEN END OF BLOCK b1.
SKIP.
SELECTION-SCREEN BEGIN OF BLOCK b2 WITH FRAME TITLE TEXT-002.
PARAMETERS : p_carrid TYPE s_carr_id MODIF ID m1,
p_file TYPE rlgrap-filename MODIF ID m1.
SELECTION-SCREEN END OF BLOCK b2.
SKIP.
SELECTION-SCREEN BEGIN OF BLOCK b3 WITH FRAME TITLE TEXT-003.
PARAMETERS : p_ufile TYPE rlgrap-filename MODIF ID m2.
SELECTION-SCREEN END OF BLOCK b3.
AT SELECTION-SCREEN OUTPUT.
LOOP AT SCREEN.
IF p_dload NE 'X' AND "if download option is not selected, hide the download screen
screen-group1 = 'M1'.
screen-active = '0'.
ENDIF.
MODIFY SCREEN.
ENDLOOP.
LOOP AT SCREEN.
IF p_uload NE 'X' AND "if upload option is not selected, hide the upload screen
screen-group1 = 'M2'.
screen-active = '0'.
ENDIF.
MODIFY SCREEN.
ENDLOOP.
AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_file.
PERFORM file_value_help.
AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_ufile.
PERFORM file_value_upload_help.
AT SELECTION-SCREEN.
PERFORM validation. "validation on screen
START-OF-SELECTION.
PERFORM get_flight_details.
END-OF-SELECTION.
PERFORM display_details.
IF p_dload = 'X'.
PERFORM download_flight_data.
ENDIF.
IF p_uload = 'X'.
PERFORM upload_flight_data.
ENDIF.
FORM validation.
IF p_dload = 'X' AND p_uload = 'X'.
MESSAGE 'Please select only one option' TYPE 'E'.
ENDIF.
ENDFORM.
FORM get_flight_details.
SELECT carrid
connid
fldate
price
planetype
seatsmax
seatsocc
FROM sflight
INTO TABLE it_sflight
WHERE carrid = p_carrid.
ENDFORM.
FORM display_details.
IF it_sflight IS NOT INITIAL.
LOOP AT it_sflight INTO wa_sflight.
WRITE: / wa_sflight-carrid, wa_sflight-connid, wa_sflight-fldate, wa_sflight-price, wa_sflight-planetype, wa_sflight-seatsmax, wa_sflight-seatsocc.
ENDLOOP.
ENDIF.
ENDFORM.
FORM download_flight_data.
DATA : lv_file TYPE string .
lv_file = p_file .
CALL FUNCTION 'GUI_DOWNLOAD'
EXPORTING
filename = lv_file
* FILETYPE = 'ASC'
* APPEND = ' '
write_field_separator = 'x'
TABLES
data_tab = it_sflight
.
IF sy-subrc = 0.
MESSAGE 'Download is successful' TYPE 'I'.
ENDIF.
ENDFORM.
FORM upload_flight_data.
data : p_local type string.
p_local = p_ufile.
CALL FUNCTION 'GUI_UPLOAD'
EXPORTING
filename = p_local
FILETYPE = 'ASC'
HAS_FIELD_SEPARATOR = 'x'
TABLES
data_tab = it_sflight_upload
EXCEPTIONS
BAD_DATA_FORMAT = 8
.
IF sy-subrc = 0.
MESSAGE 'Uploaded successfully' type 'I'.
ENDIF.
ENDFORM.
FORM file_value_help.
CALL FUNCTION 'F4_FILENAME'
EXPORTING
field_name = 'P_FILE'
> IMPORTING
file_name = p_file.
ENDFORM.
FORM file_value_upload_help.
CALL FUNCTION 'F4_FILENAME'
EXPORTING
field_name = 'p_ufile'
IMPORTING
file_name = p_ufile.
ENDFORM.
*& Report ZRDS_CLASSICAL_REPORT
*&---------------------------------------------------------------------*
*&
*&---------------------------------------------------------------------*
REPORT zrds_classical_report.
TYPES : BEGIN OF ty_sflight,
carrid TYPE s_carr_id,
connid TYPE s_conn_id,
fldate TYPE s_date,
price TYPE s_price,
planetype TYPE s_planetye,
seatsmax TYPE s_seatsmax,
seatsocc TYPE s_seatsocc,
END OF ty_sflight.
DATA: it_sflight TYPE TABLE OF ty_sflight,
it_sflight_upload type TABLE of sflight,
wa_sflight TYPE ty_sflight.
SELECTION-SCREEN BEGIN OF BLOCK b1 WITH FRAME TITLE TEXT-001.
PARAMETERS : p_dload AS CHECKBOX USER-COMMAND flag, "to select download option
p_uload AS CHECKBOX USER-COMMAND flag . "to select upload option
SELECTION-SCREEN END OF BLOCK b1.
SKIP.
SELECTION-SCREEN BEGIN OF BLOCK b2 WITH FRAME TITLE TEXT-002.
PARAMETERS : p_carrid TYPE s_carr_id MODIF ID m1,
p_file TYPE rlgrap-filename MODIF ID m1.
SELECTION-SCREEN END OF BLOCK b2.
SKIP.
SELECTION-SCREEN BEGIN OF BLOCK b3 WITH FRAME TITLE TEXT-003.
PARAMETERS : p_ufile TYPE rlgrap-filename MODIF ID m2.
SELECTION-SCREEN END OF BLOCK b3.
AT SELECTION-SCREEN OUTPUT.
LOOP AT SCREEN.
IF p_dload NE 'X' AND "if download option is not selected, hide the download screen
screen-group1 = 'M1'.
screen-active = '0'.
ENDIF.
MODIFY SCREEN.
ENDLOOP.
LOOP AT SCREEN.
IF p_uload NE 'X' AND "if upload option is not selected, hide the upload screen
screen-group1 = 'M2'.
screen-active = '0'.
ENDIF.
MODIFY SCREEN.
ENDLOOP.
AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_file.
PERFORM file_value_help.
AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_ufile.
PERFORM file_value_upload_help.
AT SELECTION-SCREEN.
PERFORM validation. "validation on screen
START-OF-SELECTION.
PERFORM get_flight_details.
END-OF-SELECTION.
PERFORM display_details.
IF p_dload = 'X'.
PERFORM download_flight_data.
ENDIF.
IF p_uload = 'X'.
PERFORM upload_flight_data.
ENDIF.
FORM validation.
IF p_dload = 'X' AND p_uload = 'X'.
MESSAGE 'Please select only one option' TYPE 'E'.
ENDIF.
ENDFORM.
FORM get_flight_details.
SELECT carrid
connid
fldate
price
planetype
seatsmax
seatsocc
FROM sflight
INTO TABLE it_sflight
WHERE carrid = p_carrid.
ENDFORM.
FORM display_details.
IF it_sflight IS NOT INITIAL.
LOOP AT it_sflight INTO wa_sflight.
WRITE: / wa_sflight-carrid, wa_sflight-connid, wa_sflight-fldate, wa_sflight-price, wa_sflight-planetype, wa_sflight-seatsmax, wa_sflight-seatsocc.
ENDLOOP.
ENDIF.
ENDFORM.
FORM download_flight_data.
DATA : lv_file TYPE string .
lv_file = p_file .
CALL FUNCTION 'GUI_DOWNLOAD'
EXPORTING
filename = lv_file
* FILETYPE = 'ASC'
* APPEND = ' '
write_field_separator = 'x'
TABLES
data_tab = it_sflight
.
IF sy-subrc = 0.
MESSAGE 'Download is successful' TYPE 'I'.
ENDIF.
ENDFORM.
FORM upload_flight_data.
data : p_local type string.
p_local = p_ufile.
CALL FUNCTION 'GUI_UPLOAD'
EXPORTING
filename = p_local
FILETYPE = 'ASC'
HAS_FIELD_SEPARATOR = 'x'
TABLES
data_tab = it_sflight_upload
EXCEPTIONS
BAD_DATA_FORMAT = 8
.
IF sy-subrc = 0.
MESSAGE 'Uploaded successfully' type 'I'.
ENDIF.
ENDFORM.
FORM file_value_help.
CALL FUNCTION 'F4_FILENAME'
EXPORTING
field_name = 'P_FILE'
> IMPORTING
file_name = p_file.
ENDFORM.
FORM file_value_upload_help.
CALL FUNCTION 'F4_FILENAME'
EXPORTING
field_name = 'p_ufile'
IMPORTING
file_name = p_ufile.
ENDFORM.
- AS CHECKBOX is a keyword to enable a checkbox before a field.
- NE stands for Not Equal to
- GUI_DOWNLOAD is an in-built function of SAP ABAP to download from the Application Server to the Presentation layer.
- GUI_UPLOAD is an in-built function of SAP ABAP to upload a file from the Presentation layer to the Application layer.
- 'F4_FILENAME' is an in-built function of SAP ABAP to provide F4 help for a file on the presentation layer.
- sy-subrc = 0 means the execution is performed successfully.
Interactive reports in SAP ABAP
Interactive Report includes sub-reports. Interactive Reports, as the name suggests, allow users to interact with the reports. When we click on the data of the first report based on the user command, the second report gets generated.
Before jumping to the code, let's look at the case study.
Let's consider an example of the Interactive Report Program.
- Use the select-options to enter the range of records for 'SFLIGHT' table.
- Once the user hits the F8 button, the record will be fetched from 'SFLIGHT' table. When the user double-clicks on specific data from the 'SFLIGHT' table, data of the particular record will be fetched from the 'SPFLI' table.
NOTE: SFLIGHT is the standard SAP table that is used to store the flight data. SPFLI is the standard SAP table that is used to store the flight schedule.
OUTPUT:
LOGIC:
*&---------------------------------------------------------------------*
*& Report ZRDS_INTERACTIVE_REPORT
*&---------------------------------------------------------------------*
*&
*&---------------------------------------------------------------------*
REPORT zrds_interactive_report.
TYPES : BEGIN OF ty_sflight,
carrid TYPE s_carr_id,
connid TYPE s_conn_id,
fldate TYPE s_date,
price TYPE s_price,
planetype TYPE s_planetye,
seatsmax TYPE s_seatsmax,
seatsocc TYPE s_seatsocc,
END OF ty_sflight.
TYPES: BEGIN OF ty_spfli,
carrid TYPE s_carr_id,
connid TYPE s_conn_id,
countryfr TYPE land1,
cityfrom TYPE s_from_cit,
airpfrom TYPE s_fromairp,
countryto TYPE land1,
cityto TYPE s_to_city,
airpto TYPE s_toairp,
END OF ty_spfli.
DATA: it_sflight TYPE TABLE OF ty_sflight,
it_spfli TYPE TABLE OF ty_spfli,
wa_sflight TYPE ty_sflight,
wa_spfli TYPE ty_spfli.
DATA: lv_carrid TYPE ty_sflight-carrid.
SELECT-OPTIONS : s_carrid FOR lv_carrid.
START-OF-SELECTION.
SELECT carrid
connid
fldate
price
planetype
seatsmax
seatsocc
FROM sflight
INTO TABLE it_sflight
WHERE carrid IN s_carrid.
LOOP AT it_sflight INTO wa_sflight.
at FIRST.
write: / 'carrid' , 10 'connid', 20 'fldate', 50 'price', 60 'planetype', 75 'seatsmax', 85 'seatsocc'.
endat.
WRITE: / wa_sflight-carrid,10 wa_sflight-connid, 20 wa_sflight-fldate, 35 wa_sflight-price, 60 wa_sflight-planetype, 75 wa_sflight-seatsmax,
85 wa_sflight-seatsocc.
FORMAT HOTSPOT ON. "to enable hand like icon
HIDE: wa_sflight-carrid, wa_sflight-connid. " to enable interactive report
ENDLOOP.
AT LINE-SELECTION.
IF sy-lsind = 1.
SELECT carrid
connid
countryfr
cityfrom
airpfrom
countryto
cityto
airpto
FROM spfli
INTO TABLE it_spfli
WHERE carrid = wa_sflight-carrid and connid = wa_sflight-connid.
LOOP AT it_spfli INTO wa_spfli.
at FIRST.
write: /5 'carrid' , 15 'connid', 25 'countryfrm', 40 'cityfrom', 55 'airpfrom', 70 'countryto', 85 'cityto', 100 'airpto '.
endat.
write: /5 wa_spfli-carrid, 15 wa_spfli-connid , 25 wa_spfli-countryfr, 40 wa_spfli-cityfrom, 55 wa_spfli-airpfrom, 70 wa_spfli-
countryto,85 wa_spfli-cityto, 100 wa_spfli-airpto.
ENDLOOP.
endif.
*& Report ZRDS_INTERACTIVE_REPORT
*&---------------------------------------------------------------------*
*&
*&---------------------------------------------------------------------*
REPORT zrds_interactive_report.
TYPES : BEGIN OF ty_sflight,
carrid TYPE s_carr_id,
connid TYPE s_conn_id,
fldate TYPE s_date,
price TYPE s_price,
planetype TYPE s_planetye,
seatsmax TYPE s_seatsmax,
seatsocc TYPE s_seatsocc,
END OF ty_sflight.
TYPES: BEGIN OF ty_spfli,
carrid TYPE s_carr_id,
connid TYPE s_conn_id,
countryfr TYPE land1,
cityfrom TYPE s_from_cit,
airpfrom TYPE s_fromairp,
countryto TYPE land1,
cityto TYPE s_to_city,
airpto TYPE s_toairp,
END OF ty_spfli.
DATA: it_sflight TYPE TABLE OF ty_sflight,
it_spfli TYPE TABLE OF ty_spfli,
wa_sflight TYPE ty_sflight,
wa_spfli TYPE ty_spfli.
DATA: lv_carrid TYPE ty_sflight-carrid.
SELECT-OPTIONS : s_carrid FOR lv_carrid.
START-OF-SELECTION.
SELECT carrid
connid
fldate
price
planetype
seatsmax
seatsocc
FROM sflight
INTO TABLE it_sflight
WHERE carrid IN s_carrid.
LOOP AT it_sflight INTO wa_sflight.
at FIRST.
write: / 'carrid' , 10 'connid', 20 'fldate', 50 'price', 60 'planetype', 75 'seatsmax', 85 'seatsocc'.
endat.
WRITE: / wa_sflight-carrid,10 wa_sflight-connid, 20 wa_sflight-fldate, 35 wa_sflight-price, 60 wa_sflight-planetype, 75 wa_sflight-seatsmax,
85 wa_sflight-seatsocc.
FORMAT HOTSPOT ON. "to enable hand like icon
HIDE: wa_sflight-carrid, wa_sflight-connid. " to enable interactive report
ENDLOOP.
AT LINE-SELECTION.
IF sy-lsind = 1.
SELECT carrid
connid
countryfr
cityfrom
airpfrom
countryto
cityto
airpto
FROM spfli
INTO TABLE it_spfli
WHERE carrid = wa_sflight-carrid and connid = wa_sflight-connid.
LOOP AT it_spfli INTO wa_spfli.
at FIRST.
write: /5 'carrid' , 15 'connid', 25 'countryfrm', 40 'cityfrom', 55 'airpfrom', 70 'countryto', 85 'cityto', 100 'airpto '.
endat.
write: /5 wa_spfli-carrid, 15 wa_spfli-connid , 25 wa_spfli-countryfr, 40 wa_spfli-cityfrom, 55 wa_spfli-airpfrom, 70 wa_spfli-
countryto,85 wa_spfli-cityto, 100 wa_spfli-airpto.
ENDLOOP.
endif.
CODE EXPLANATION:
- 'FORMAT HOTSPOT ON' will enable a hand like icon on the screen.
- HIDE is the statement that is used to store the single record of the user.
- 'AT LINE-SELECTION' makes the report interactive.
This serves the purpose of this article. I hope the above illustrations were helpful for precise understanding.
Also review the previous articles:
Feel free to drop your suggestions or comments in the comments sections below and stay tuned for the final part of 5 part series in SAP ABAP.
0 Comments