Thursday, September 18, 2014

ABAP TIP - Function module Test Data

ABAP TIP - Function module Test Data

It’s very trivial to input the test data in function module each test. As you already know, we can save the test data into test data dictionary by pressing save button.




There is another useful way to save test data for function module when function module is called from ABAP program. Let’s make a simple case how to save test data when calling some function module. After writing program, set the break point at the source where calling the function module.

 




When the program meet the debugging point, the debugger become activated. Move to next step by pressing ‘F5’.
 



If pressing the technical button in the debugger, there will be popup screen that can save test data for function module. Press Menu “Save Parameters as Test Data(SE37)”




Enter the title and save.





Moving to T-CODE:SE37 and check the test Data Directory, you can see the test data created.

 



Tuesday, September 16, 2014

ABAP TIP - External Command


ABAP TIP - External Command


You can register the UNIX command as a external command in sap system.
SAP 시스템에서 UNIX 서버의 외부 명령어를 등록하여, 실행할 있다.









1. Let’s execute T-CODE:SM49, SM69 and register external command.  Press ‘create’ icon.
2. If you want to register shell script, you should describe all directory path including shell script file. In case of UNIX command, you can write like below.
3. It’s also possible to use external command you created in T-CODE:SM36 and call external command in abap program like below example.


CONSTANTS: c_extcom    TYPE sxpgcolist-name VALUE 'ZCOMMAND,
           c_oper      TYPE syopsys VALUE 'UNIX'.

DATA: v_dir_input      TYPE sxpgcolist-parameters.  " Input Directory
DATA: t_result         TYPE STANDARD TABLE OF btcxpm.

v_dir_input = '/sapmmt'.

CALL FUNCTION 'SXPG_COMMAND_EXECUTE'
  EXPORTING
    commandname                   = c_extcom
    additional_parameters         = v_dir_input
    operatingsystem               = c_oper
  TABLES
    exec_protocol                 = t_result
  EXCEPTIONS
    no_permission                 = 1
    command_not_found             = 2
    parameters_too_long           = 3
    security_risk                 = 4
    wrong_check_call_interface    = 5
    program_start_error           = 6
    program_termination_error     = 7
    x_error                       = 8
    parameter_expected            = 9
    too_many_parameters           = 10
    illegal_command               = 11
    wrong_asynchronous_parameters = 12
    cant_enq_tbtco_entry          = 13
    jobcount_generation_error     = 14
    OTHERS                        = 15.




Monday, September 15, 2014

ABAP TIP : FILE management with CBO program

ABAP TIP : FILE management with CBO program

Let’s make a program that can upload and download file.
User can select file and upload.






Generally, we use the ‘ASC’ mode when calling function ‘GUI_UPLOAD/GUI_DOWNLOAD’.

 CALL FUNCTION 'GUI_UPLOAD'
    
EXPORTING
      filename                
gv_fname
      filetype                'ASC'


This program aims to upload/donwload file, so we must use the ‘BIN’ mode when calling function.
If we save the file as a BIN mode, it’s possible to download as a origin file.

 CALL FUNCTION 'GUI_UPLOAD'
    
EXPORTING
      filename                
gv_fname
      filetype                'BIN'

Fisrt of all, let’s create 2 table for saving header and contents of file.







And, Let’s write program and activate.

REPORT  zfile.

 
TABLES : zfilehead, zfiledata, sscrfields.
 
DATA : gs_fhead  LIKE zfilehead,
        gt_fdata  
LIKE zfiledata OCCURS WITH HEADER LINE.

*attache file
DATA : BEGIN OF gt_uptab OCCURS 0,
         content
(255TYPE x,
       
END OF gt_uptab.

DATA :  gv_length  TYPE int4,
        gv_fname   
TYPE string,
        gv_fname2  
TYPE string,
        gv_ftype   
TYPE zfilehead-filetype,
        gv_temp
(120).


DATA:   gv_work_dir TYPE string,
        gv_cmd
(128).

PARAMETERS : p_carrid TYPE s_carr_id OBLIGATORY,
             p_fname 
TYPE zfilehead-filename.
SELECTION-SCREEN FUNCTION KEY 1.

INITIALIZATION.
    sscrfields
-functxt_01 'Display file'.

AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_fname.

DATA:   l_rc                TYPE i VALUE 1,
        l_window_title      
TYPE string,
        lt_file_table       
TYPE filetable,
        ls_file             
TYPE file_table.

CALL METHOD cl_gui_frontend_services=>file_open_dialog
    
EXPORTING
      window_title            
'Select file'
      file_filter             
'*.*'
      multiselection          
space
    
CHANGING
      file_table              
lt_file_table
      rc                      
l_rc.
      
IF sy-subrc EQ 0.
      
READ TABLE lt_file_table INDEX INTO ls_file.
      p_fname 
ls_file-filename.
      
ENDIF.

 
CLEAR: gt_uptab, gt_uptab[].
 gv_fname 
p_fname.
 
CALL FUNCTION 'GUI_UPLOAD'
    
EXPORTING
      filename                
gv_fname
      filetype                
'BIN'
    
IMPORTING
      filelength              
gv_length
    
TABLES
      data_tab                
gt_uptab.

START-OF-SELECTION.

*& file name
  
CLEAR gv_temp.
  
DO.
    
SPLIT gv_fname AT '\' INTO gv_temp gv_fname.
    
IF gv_fname EQ space.
      gv_fname 
gv_temp.
      
EXIT.
    
ENDIF.
    
IF sy-index 100. EXIT. ENDIF.
  
ENDDO.

*& file type
  
CLEAR gv_temp.
  gv_fname2 
gv_fname.
  
DO.
    
SPLIT gv_fname2 AT '.' INTO gv_temp gv_fname2.
    
IF gv_fname2 EQ space.
      gv_fname2 
gv_temp.
      
EXIT.
    
ENDIF.
    
IF sy-index 100. EXIT. ENDIF.
  
ENDDO.

  gv_ftype 
gv_fname2+0(3).
  
TRANSLATE gv_ftype TO UPPER CASE.
*& Header
  gs_fhead
-carrid   p_carrid.
  gs_fhead
-filename gv_fname.
  gs_fhead
-filetype gv_ftype.
  gs_fhead
-length   gv_length.
  
MODIFY zfilehead FROM gs_fhead.

*& Data.
  
LOOP AT gt_uptab.
    gt_fdata
-carrid p_carrid.
    gt_fdata
-content gt_uptab-content.
    gt_fdata
-seq sy-tabix.
    
APPEND gt_fdata.
  
ENDLOOP.
  
MODIFY zfiledata FROM TABLE gt_fdata.
  
IF sy-subrc EQ 0.
     
MESSAGE 'SUCCESS' TYPE 'S'.
  
ELSE.
     
MESSAGE 'ERROR' TYPE 'E'.
  
ENDIF.


  
AT SELECTION-SCREEN.
    
CHECK sscrfields-ucomm 'FC01'.
CLEAR : gs_fhead, gt_fdata, gt_fdata[].

*& Header
  
SELECT SINGLE INTO gs_fhead FROM zfilehead
                 
WHERE carrid p_carrid.
  
IF sy-subrc NE 0.
         
MESSAGE 'There is no file' TYPE 'E'.
  
ENDIF.

*& detail
  
SELECT INTO CORRESPONDING FIELDS OF TABLE gt_fdata
           
FROM zfiledata
           
WHERE carrid p_carrid.

  
CLEAR: gt_uptab, gt_uptab[].

  
SORT gt_fdata BY seq.
  
LOOP AT gt_fdata.
    gt_uptab
-content gt_fdata-content.
    
APPEND gt_uptab.
  
ENDLOOP.

* Get SAP Working Directory.
  
CALL METHOD cl_gui_frontend_services=>get_sapgui_workdir
    
CHANGING
      sapworkdir 
gv_work_dir.

*& file name
  
CONCATENATE gv_work_dir '\' gs_fhead-filename INTO gv_fname.

*& Download
  
CALL FUNCTION 'GUI_DOWNLOAD'
    
EXPORTING
      bin_filesize            
gs_fhead-length
      filename                
gv_fname
      filetype                
'BIN'
    
TABLES
      data_tab                
gt_uptab.

*& file run
  gv_cmd 
gv_fname.
  
CALL FUNCTION 'GUI_RUN'
    
EXPORTING
      command          
gv_cmd.

Select test image file and execute.



After uploading, press the button ‘display file’ and you can see the file uploaded.




ABAP TIP - Function of‘&’ symbol


ABAP TIP - Function of& symbol

& symbol has a meaning of parameter. Representative examples for & is when passing the parameter to MACRO like below program.

REPORT  z01_01.

DATA: gv_val1 TYPE c VALUE 'A',
       gv_val2 TYPE c VALUE 'B',
       gv_val3 TYPE char3.

DEFINE con.
  CONCATENATE &1 &2 INTO &3 SEPARATED BY space.
  dis &3.
END-OF-DEFINITION.

DEFINE dis.
  WRITE &1.
END-OF-DEFINITION.

con gv_val1 gv_val2 gv_val3.

& symbol also is useful when assigning the title of screen.






If you declare the & in title of screen, you can define the screen title with the below command.

  SET TITLEBAR 'T0001' WITH 'Title Test'.

 



Definitely, you can declare 2 parameter with &, the title of screen is named like below command.

  
SET TITLEBAR 'T0001' WITH 'Title' 'Test'.


 



ABAP tip : Download Spool Data


Download Spool Data


When the program is executed in background mode, the result written by WRITE statement is saved into spool. In order to check the result, we use the spool button via T-CODE:SM37 to display the spool information. At this time, there is a restriction that its only possible to display spool within 10 pages. So, in case of lots of result lists, its very trivial to display the results and its sometimes impossible to do.







To display next 01 page, you should press the settings button, and enter the page number from value and to value.
 



If you download the list at this screen, only 10 pages displayed are downloaded. Its very difficult to download all data.

We usually use the below 2 methods to download spool data.
1. T-CODE:SP01 and download.
2. T-CODE:SE37 : function RSPO_DOWNLOAD_SPOOLJOB.

1. T-CODE:SP01 download
By using the spool number with SM37, you can download the spool into file. Enter the spool number and execute.
 



Press Menu : Forward -> Export as text
 



Once download is done, the file information is displayed on a status bar with the name of file and folder.
 



You can see the file in the folder.
 



2. T-CODE:SE37 and function RSPO_DOWNLOAD_SPOOLJOB

You can also download the spool by using function RSPO_DOWNLOAD_SPOOLJOB in T-CODE:SE37.
Enter the spool number in ID parameter and input the file name and folder for the rest of parameter.

 



There is a file downloaded in a destination folder.