Вы находитесь на странице: 1из 18

Reusable

snippets of
code
Some helpful pieces of Code

Sl. No. Topic Page No.

1. FM for getting values from a SET 2

2. Filling a RANGE from an Internal table 3

3. F4 Search help for DIRECTORIES on PRESENTATION server 4

4. F4 Search help for FILES on PRESENTATION server 5

5. F4 Search help for PATH+FILENAME on PRESENTATION server 7

6. FM for checking existence of a PATH/DIR on PRESENTATION server 8


7. FM for checking existence of a FILE on PRESENTATION server 9
8. F4 Search help for PATH(DIR) on APPLICATION server 11
9. F4 Search help for FILES on APPLICATION server 12
10. FM for checking existence of PATH(DIR) on APPLICATION server 14
11. For checking existence of a FILE on APPLICATION server 15
12. FM for checking the EXTENSION of an input file 17

1
1. FM for getting values from a SET:

For getting the values of a given set, you can first call the FM G_SET_GET_ID_FROM_NAME to get the ID
of the set. Further you can call the FM G_SET_GET_ALL_VALUES, passing to it the set ID received from
the above called FM, to populate the required internal table with values of the set. Also, to view the
values in a set, you can open the transaction GS03 and to create a set you can go to transaction GS01.
Following is an example piece of code for the same:
=====================================================================================
* Local Dictionary
CONSTANTS lc_setname TYPE char13 VALUE 'ZK4_CONT_DISP'.

DATA: i_set_values TYPE STANDARD TABLE OF rgsb4,


l_set_id TYPE sethier-setid.

CLEAR l_set_id.
REFRESH i_set_values.

CALL FUNCTION 'G_SET_GET_ID_FROM_NAME'


EXPORTING
shortname = lc_setname "ZK4_CONT_DISP
IMPORTING
new_setid = l_set_id
EXCEPTIONS
no_set_found = 1
no_set_picked_from_popup = 2
wrong_class = 3
wrong_subclass = 4
table_field_not_found = 5
fields_dont_match = 6
set_is_empty = 7
formula_in_set = 8
set_is_dynamic = 9
OTHERS = 10.
IF sy-subrc EQ 0.

CALL FUNCTION 'G_SET_GET_ALL_VALUES'


EXPORTING
setnr = l_set_id
TABLES
set_values = i_set_values
EXCEPTIONS
set_not_found = 1
OTHERS = 2.
IF sy-subrc EQ 0.
SORT i_set_values BY from.
ENDIF.
ENDIF.

2
2. Filling a RANGE from an Internal Table:

For filling a range from an internal table following is a sample piece of code:

Here we are creating a range for all the data in the internal table I_SET_VALUES. In the below example
the range will contain data of field CAUSRICH of table ZK4_HEAD and hence declared correspondingly.
The internal table I_SET_VALUES also has the data of CAUSIRICH field of table ZK4_HEAD.

=====================================================================================

* Local Dictionary

DATA: ra_zk4_cont_disp TYPE RANGE OF zk4_head-causrich,


wa_zk4_cont_disp LIKE LINE OF ra_zk4_cont_disp.

CONSTANTS: lc_i TYPE char1 VALUE 'I',


lc_eq TYPE char2 VALUE 'EQ',
lc_bt TYPE char2 VALUE 'BT'.

* Filling the range RA_ZK4_CONT_DISP with values from table I_SET_VALUES


REFRESH ra_zk4_cont_disp.
CLEAR wa_zk4_cont_disp.

IF <fs_set_values> IS ASSIGNED.
UNASSIGN <fs_set_values>.
ENDIF.

LOOP AT i_set_values ASSIGNING <fs_set_values>.


wa_zk4_cont_disp-sign = lc_i."I
wa_zk4_cont_disp-low = <fs_set_values>-from.
wa_zk4_cont_disp-high = <fs_set_values>-to.

IF <fs_set_values>-from EQ <fs_set_values>-to.
wa_zk4_cont_disp-option = lc_eq."EQ
ELSE.
wa_zk4_cont_disp-option = lc_bt."BT
ENDIF.

APPEND wa_zk4_cont_disp TO ra_zk4_cont_disp.


CLEAR wa_zk4_cont_disp.
ENDLOOP.

IF <fs_set_values> IS ASSIGNED.
UNASSIGN <fs_set_values>.
ENDIF.

3
3. F4 Search help for DIRECTORIES on PRESENTATION server

To create a F4 search help for the Directories present on the PRESENTATION SERVER (Local
Directories on your system) you can call the method DIRECTORY_BROWSE of class
CL_GUI_FRONTEND_SERVICES. This method should be called under the event:
AT SELECTION-SCREEN ON VALUE-REQUEST. Following is a sample piece of code for your reference
where P_DIR_P is an input parameter, which will contain the input Directory.

==================================================================================

AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_dir_p.

* Subroutine for F4 Search help for directory on Presentation Server.


PERFORM f_f4_dir_pres.

Below is the code for the subroutine F_F4_DIR_PRES

*&---------------------------------------------------------------------*
*& Form F_F4_DIR_PRES
*&---------------------------------------------------------------------*
FORM f_f4_dir_pres.

CONSTANTS lc_title TYPE string VALUE 'SELECT DIRECTORY'.

DATA: l_dir TYPE string.


CLEAR l_dir.

CALL METHOD cl_gui_frontend_services=>directory_browse


EXPORTING
window_title = lc_title "'SELECT DIRECTORY'
CHANGING
selected_folder = l_dir
EXCEPTIONS
cntl_error = 1
error_no_gui = 2
not_supported_by_gui = 3
OTHERS = 4.

IF sy-subrc EQ 0.
p_dir_p = l_dir.
ENDIF

ENDFORM. " F_F4_DIR_PRES

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

4
4. F4 Search help for FILES on PRESENTATION server

To create a F4 search help for the Files present on the PRESENTATION SERVER (Local Files on your
system) you can call the method FILE_OPEN_DIALOG of class CL_GUI_FRONTEND_SERVICES. This
method should be called under the event: AT SELECTION-SCREEN ON VALUE-REQUEST. Following is
a sample piece of code for your reference where P_FILE_P is an input parameter, which will contain
the input File. Only if the input Directory (P_DIR_P) is enhanced, the F4 help will be created for Files.

==================================================================================

AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_file_p.

* Check the Directory parameter.


IF p_dir_p IS NOT INITIAL.
* Subroutine for F4 search help for FILE on Presentation Server.
PERFORM f_f4_file_pres.
ENDIF.

Below is the code for the subroutine F_F4_FILE_PRES

*&---------------------------------------------------------------------*
*& Form F_F4_FILE_PRES
*&---------------------------------------------------------------------*
* For creating F4 search help for Files on Presentation Server
*----------------------------------------------------------------------*
FORM f_f4_file_pres.

DATA: l_temp_dir TYPE string,


l_file TYPE filetable,
wa_line TYPE file_table,
l_rc TYPE i,
l_len TYPE i.

CONSTANTS lc_title TYPE string VALUE 'SELECT FILE'.

CLEAR : l_file,l_rc,l_temp_dir,wa_line.

l_temp_dir = p_dir_p.

CALL METHOD cl_gui_frontend_services=>file_open_dialog


EXPORTING
window_title = lc_title "'SELECT FILE'
initial_directory = l_temp_dir
CHANGING
file_table = l_file
rc = l_rc
EXCEPTIONS
file_open_dialog_failed = 1
cntl_error = 2

5
error_no_gui = 3
not_supported_by_gui = 4
OTHERS = 5.
IF sy-subrc EQ 0.
IF l_rc = 1.
READ TABLE l_file INDEX 1 INTO wa_line.
IF wa_line-filename IS NOT INITIAL.

* Calling FM TRINT_SPLIT_FILE_AND_PATH to get File Name


CALL FUNCTION 'TRINT_SPLIT_FILE_AND_PATH'
EXPORTING
full_name = wa_line-filename
IMPORTING
stripped_name = p_file_p
EXCEPTIONS
x_error = 1
OTHERS = 2.
IF sy-subrc <> 0.
MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
ENDIF.
ENDIF.
ENDIF.
ENDIF.

ENDFORM. " F_F4_FILE_PRES

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

6
5. F4 Search help for PATH+FILENAME on PRESENTATION server

To create a F4 search help for PATH + FILENAME together in one input parameter, present on the
PRESENTATION SERVER, you can call the FM F4_FILENAME. This Function module should be called
under the event: AT SELECTION-SCREEN ON VALUE-REQUEST. Following is a sample piece of code for
your reference where P_PATH_F is an input parameter, which will contain the input path and filename.

==================================================================================

AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_path_f.

* Subroutine for F4 search help for PATH & FILE on Presentation Server.
PERFORM f_f4_path_file_pres.

Below is the code for the subroutine F_F4_PATH_FILE_PRES:

*&---------------------------------------------------------------------*
*& Form F_F4_PATH_FILE_PRES
*&---------------------------------------------------------------------*

FORM f_f4_path_file_pres .

* Constants
CONSTANTS lc_pfile TYPE dynfnam VALUE 'P_PATH_F'.

* Calling FM for creating serach help for files on Presentation server.


CALL FUNCTION 'F4_FILENAME'
EXPORTING
field_name = lc_pfile "'P_PATH_F'
IMPORTING
file_name = p_path_f.

ENDFORM. ”F_F4_PATH_FILE_PRES

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

7
6. FM for checking existence of a PATH/DIR on PRESENTATION server

To check the existence of a PATH/ DIRECTORY on the presentation server, entered by the user, you can
call the method DIRECTORY_EXIST of the class CL_GUI_FRONTEND_SERVICES. Following is a sample
piece of code for your reference where P_DIR_P is the input parameter containing the Path/ Directory
name. The Result parameter of the method returns ‘X’ when Directory exists otherwise returns Blank.

=====================================================================================

* Subrouine for checking existence of Directory on Presentation Server.


PERFORM f_check_dir_pres.

Below is the code for the subroutine F_CHECK_DIR_PRESS:

*&---------------------------------------------------------------------*
*& Form F_CHECK_DIR_PRES
*&---------------------------------------------------------------------*
* For checking input Directory's existence on Presentation server.
*----------------------------------------------------------------------*
FORM f_check_dir_pres .

DATA: fl_flag TYPE abap_bool,


l_dir TYPE string.

CONSTANTS: lc_e TYPE char01 VALUE 'E',


lc_x TYPE abap_bool VALUE 'X'.

CLEAR:l_dir, fl_flag.

l_dir = p_dir_p.
CALL METHOD cl_gui_frontend_services=>directory_exist
EXPORTING
directory = l_dir
RECEIVING
result = fl_flag
EXCEPTIONS
cntl_error = 1
error_no_gui = 2
wrong_parameter = 3
not_supported_by_gui = 4
OTHERS = 5.

IF sy-subrc NE 0 OR fl_flag NE lc_x.


* Error message:'Directory doesnot exist'.
MESSAGE s208(00) WITH text-003 DISPLAY LIKE lc_e.
ENDIF.
ENDFORM. ”F_CHECK_DIR_PRES

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

8
7. FM for checking existence of FILE on PRESENTATION server

To check the existence of a FILE on the presentation server, entered by the user, you can call the
method FILE_EXIST of the class CL_GUI_FRONTEND_SERVICES. Following is a sample piece of code for
your reference where P_DIR_P is the input parameter containing the Path/ Directory name and P_FILE_P
is the input file whose existence is to be checked. The Result parameter of the method returns ‘X’ when
the File exists otherwise it returns Blank.

=====================================================================================

* Subrouine for checking Input File's Existence on Presentation Server.


PERFORM f_check_file_pres.

Below is the code for the subroutine F_CHECK_FILE_PRES:

*&---------------------------------------------------------------------*
*& Form F_CHECK_FILE_PRES
*&---------------------------------------------------------------------*
* For checking input File's existence on Presentation server.
*----------------------------------------------------------------------*
FORM f_check_file_pres .

CONSTANTS: lc_e TYPE char01 VALUE 'E',


lc_x TYPE abap_bool VALUE 'X'.

DATA: fl_flag TYPE abap_bool,


l_file TYPE string,
l_len TYPE i.

CLEAR: fl_flag,l_file.
l_len = STRLEN( p_dir_p ).

l_len = l_len - 1.

* Checking if at the end of the Directory '\' is present or not.


IF p_dir_p+l_len(1) NE '\'.
CONCATENATE p_dir_p
'\'
INTO p_dir_p.
ENDIF.

* Concatenating Directory and file name.


CONCATENATE p_dir_p
p_file_p
INTO l_file.

9
CALL METHOD cl_gui_frontend_services=>file_exist
EXPORTING
file = l_file
RECEIVING
result = fl_flag
EXCEPTIONS
cntl_error = 1
error_no_gui = 2
wrong_parameter = 3
not_supported_by_gui = 4
OTHERS = 5.

IF sy-subrc NE 0 OR fl_flag NE lc_x.


* Error message:'File doesnot exist'.
MESSAGE s208(00) WITH text-001 DISPLAY LIKE lc_e.
ENDIF.

ENDFORM. " F_CHECK_FILE_PRES

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

10
8. F4 Search help for PATH/DIRECTORY on APPLICATION server

To create a F4 search help for the Directory or Path present on the APPLICATION SERVER (i.e. Directories
on SAP system) you can call the Function Module /SAPDMC/LSM_F4_SERVER_FILE. This method should
be called under the event: AT SELECTION-SCREEN ON VALUE-REQUEST. Following is a sample piece of
code for your reference where P_DIR_A is an input parameter, which will contain the input Directory.

=====================================================================================

AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_dir_a.

* Subroutine for F4 search help for Directory/Path on Application Server.


PERFORM f_f4_dir_appl.

Below is the code for the subroutine F_F4_DIR_APPL:

*&---------------------------------------------------------------------*
*& Form F_F4_DIR_APPL
*&---------------------------------------------------------------------*
* For creating F4 search help for Directory/Path on Application Server
*----------------------------------------------------------------------*
FORM f_f4_dir_appl .

CONSTANTS lc_x TYPE char01 VALUE 'X'.

CALL FUNCTION '/SAPDMC/LSM_F4_SERVER_FILE'


EXPORTING
directory = p_dir_a
filemask = lc_x "'X' only directory can be chosen and not file
IMPORTING
serverfile = p_dir_a
EXCEPTIONS
canceled_by_user = 1
OTHERS = 2.

IF sy-subrc = 0.
* Do nothing.
ENDIF.

ENDFORM. " F_F4_DIR_APPL

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

11
9. F4 Search help for files on APPLICATION Server

To create a F4 search help for the Files present on the APPLICATION SERVER (i.e. Files on SAP system)
you can call the Function Module /SAPDMC/LSM_F4_SERVER_FILE. This method should be called under
the event: AT SELECTION-SCREEN ON VALUE-REQUEST. Following is a sample piece of code for your
reference where P_DIR_A is the input directory whose files you want to display as search help.

=====================================================================================

AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_file_a.


* Subroutine for F4 search help for Directory/Path on Application Server.
PERFORM f_f4_file_appl.

Below is the code for the subroutine F_F4_FILE_APPL:

*&---------------------------------------------------------------------*
*& Form F_F4_FILE_APPL
*&---------------------------------------------------------------------*
* For creating F4 search help for Files on Application Server
*----------------------------------------------------------------------*
FORM f_f4_file_appl .

* Local Dictionary
DATA : l_file TYPE rlgrap-filename,
l_len TYPE i.

CLEAR l_file.

IF p_dir_a IS NOT INITIAL.

CALL FUNCTION '/SAPDMC/LSM_F4_SERVER_FILE'


EXPORTING
directory = p_dir_a
filemask = ' '
IMPORTING
serverfile = l_file
EXCEPTIONS
canceled_by_user = 1
OTHERS = 2.

IF sy-subrc = 0.
l_len = STRLEN( p_dir_a ).
l_len = l_len + 1.
p_file_a = l_file+l_len(*).
ENDIF.

12
ELSE.
* Error message:'Si prega di selezionare Directory prima'(Please select
directory first).
MESSAGE S208(00) WITH text-004 DISPLAY LIKE 'E'.
ENDIF.

ENDFORM. " F_F4_FILE_APPL

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

13
10. FM for checking existence of a PATH/DIR on APPLICATION server

To check the existence of a PATH/ DIRECTORY on the Application server, entered by the user, you can
call the method PFL_CHECK_DIRECTORY. Following is a sample piece of code for your reference where
P_DIR_A is the input parameter containing the Path/ Directory name.

====================================================================================

* Subrouine for checking existence of Directory on Application Server.


PERFORM f_check_dir_appl.

Below is the code for the subroutine F_CHECK_DIR_APPL :

*&---------------------------------------------------------------------*
*& Form F_CHECK_DIR_APPL
*&---------------------------------------------------------------------*
* For checking input Directory's existence on Presentation server.
*----------------------------------------------------------------------*
FORM f_check_dir_appl .

CONSTANTS lc_e TYPE char01 VALUE 'E'.

CALL FUNCTION 'PFL_CHECK_DIRECTORY'


EXPORTING
directory_long = p_dir_a
EXCEPTIONS
pfl_dir_not_exist = 1
pfl_permission_denied = 2
pfl_cant_build_dataset_name = 3
pfl_file_not_exist = 4
OTHERS = 5.

IF sy-subrc NE 0.
* Error message:'Directory doesnot exist'.
MESSAGE e208(00) WITH text-003.
ENDIF.

ENDFORM. " F_CHECK_DIR_APPL

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

14
11. For checking the existence of a FILE on APPLICATION server

To check the existence of a FILE on the Application server following is a sample piece of code for your
reference where P_FILE_A is the input parameter containing the File Name and P_DIR_A contains the
Directory name.

=====================================================================================

* Subrouine for checking existence of File on Application Server.


PERFORM f_check_file_appl.

Below is the code for the subroutine F_CHECK_FILE_APPL:

*&---------------------------------------------------------------------*
*& Form F_CHECK_FILE_APPL
*&---------------------------------------------------------------------*
* For checking input File's existence on Presentation server.
*----------------------------------------------------------------------*
FORM f_check_file_appl .

DATA : l_file TYPE string,


l_len TYPE i.

CLEAR l_len.
l_len = STRLEN( p_dir_a ).
l_len = l_len - 1.

* Checking if at the end of the Directory '/' is present or not.


IF p_dir_p+l_len(1) NE '/'.
CLEAR l_file.
CONCATENATE p_dir_a
'/'
p_file_a
INTO l_file.
ELSE.

* Concatenating Directory and file name.


CLEAR l_file.
CONCATENATE p_dir_a
p_file_a
INTO l_file.
ENDIF.

OPEN DATASET l_file FOR INPUT IN TEXT MODE ENCODING DEFAULT.


IF sy-subrc NE 0.
* Error message:'File does not exist.'
MESSAGE e208(00) WITH text-005.

15
ELSE.
CLOSE DATASET l_file.
ENDIF.

ENDFORM. " F_CHECK_FILE_APPL

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

16
12. FM for checking the EXTENSION of an input file

To check the extension of an input file whether it is CSV or XLS etc., you can call the FM
TRINT_FILE_GET_EXTENSION. You can call this FM inside the event: AT SELECTION-SCREEN. Following is
a sample piece of code for your reference where P_FILE_P is the input parameter, containing the FILE
NAME with File Extension which is passed to the FM. Also ‘X’ is passed to the Exporting Parameter
UPPER CASE, to convert the extension of the input file to Upper case to avoid conflicts.

=====================================================================================

AT SELECTION-SCREEN.

* Subroutine for checking the Input File's Extension.


PERFORM f_check_file_ext.

Below is the code for the subroutine F_CHECK_FILE_EXT:

*&---------------------------------------------------------------------*
*& Form F_CHECK_FILE_EXT
*&---------------------------------------------------------------------*
* For checking the extension of the input file whether it is CSV or not.
*----------------------------------------------------------------------*
FORM f_check_file_ext.

DATA l_ext TYPE char3.


CONSTANTS : lc_csv TYPE char03 VALUE 'CSV',
lc_x TYPE char01 VALUE 'X'.

CLEAR l_ext.

CALL FUNCTION 'TRINT_FILE_GET_EXTENSION'


EXPORTING
filename = p_file_p
uppercase = lc_x "'X'
IMPORTING
extension = l_ext.

IF l_ext NE lc_csv. "CSV


* Displaying error message "ll formato del file di input non è csv.
MESSAGE e368(00) WITH text-e02.
ENDIF.

ENDFORM. ”F_CHECK_FILE_EXT

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

17

Вам также может понравиться