1.Starting the debugger in a modal screen
Usually in ABAP, when you want to start the debugger from a certain point, you just have to write “/H” in the
command window. But if you are in a modal screen or in a message show, you cannot write the traditional
“/H”, here is how to do it :
- Open notepad and paste these lines in a document : [FUNCTION] Command=/H Title=Debugger Type=SystemCommand
- Save this document anywhere on your local pc. ex: c:breakpoint.txt
- Drag&drop this file from the explorer to your ABAP modal screen…. et voila !!
2.Retrieving field names dynamically
Sometimes you may have only a table name and want to retrieve the name of each field of the corresponding table. For example, when you want to use ASSIGN COMPONENT fieldname OF TABLE table.
An ABAPer’s first reaction is to read the standard ABAP basis tables DD02L, DD03L, etc
This way of reading fields is very slow. Use methods from the class CL_ABAP_TYPEDESCR instead.
Example:
data: descr_struc_ref TYPE REF TO cl_abap_structdescr.
descr_struc_ref ?= cl_abap_typedescr=>describe_by_name(‘SFLIGHT’ ).
Here is the result of descr_struct_ref after the execution of this piece of code
ABSOLUTE_NAME C 200 TYPE=SFLIGHT
TYPE_KIND C 1 u
LENGTH I 4 80
DECIMALS I 4 0
KIND C 1 S
STRUCT_KIND C 1 F
COMPONENTS h 8 Table[14x40]
HAS_INCLUDE C 1
The table COMPONENTS is filled with :
LENGTH DECIMALS TYPE_KIND NAME
3 | 0 |C |MANDT
3 | 0 |C |CARRID
4 | 0 |N |CONNID
8 | 0 |D |FLDATE
etc.
You have the fields name and a lot more information about the table. This class can also handle structure, table type, etc.
Note that this method is very quick because it uses the database layer directly thanks to SYSTEM CALLs.
To have a look at the class, use transaction SE24.
3.Restricting the selection screen
When you make a select-option for an input to your program, for each field, the default selection screen looks like this:
And the default possible selections are:
but sometime you don’t want to give the user the possibility to select a range, select values “greater than”, etc.
Here is a small example on how to do it:
REPORT ZDANY_RESTRICT_SELECTION.
* Include type pool SSCR
TYPE-POOLS sscr.
TABLES : sflight.
* defining the selection-screen
select-options :
s_carrid for sflight-carrid,
s_connid for sflight-connid.
* Define the object to be passed to the RESTRICTION parameter
DATA restrict TYPE sscr_restrict.
* Auxiliary objects for filling RESTRICT
DATA : optlist TYPE sscr_opt_list,
ass type sscr_ass.
INITIALIZATION.
* Restricting the carrid selection to only EQ and ‘BT’.
optlist-name = ‘OBJECTKEY1′.
optlist-options-eq = ‘X’.
optlist-options-bt = ‘X’.
APPEND optlist TO restrict-opt_list_tab.
ass-kind = ‘S’.
ass-name = ‘S_carrid’.
ass-sg_main = ‘I’.
ass-sg_addy = space.
ass-op_main = ‘OBJECTKEY1′.
APPEND ass TO restrict-ass_tab.
* Restricting the connid selection to CP, GE, LT, NE.
optlist-name = ‘OBJECTKEY2′.
optlist-options-cp = ‘X’.
optlist-options-ge = ‘X’.
optlist-options-lt = ‘X’.
optlist-options-ne = ‘X’.
APPEND optlist TO restrict-opt_list_tab.
ass-kind = ‘S’.
ass-name = ‘S_connid’.
ass-sg_main = ‘I’.
ass-sg_addy = space.
ass-op_main = ‘OBJECTKEY2′.
APPEND ass TO restrict-ass_tab.
CALL FUNCTION ‘SELECT_OPTIONS_RESTRICT’
EXPORTING
restriction = restrict
EXCEPTIONS
TOO_LATE = 1
REPEATED = 2
SELOPT_WITHOUT_OPTIONS = 3
SELOPT_WITHOUT_SIGNS = 4
INVALID_SIGN = 5
EMPTY_OPTION_LIST = 6
INVALID_KIND = 7
REPEATED_KIND_A = 8
OTHERS = 9.
IF sy-subrc <> 0.
MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
ENDIF.
when you do this piece of code, you will notice that for carrid, the selection screen is now restricted :
You want to call a program on your PC from an ABAP program ? try this function :
*This example calls the dos prompt DATA:
i_returncode TYPE i.
CALL FUNCTION ‘GUI_EXEC’
EXPORTING
command = ‘CMD’ <<==– CMD calls the DOS prompt but you can place any program here
* PARAMETER = ‘test’
IMPORTING
returncode = i_returncode.
there is a lot of other functions to communicate with the PC like:
GUI_CREATE_DIRECTORY GUI_DELETE_FILE GUI_DOWNLOAD
GUI_EXEC
GUI_FILE_SAVE_DIALOG
GUI_GET_DESKTOP_INFO
GUI_GET_FILE_INFO
etc…
5.Using a variable from a calling program without passing it in parameter
Did you ever try to use a variable from another program without passing this variable as a parameter. This is very useful when you CANNOT add the field as a standard parameter. For example, when you want to use a variable in a BADI which is not already passed as a parameter. Another excellent example is when you make a correction in a function and you want to keep the installation of the OSS note automatic (if you add parameters in a note, the user will have to install the note manually).
Here is a small example on how to do this :
REPORT zdany_test_var_from_fm.
TABLES: spfli.
DATA dbcnt TYPE sy-dbcnt.
DATA: itab TYPE spfli_tab.
SELECT * FROM spfli INTO TABLE itab UP TO 2 ROWS.
dbcnt = sy-dbcnt.
CALL FUNCTION ‘ZFUNCTION’.
FUNCTION zfunction.
* We want to use the DBCNT from the program ZDANY_TEST_VAR_FROM_FM
DATA: field(50).
FIELD-SYMBOLS: <dbcnt>.
field = ‘(ZDANY_TEST_VAR_FROM_FM)dbcnt’.
ASSIGN (field) TO <dbcnt>.
WRITE <dbcnt>.
* We want to use the internal table from the program ZDANY_TEST_VAR_FROM_FM
DATA: itab TYPE spfli.
FIELD-SYMBOLS: <itab> TYPE spfli_tab.
field = ‘(ZDANY_TEST_VAR_FROM_FM)ITAB[]‘.
ASSIGN (field) TO <itab>.
LOOP AT <itab> INTO itab.
WRITE: / itab-carrid, itab-connid.
ENDLOOP.
ENDFUNCTION
6.List of internal tables while debugging
There can be times when you need a list of active internal tables while you are debugging ABAP code.
The first method, lets you see which table required a lot of memory
- While on debug mode follow the menu path GOTO->Show condition->memory use
- Select tabstrip: Memory use – ranked list
The second method provides a very detailed list
- While in debug mode, choose GOTO->System Areas->internal information
- A list appears on the debug screen
- By default the area editbox contains a ?
- Now enter DSEG in that field.
- Press Return .
- You will get a list of all the tables active in that program along with some other attributes.
- The type and size of the records determine if they are internal tables and how many records there are.
You can play around and try the other area than DSEG, some are fascinating.
7.Showing a progress bar in ABAP
This process is very simple but a lot of programmers don’t know how to do it. You just have to call the ABAP FM SAPGUI_PROGRESS_INDICATOR, at the appropriate points.
Below is a simple example, give it a try !!
REPORT zdany_progress_bar.
DATA: percentage_text TYPE c LENGTH 5.
DO 100 TIMES.
WAIT UP TO ’0.5′ SECONDS.
percentage_text(3) = sy-index.
percentage_text+3 = ‘%’.
CALL FUNCTION ‘SAPGUI_PROGRESS_INDICATOR’
EXPORTING
percentage = sy-index
text = percentage_text.
ENDDO.
Really, we should use time stamp in our abap programs instead of the traditional date and time fields.
When we have to do some calculation on a time stamp is not as simple as on a date field.
For example, to add an hour to a timestamp, many people will:
- convert the time stamp into a standard date and field
- add an hour
- convert back the date and time to a timestamp
But, it is the slowest method you can use.
Instead, use class CL_ABAP_TSTMP.
It enables you to make whatever calculation you want on a time stamp.
In the following example, 1 hour is added
REPORT zdany_tstamp.
DATA : l_tstamp TYPE timestamp,
l_tstamp_out TYPE timestamp.
GET TIME STAMP FIELD l_tstamp.
TRY.
CALL METHOD cl_abap_tstmp=>add
EXPORTING
tstmp = l_tstamp
secs = 3600 <<<===— 1 hour = 3600 seconds
RECEIVING
r_tstmp = l_tstamp_out.
CATCH cx_parameter_invalid_range .
WRITE ‘invalid range’.
EXIT.
CATCH cx_parameter_invalid_type .
WRITE ‘invalid type’.
EXIT.
ENDTRY.
WRITE l_tstamp time zone ‘UTC ‘.
SKIP.
WRITE l_tstamp_out time zone ‘UTC ‘.
9.Generating your own standard F4 help
To avoid the standard F4 help to be show, insert the event PROCESS ON-VALUE-REQUEST request in the program and add a field statement for the field that should trigger the F4 help. In the module called from PROCESS ON-VALUE-REQUEST request, call function module F4IF_FIELD_VALUE_REQUEST.
Example:
process before output.
…..
process after input.
…..
PROCESS ON VALUE-REQUEST. FIELD sflight-carrid MODULE f4_help_for_carrid.
MODULE f4_help_for_carrid INPUT.
* NOTE:
* Tabname/fieldname is the name of the table and field
* for which F4 should be shown.
*
* Dynprog/Dynpnr/Dynprofield are the names of the Progran/Dynpro/Field
* in which the f4 value should be returned.
*
* Value: The value of the Dynpro field when calling the F4 help.
* You can limit the values shown, by inseting a value in this parameter
* e.g ‘A*’ to show only values beginning with A
CALL FUNCTION ‘F4IF_FIELD_VALUE_REQUEST’
EXPORTING
tabname = ‘SFLIGHT’
fieldname = ‘CARRID’
* SEARCHHELP = ‘ ‘
* SHLPPARAM = ‘ ‘
dynpprog = ‘ZDANY_F4_OWN_CALL’
dynpnr = ’0100′
dynprofield = ‘SFLIGHT-CARRID’
* STEPL = 0
value = ‘A*’
* MULTIPLE_CHOICE = ‘ ‘
* SHOW = ‘ ‘
* SUPPRESS_RECORDLIST = ‘ ‘
* CALLBACK_PROGRAM = ‘ ‘
* CALLBACK_FORM = ‘ ‘
* TABLES
* RETURN_TAB =
* EXCEPTIONS
* FIELD_NOT_FOUND = 1
* NO_HELP_FOR_FIELD = 2
* INCONSISTENT_HELP = 3
* NO_VALUES_FOUND = 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.
ENDMODULE. ” F4_help_for_carrid INPUT
To control F4 help in a selection screen use the AT SELECTION-SCREEN ON VALUE-REQUEST FOR
<field> event.
Note that for ranges both the low and high value of the field must have there own ON VALUE-REQUEST
Example:
AT SELECTION-SCREEN ON VALUE-REQUEST FOR s_prctr-low.
PERFORM f4_help_carrid.
AT SELECTION-SCREEN ON VALUE-REQUEST FOR s_prctr-high.
PERFORM f4_help_carrid.
10.Making an ALV Grid in 3 lines
Did you know that you can make an ALV Grid very quick; you don’t need to define a layout, a fieldcatalog, a container and all the other small things we usually define in an ALV Grid. If we don’t need to finetune the ALV Grid and just want to show a list on the screen or to the printer, here is a very simple way to proceed:
DATA: l_alv TYPE REF TO cl_gui_alv_grid,
lt_sflight TYPE TABLE OF sflight.
SELECT * FROM sflight INTO TABLE lt_sflight.
* Creation of the ALV object, when we use cl_gui_container=>screen0 as parent, the ALVGrid control will
* automatically use the full screen to show the grid, NO CONTAINER DEFINITION IS REQUIRED !
MAKE OBJECT l_alv EXPORTING i_parent = cl_gui_container=>screen0.
* calling the show of the grid, the system will automatically make the fieldcatalog based
* on the table name you pass in parameter
CALL METHOD l_alv->set_table_for_first_display
EXPORTING i_structure_name = ‘SFLIGHT’
CHANGING it_outtab = lt_sflight.
* You have to make an EMPTY screen, place NOTHING in the layout and this is going to work
CALL SCREEN 100
Related posts:
- SAP ABAP Interview Questions part 2
- SAP ABAP Scripts Question and Answers Part 6
- SAP ABAP Interview Questions part 4
- SAP ABAP BDC Programs Questions 2
- SAP ABAP Written Test Questions
- 100 SAP ABAP Interview Questions
- SAP ABAP Questions with Answers
- SAP ABAP Report Programming Questions 3
- SAP ABAP Interview Questions part 5
- SAP ABAP Interview Questions part 1
SAP BI Online Training
SAP BOBJ |SAP BO 4.0 Online Training
0 Comments until now.
Comment!