segunda-feira, 22 de março de 2010

Upload txt/excel files to internal table (example)

DATA: file_path TYPE string.
DATA: len TYPE i.

file_path = l_file.
TRANSLATE file_path TO UPPER CASE.
len = STRLEN( file_path ) - 3.

CLEAR tabi.

IF file_path+len(3) = 'XLS'.
break slopes.

DATA t_excel TYPE alsmex_tabline OCCURS 0 WITH HEADER LINE.

CALL FUNCTION 'ALSM_EXCEL_TO_INTERNAL_TABLE'
EXPORTING
filename = l_file
i_begin_col = 1
i_begin_row = 1
i_end_col = 2
i_end_row = 50
TABLES
intern = t_excel
EXCEPTIONS
inconsistent_parameters = 1
upload_ole = 2
OTHERS = 3.


LOOP AT t_excel.
CASE t_excel-col.
WHEN 1.
s_tabi-matnr = t_excel-value.
WHEN 2.
s_tabi-erfmg = t_excel-value.
ENDCASE.
AT END OF row.
APPEND s_tabi to tabi.
CLEAR s_tabi.
ENDAT.
ENDLOOP.


ELSE.




CALL METHOD cl_gui_frontend_services=>gui_upload
EXPORTING
filename = file_path
filetype = 'ASC'
has_field_separator = 'X'
CHANGING
data_tab = tabi.
ENDIF.

sexta-feira, 19 de março de 2010

How to download/upload files

*&---------------------------------------------------------------------*
*& Report ZSL_TRANSFERENCIAS
*&
*&---------------------------------------------------------------------*
*&
*&
*&---------------------------------------------------------------------*

REPORT zsl_transferencias.

TABLES: mara, makt.

DATA: BEGIN OF s_tabi,
matnr LIKE mara-matnr,
ERSDA like mara-ERSDA,
maktx LIKE makt-maktx,
END OF s_tabi,

tabi LIKE s_tabi OCCURS 0 WITH HEADER LINE.

SELECTION-SCREEN: BEGIN OF BLOCK block1 WITH FRAME TITLE text-001.
PARAMETERS: upload RADIOBUTTON GROUP 1,
download RADIOBUTTON GROUP 1.

SELECTION-SCREEN:END OF BLOCK block1.

SELECTION-SCREEN: BEGIN OF BLOCK block2 WITH FRAME TITLE text-002.
PARAMETERS: f_server(132) DEFAULT 'TESTE_SLOPES',
f_client LIKE RLGRAP-FILENAME default 'C:\TESTE\TESTE.TXT'.
SELECTION-SCREEN:END OF BLOCK block2.


*&---------------------------------------------------------------------*
AT SELECTION-SCREEN ON VALUE-REQUEST FOR F_CLIENT.
*&---------------------------------------------------------------------*

PERFORM F4_FILE_PC.

*&---------------------------------------------------------------------*
AT SELECTION-SCREEN ON VALUE-REQUEST FOR F_SERVER.
*&---------------------------------------------------------------------*

PERFORM F4_FILE_SERVER.


*&---------------------------------------------------------------------*
START-OF-SELECTION.
*&---------------------------------------------------------------------*

*clear tabi.
*refresh tabi.

IF upload = 'X'.

* bastava select a MAKT
SELECT a~matnr a~ERSDA
t~maktx
FROM mara AS a
INNER JOIN makt AS t
ON a~matnr = t~matnr
INTO TABLE tabi.

*copia para o pc

PERFORM local_save USING f_client.

*copia para o servidor

OPEN DATASET f_server FOR OUTPUT IN TEXT MODE ENCODING DEFAULT.

LOOP AT tabi.
TRANSFER tabi TO f_server.
ENDLOOP.

CLOSE DATASET f_server.

ELSE.

*preenche tabi

OPEN DATASET f_server FOR INPUT IN TEXT MODE ENCODING DEFAULT.

DO.
READ DATASET f_server INTO tabi.
IF sy-subrc NE 0.
EXIT.
ENDIF.
APPEND tabi.
ENDDO.

*escreve no pc

PERFORM local_save USING f_client.


ENDIF.
*&---------------------------------------------------------------------*
*& Form local_save
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* -->file_name text
*----------------------------------------------------------------------*
FORM local_save USING file_name.


DATA: L_FILE TYPE STRING.
L_FILE = FILE_NAME.

CALL FUNCTION 'GUI_DOWNLOAD'
EXPORTING
filename = L_FILE
tables
data_tab = tabi.



ENDFORM. " loc
*&---------------------------------------------------------------------*
*& Form F4_FILE_PC
*&---------------------------------------------------------------------*
form F4_FILE_PC .

CALL FUNCTION 'F4_FILENAME'
EXPORTING
PROGRAM_NAME = SYST-CPROG
DYNPRO_NUMBER = SYST-DYNNR
FIELD_NAME = 'F_CLIENT'
IMPORTING
FILE_NAME = F_CLIENT .


endform. " F4_FILE_
*&---------------------------------------------------------------------*
*& Form F4_FILE_SERVER
*&---------------------------------------------------------------------*

form F4_FILE_SERVER .

DATA: l_location LIKE dxfields-location,
l_server LIKE msxxlist-name,
l_path LIKE dxfields-longpath,
l_flag LIKE dxfields-abendflag.

CALL FUNCTION 'F4_DXFILENAME_TOPRECURSION'
EXPORTING
I_LOCATION_FLAG = 'A'
* I_SERVER = '?'
* I_PATH =
* FILEMASK = '*.*'
* FILEOPERATION = 'R'
IMPORTING
o_location_flag = l_location
o_server = l_server
o_path = l_path
abend_flag = l_flag
EXCEPTIONS
RFC_ERROR = 1
ERROR_WITH_GUI = 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.

MOVE l_path TO f_server.

endform. " F4_FILE_S

How to create a dialog to go to a local/server directory and save/read a file

this form opens a dialog to select the directory where you want to save/read a local file


*&---------------------------------------------------------------------*
*& Form f4_save_lfile
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* <--P_P_LFILE text *----------------------------------------------------------------------* FORM f4_save_lfile CHANGING p_file. DATA: l_filename TYPE string, l_path TYPE string, l_fullpath TYPE string. CALL METHOD cl_gui_frontend_services=>file_save_dialog
EXPORTING
* window_title =
default_extension = 'TXT'
* default_file_name =
* with_encoding =
* file_filter =
* initial_directory =
prompt_on_overwrite = 'X'
CHANGING
filename = l_filename
path = l_path
fullpath = l_fullpath
* user_action =
* file_encoding =
EXCEPTIONS
cntl_error = 1
error_no_gui = 2
not_supported_by_gui = 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.

p_file = l_fullpath.

ENDFORM. " f4_save_lfile

To do the same but on a server file, you should use this:

*&---------------------------------------------------------------------*
*& Form f4_rfile
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* <--P_P_RFILE text *----------------------------------------------------------------------* FORM f4_rfile CHANGING p_rfile. *" VALUE(I_LOCATION_FLAG) LIKE DXFIELDS-LOCATION DEFAULT ' ' *" VALUE(I_SERVER) LIKE MSXXLIST-NAME DEFAULT '?' *" VALUE(I_PATH) LIKE DXFIELDS-LONGPATH OPTIONAL *" VALUE(FILEMASK) LIKE DXFIELDS-FILEMASK DEFAULT '*.*' *" VALUE(FILEOPERATION) LIKE DXFIELDS-FILEOPER DEFAULT 'R' *" EXPORTING DATA: l_location LIKE dxfields-location, l_server LIKE msxxlist-name, l_path LIKE dxfields-longpath, l_flag LIKE dxfields-abendflag. CALL FUNCTION 'F4_DXFILENAME_TOPRECURSION' EXPORTING i_location_flag = 'A' * I_SERVER = '?' * I_PATH = * FILEMASK = '*.*' * FILEOPERATION = 'R' IMPORTING o_location_flag = l_location o_server = l_server o_path = l_path abend_flag = l_flag EXCEPTIONS rfc_error = 1 error_with_gui = 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.

MOVE l_path TO p_rfile.

ENDFORM. " f4_rfile

quarta-feira, 17 de março de 2010

How to sum all entries with the same key

Just use the COLLECT function it sums all entries with the same key, or add a new entrie if the key doesnt exist yet


...
MOVE-CORRESPONDING t_vbrp TO tab_pesos.
COLLECT tab_pesos.
...

segunda-feira, 15 de março de 2010

How to create a Form using Sapscript

First you need to create your form in transaction se71.

and second create an executable report like this:

*&---------------------------------------------------------------------*
*& Report ZSL_EX_FORM
*&
*&---------------------------------------------------------------------*
*&
*&
*&---------------------------------------------------------------------*

REPORT zsl_ex_form.

TABLES: zslproj, zslaloc, zslcli, zslcons.

DATA: s_zslproj LIKE zslproj,
t_zslaloc LIKE zslaloc OCCURS 0 WITH HEADER LINE,
s_zslcli LIKE zslcli,
s_zslcons like zslcons.


SELECTION-SCREEN: BEGIN OF BLOCK myblock WITH FRAME TITLE text-001.
PARAMETERS: proj LIKE zslproj-zcod_proj.
SELECTION-SCREEN:END OF BLOCK myblock.

SELECT SINGLE * FROM zslproj INTO s_zslproj WHERE zcod_proj = proj.
SELECT * FROM zslaloc INTO TABLE t_zslaloc WHERE zcod_proj = proj.
SELECT SINGLE * FROM zslcli INTO s_zslcli
WHERE zcod_cli = s_zslproj-zcod_cli.

CALL FUNCTION 'OPEN_FORM'
EXPORTING
form = 'ZSL_EX_FORM'.


CALL FUNCTION 'START_FORM'
EXPORTING
* ARCHIVE_INDEX =
form = 'ZSL_EX_FORM'
language = sy-langu
startpage = 'FIRST'
program = sy-repid.


CALL FUNCTION 'WRITE_FORM'
EXPORTING
element = 'HEAD'
* FUNCTION = 'SET'
* TYPE = 'BODY'
window = 'MAIN'.

loop at t_zslaloc.
SELECT SINGLE * FROM zslcons INTO s_zslcons
WHERE zcod_cons = t_zslaloc-zcod_cons.

CALL FUNCTION 'WRITE_FORM'
EXPORTING
element = 'TABELA'
* FUNCTION = 'SET'
* TYPE = 'BODY'
window = 'MAIN'.
endloop.


CALL FUNCTION 'WRITE_FORM'
EXPORTING
* element = 'TOPRIGHT'
* FUNCTION = 'SET'
* TYPE = 'BODY'
window = 'LOGO'.

CALL FUNCTION 'WRITE_FORM'
EXPORTING
element = 'TOPRIGHT'
* FUNCTION = 'SET'
* TYPE = 'BODY'
window = 'TOPRIGHT'.

CALL FUNCTION 'WRITE_FORM'
EXPORTING
* element = 'TOPRIGHT'
* FUNCTION = 'SET'
* TYPE = 'BODY'
window = 'MIDLE'.

CALL FUNCTION 'WRITE_FORM'
EXPORTING
element = 'FOOTER'
* FUNCTION = 'SET'
* TYPE = 'BODY'
window = 'FOOTER'

.
IF sy-subrc <> 0.
* MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
* WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
ENDIF.





*
* CALL FUNCTION 'WRITE_FORM'
* EXPORTING
* element = 'ADDRESS'
** FUNCTION = 'SET'
** TYPE = 'BODY'
* window = 'WINDOW1'
** IMPORTING
** PENDING_LINES =
** EXCEPTIONS
** ELEMENT = 1
** FUNCTION = 2
** TYPE = 3
** UNOPENED = 4
** UNSTARTED = 5
** WINDOW = 6
** BAD_PAGEFORMAT_FOR_PRINT = 7
** SPOOL_ERROR = 8
** CODEPAGE = 9
** OTHERS = 10
* .
* IF sy-subrc <> 0.
** MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
** WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
* ENDIF.
*
*
*LOOP AT T_VBAP.
* CALL FUNCTION 'WRITE_FORM'
* EXPORTING
* element = 'ELEMENTO'
** FUNCTION = 'SET'
** TYPE = 'BODY'
* window = 'MAIN'
** IMPORTING
** PENDING_LINES =
** EXCEPTIONS
** ELEMENT = 1
** FUNCTION = 2
** TYPE = 3
** UNOPENED = 4
** UNSTARTED = 5
** WINDOW = 6
** BAD_PAGEFORMAT_FOR_PRINT = 7
** SPOOL_ERROR = 8
** CODEPAGE = 9
** OTHERS = 10
* .
* IF sy-subrc <> 0.
** MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
** WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
* ENDIF.
*
*
*ENDLOOP.



CALL FUNCTION 'END_FORM'.

CALL FUNCTION 'CLOSE_FORM'.

quarta-feira, 10 de março de 2010

How to use ALV (example 2)

*&---------------------------------------------------------------------*
*& Report ZSL_LISTAGENS
*&
*&---------------------------------------------------------------------*
*&
*&
*&---------------------------------------------------------------------*

REPORT zsl_listagens.

TYPE-POOLS: slis.

TABLES: zslaloc, zslproj, zslcli.

DATA: BEGIN OF t_lista OCCURS 0,
zcod_proj LIKE zslaloc-zcod_proj,
zdesc_proj LIKE zslproj-zdesc_proj,
zcod_cli LIKE zslcli-zcod_cli,
znom_cli LIKE zslcli-znom_cli,
zcod_cons LIKE zslaloc-zcod_cons,
znom_cons LIKE zslcons-znom_cons,
zinicio_aloc LIKE zslaloc-zinicio_aloc,
zfim_aloc LIKE zslaloc-zfim_aloc,
END OF t_lista.

*DATA: t_lista LIKE s_lista OCCURS 0 WITH HEADER LINE.

SELECTION-SCREEN: BEGIN OF BLOCK myblock WITH FRAME TITLE text-001.

SELECT-OPTIONS: s_proj FOR zslaloc-zcod_proj,
s_cli FOR zslcli-zcod_cli,
s_cons FOR zslaloc-zcod_cons,
s_dati FOR zslaloc-zinicio_aloc.

SELECTION-SCREEN:END OF BLOCK myblock.

*&---------------------------------------------------------------------*
START-OF-SELECTION.
*&---------------------------------------------------------------------*
SELECT a~zcod_proj p~zdesc_proj p~zcod_cli cl~znom_cli
a~zcod_cons co~znom_cons a~zinicio_aloc a~zfim_aloc
FROM zslaloc AS a
INNER JOIN zslcons AS co ON a~zcod_cons = co~zcod_cons
INNER JOIN zslproj AS p ON a~zcod_proj = p~zcod_proj
INNER JOIN zslcli AS cl ON p~zcod_cli = cl~zcod_cli
INTO CORRESPONDING FIELDS OF TABLE t_lista
WHERE a~zcod_proj IN s_proj AND p~zcod_cli IN s_cli AND
a~zcod_cons IN s_cons AND a~zinicio_aloc IN s_dati.
*&---------------------------------------------------------------------*
END-OF-SELECTION.
*&---------------------------------------------------------------------*

IF t_lista[] IS INITIAL.
MESSAGE i003(zsl_m) WITH 'Não ha alocações com '
'estes critérios de selecção'.
ELSE.

PERFORM write_data.

ENDIF.
*&---------------------------------------------------------------------*
*& Form write_data
*&---------------------------------------------------------------------*
FORM write_data .

DATA: lt_fields TYPE slis_t_fieldcat_alv,
wa_fields TYPE slis_fieldcat_alv,
wa_layout TYPE slis_layout_alv.

CLEAR: lt_fields.
* wa_fields-col_pos = 1.
* wa_fields-fieldname = 'ZCOD_PROJ'.
* WA_FIELDS-seltext_m = 'Código Projecto'.
** wa_fields-ddictxt = 'M'.
* append wa_fields to lt_fields.
*
*clear: wa_fields.
* wa_fields-col_pos = 2.
* wa_fields-fieldname = 'ZCOD_CLI'.
* WA_FIELDS-seltext_m = 'Código Cliente'.
** wa_fields-ddictxt = 'M'.
* append wa_fields to lt_fields.


CALL FUNCTION 'REUSE_ALV_FIELDCATALOG_MERGE'
EXPORTING
i_program_name = sy-repid
i_internal_tabname = 'T_LISTA'
* i_structure_name = 'S_LISTA'
i_inclname = sy-repid
CHANGING
ct_fieldcat = lt_fields
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.




wa_layout-colwidth_optimize = 'X'.
wa_layout-edit = 'X'.
wa_layout-key_hotspot = 'X'."faz com que as chavez sejam clicaveis
*ficam com o fcode '&IC1'

CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
EXPORTING
i_callback_program = sy-repid
i_callback_pf_status_set = 'SET_PF_STATUS'
i_callback_user_command = 'USER_COMMAND'
is_layout = wa_layout
it_fieldcat = lt_fields
TABLES
t_outtab = t_lista
* EXCEPTIONS
* PROGRAM_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.




ENDFORM. " write




*&--------------------------------------------------------------------*
*& Form set_pf_status
*&--------------------------------------------------------------------*
* text
*---------------------------------------------------------------------*
* -->RT_EXTAB text
*---------------------------------------------------------------------*
FORM set_pf_status USING rt_extab TYPE slis_t_extab.
SET PF-STATUS 'STAT100'.
ENDFORM. "set_pf_sta

*&--------------------------------------------------------------------*
*& Form user_comand
*&--------------------------------------------------------------------*
FORM user_command USING r_ucomm LIKE sy-ucomm
rs_selfield TYPE slis_selfield.

rs_selfield-refresh = 'X'.


CASE r_ucomm.
WHEN 'BACK1'.

SET SCREEN 0.
LEAVE SCREEN.
* leave to selection-screen 1000.
WHEN 'ALOC'.
WHEN '&IC1'.
IF rs_selfield-fieldname = 'ZCOD_PROJ'.
SET PARAMETER ID 'ZPROJ' FIELD rs_selfield-value.
CALL TRANSACTION 'ZSL_PROJECT03' AND SKIP FIRST SCREEN.
ELSEIF rs_selfield-fieldname = 'ZCOD_CLI'.
SET PARAMETER ID 'ZCLI' FIELD rs_selfield-value.
CALL TRANSACTION 'ZSL_CLIENTES03' AND SKIP FIRST SCREEN.
ELSEIF rs_selfield-fieldname = 'ZCOD_CONS'.
SET PARAMETER ID 'ZCONS' FIELD rs_selfield-value.
CALL TRANSACTION 'ZSL_CONS03' AND SKIP FIRST SCREEN.
ENDIF.
WHEN OTHERS.
MESSAGE s003(zsl_m) WITH rs_selfield-sel_tab_field
rs_selfield-col_stable
rs_selfield-row_stable
rs_selfield-fieldname.



ENDCASE.


ENDFORM. "user_co

How to refresh an internal table using ALV OO

Its really simple, all you have to do is register the enter and modified event before calling the method set_table_for_first_display( ).

or without registing those events just do this: CALL METHOD go_grid->check_changed_data

code:


CALL METHOD go_grid->register_edit_event
EXPORTING
i_event_id = cl_gui_alv_grid=>mc_evt_modified
EXCEPTIONS
error = 1
OTHERS = 2.

CALL METHOD go_grid->register_edit_event
EXPORTING
i_event_id = cl_gui_alv_grid=>mc_evt_enter
EXCEPTIONS
error = 1
OTHERS = 2.

p.s.: To refresh the ALV OO go_grid->refresh_table_display

How to use Events in an ABAP/4 Report Program


There is no mandatory event in a report program. Events are actually used to organize the statements and control the flow of the program. If the program contains no explicitly defined event blocks, all the statements in the program form the entire event block START-OF-SELECTION. ABAP runtime environment automatically introduces a START-OF-SELECTION at the first execution line.

ABAP/4 report programs are event driven programs. Following events are triggered in a report.

LOAD OF PROGRAM. - If propgram is of type 1, M, F, or S; then LOAD-OF-PROGRAM is trigerred first and then INITIALIZATION. The purpose of load of program is to load the program into system memory so that the program can be executed.

INITIALIZATION. - just once.

AT SELECTION-SCREEN OUTPUT. - when there is selection-screen

AT SELECTION-SCREEN. - when user interacts.

START-OF-SELECTION. - when F8.This is the default event in ABAP.

END-OF-SELECTION. - At the end

EVENTS IN A CLASSICAL REPORT:

1. INITIALIZATION: This is triggered when the report is loaded in memory.

2. AT SELECTION-SCREEN OUTPUT: This is triggered when the selection screen is loaded in memory before being displayed.

3. AT SELECTION-SCREEN/ : This event is triggered before leaving the selection screen.

4. START-OF-SELECTION: This is the the first and default event for displaying the report. Data retrieval logic is written under this event.

OUTPUT EVENT(5, 6):
5. TOP-OF-PAGE: This event is triggered every time a new page is started in the list.

6. END-OF-PAGE : This event is triggered every time the list data reaches the footer region of the page.

7. END-OF-SELECTION: This event is triggered after the START-OF-SELECTION is completed.
This event is used when there is use of logical data base in the report. It is triggered in type 1 programs once the logical database completes reading all data i.e. all the selection is finished and before the list processor is started. This statement tells the server that all the database reading is completed and no more data reading is going to take place. END-OF-SELECTION is generally used for the summary/results of reports. In an executable program without logical data base, there is no need to implement the event block END-OF-SELECTION.
After a program is executed by the user, the database is locked when it encounters a START-OF-SELECTION statement and the lock is released when an END-OF-SELECTION statement is encountered (to maintain the consistency). Data selection is done between START-OF-SELECTION and END-OF-SELECTION. Now if we don’t write the END-OF-SELECTION statement, the database will remain locked till the programs get terminated. Hence, the performance will be reduced. So it is always good practice to write END-OF-SELECTION statement after finishing the data selection from database.

EVENTS IN AN INTERACTIVE REPORT:

In addition of the above, the following events are possible in case of Interactive Reports.

1. Top of page during line selection : top of page event for secondary list.

2. At line-selection : evey time user dbl-clicks(F2) on the list data.

3. At pF : function key from F5 to F12 to perform interactive action on the list.

4. At user-command.

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

Finally I want to include one point:
If you debug your report program from the Abap editor screen then it triggers from
1.Initialization,
2.At Selection-screen,
3.Start-of Selection,
4.End-of Selection,
5.Top-of-page,
6.End-of-page.

But if you debug from the selection-screen then it triggers from
1.At Selection-Screen,
2.Start-of-selection,
3.Top-of-page,
4.End-of-page.

terça-feira, 9 de março de 2010

How to use ALV with objects (example)

PROGRAM TEST.
DATA: OK_CODE LIKE SY-UCOMM,
GT_SFLIGHT TYPE TABLE OF SFLIGHT,
G_CONTAINER TYPE SCRFNAME VALUE 'BCALV_GRID_DEMO_0100_CONT1',
GRID1 TYPE REF TO CL_GUI_ALV_GRID,
G_CUSTOM_CONTAINER TYPE REF TO CL_GUI_CUSTOM_CONTAINER.
*---------------------------------------------------------------------*
* MAIN *
*---------------------------------------------------------------------*

SELECT * FROM SFLIGHT INTO TABLE GT_SFLIGHT.

CALL SCREEN 100.


*---------------------------------------------------------------------*
* MODULE PBO OUTPUT *
*---------------------------------------------------------------------*
MODULE PBO OUTPUT.
SET PF-STATUS 'MAIN100'.

IF G_CUSTOM_CONTAINER IS INITIAL.

CREATE OBJECT G_CUSTOM_CONTAINER
EXPORTING CONTAINER_NAME = G_CONTAINER.


CREATE OBJECT GRID1
EXPORTING I_PARENT = G_CUSTOM_CONTAINER.

CALL METHOD GRID1->SET_TABLE_FOR_FIRST_DISPLAY
EXPORTING I_STRUCTURE_NAME = 'SFLIGHT'
CHANGING IT_OUTTAB = GT_SFLIGHT.

ELSE.
CALL METHOD GRID1->refresh_table_display.
ENDIF.

ENDMODULE.
*---------------------------------------------------------------------*
* MODULE PAI INPUT *
*---------------------------------------------------------------------*
MODULE PAI INPUT.
* to react on oi_custom_events:
call method cl_gui_cfw=>dispatch.
CASE OK_CODE.
WHEN 'EXIT'.
PERFORM EXIT_PROGRAM.
WHEN OTHERS.
* do nothing
ENDCASE.
CLEAR OK_CODE.
ENDMODULE.
*---------------------------------------------------------------------*
* FORM EXIT_PROGRAM *
*---------------------------------------------------------------------*
FORM EXIT_PROGRAM.
* CALL METHOD G_CUSTOM_CONTAINER->FREE.
* CALL METHOD CL_GUI_CFW=>FLUSH.
LEAVE PROGRAM.
ENDFORm.

How to use ALV (example)

*&---------------------------------------------------------------------*
*& Report ZSL_LISTAGENS
*&
*&---------------------------------------------------------------------*
*&
*&
*&---------------------------------------------------------------------*

REPORT zsl_listagens.

TYPE-POOLS: slis.

TABLES: zslaloc, zslproj, zslcli.

DATA: BEGIN OF t_lista OCCURS 0,
zcod_proj LIKE zslaloc-zcod_proj,
zdesc_proj LIKE zslproj-zdesc_proj,
zcod_cli LIKE zslcli-zcod_cli,
znom_cli LIKE zslcli-znom_cli,
zcod_cons LIKE zslcons-zcod_cons,
znom_cons LIKE zslcons-znom_cons,
zinicio_aloc LIKE zslaloc-zinicio_aloc,
zfim_aloc LIKE zslaloc-zfim_aloc,
END OF t_lista.

*DATA: t_lista LIKE s_lista OCCURS 0 WITH HEADER LINE.

SELECTION-SCREEN: BEGIN OF BLOCK myblock WITH FRAME TITLE text-001.

SELECT-OPTIONS: s_proj FOR zslaloc-zcod_proj,
s_cli FOR zslcli-zcod_cli,
s_cons FOR zslaloc-zcod_cons,
s_dati FOR zslaloc-zinicio_aloc.

SELECTION-SCREEN:END OF BLOCK myblock.

*&---------------------------------------------------------------------*
START-OF-SELECTION.
*&---------------------------------------------------------------------*
SELECT a~zcod_proj p~zdesc_proj p~zcod_cli cl~znom_cli
a~zcod_cons co~znom_cons a~zinicio_aloc a~zfim_aloc
FROM zslaloc AS a
INNER JOIN zslcons AS co ON a~zcod_cons = co~zcod_cons
INNER JOIN zslproj AS p ON a~zcod_proj = p~zcod_proj
INNER JOIN zslcli AS cl ON p~zcod_cli = cl~zcod_cli
INTO CORRESPONDING FIELDS OF TABLE t_lista
WHERE a~zcod_proj IN s_proj AND p~zcod_cli IN s_cli AND
a~zcod_cons IN s_cons AND a~zinicio_aloc IN s_dati.
*&---------------------------------------------------------------------*
END-OF-SELECTION.
*&---------------------------------------------------------------------*

IF t_lista[] IS INITIAL.
MESSAGE i003(zsl_m) WITH 'Não ha alocações com '
'estes critérios de selecção'.
ELSE.

PERFORM write_data.

ENDIF.
*&---------------------------------------------------------------------*
*& Form write_data
*&---------------------------------------------------------------------*
FORM write_data .

DATA: lt_fields TYPE slis_t_fieldcat_alv,
wa_fields TYPE slis_fieldcat_alv,
wa_layout TYPE slis_layout_alv.

CLEAR: lt_fields.
* wa_fields-col_pos = 1.
* wa_fields-fieldname = 'ZCOD_PROJ'.
* WA_FIELDS-seltext_m = 'Código Projecto'.
** wa_fields-ddictxt = 'M'.
* append wa_fields to lt_fields.
*
*clear: wa_fields.
* wa_fields-col_pos = 2.
* wa_fields-fieldname = 'ZCOD_CLI'.
* WA_FIELDS-seltext_m = 'Código Cliente'.
** wa_fields-ddictxt = 'M'.
* append wa_fields to lt_fields.


CALL FUNCTION 'REUSE_ALV_FIELDCATALOG_MERGE'
EXPORTING
i_program_name = sy-repid
i_internal_tabname = 'T_LISTA'
* i_structure_name = 'S_LISTA'
i_inclname = sy-repid
CHANGING
ct_fieldcat = lt_fields
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.

wa_layout-colwidth_optimize = 'X'.

CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
EXPORTING
* I_INTERFACE_CHECK = ' '
* I_BYPASSING_BUFFER = ' '
* I_BUFFER_ACTIVE = ' '
i_callback_program = sy-repid
i_callback_pf_status_set = 'SET_PF_STATUS'
i_callback_user_command = 'USER_COMMAND'
* I_CALLBACK_TOP_OF_PAGE = ' '
* I_CALLBACK_HTML_TOP_OF_PAGE = ' '
* I_CALLBACK_HTML_END_OF_LIST = ' '
* I_STRUCTURE_NAME =
* I_BACKGROUND_ID = ' '
* I_GRID_TITLE =
* I_GRID_SETTINGS =
is_layout = wa_layout
it_fieldcat = lt_fields
* IT_EXCLUDING =
* IT_SPECIAL_GROUPS =
* IT_SORT =
* IT_FILTER =
* IS_SEL_HIDE =
* I_DEFAULT = 'X'
* I_SAVE = ' '
* IS_VARIANT =
* IT_EVENTS =
* IT_EVENT_EXIT =
* IS_PRINT =
* IS_REPREP_ID =
* I_SCREEN_START_COLUMN = 0
* I_SCREEN_START_LINE = 0
* I_SCREEN_END_COLUMN = 0
* I_SCREEN_END_LINE = 0
* I_HTML_HEIGHT_TOP = 0
* I_HTML_HEIGHT_END = 0
* IT_ALV_GRAPHICS =
* IT_HYPERLINK =
* IT_ADD_FIELDCAT =
* IT_EXCEPT_QINFO =
* IR_SALV_FULLSCREEN_ADAPTER =
* IMPORTING
* E_EXIT_CAUSED_BY_CALLER =
* ES_EXIT_CAUSED_BY_USER =
TABLES
t_outtab = t_lista
* EXCEPTIONS
* PROGRAM_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.






ENDFORM. " write




*&--------------------------------------------------------------------*
*& Form set_pf_status
*&--------------------------------------------------------------------*
* text
*---------------------------------------------------------------------*
* -->RT_EXTAB text
*---------------------------------------------------------------------*
FORM set_pf_status USING rt_extab TYPE slis_t_extab.
SET PF-STATUS 'STAT100'.
ENDFORM. "set_pf_sta

*&--------------------------------------------------------------------*
*& Form user_comand
*&--------------------------------------------------------------------*
FORM user_command USING r_ucomm LIKE sy-ucomm
rs_selfield TYPE slis_selfield.

CASE r_ucomm.
WHEN 'BACK1'.
* MESSAGE w003(zsl_m) WITH 'BACK'.
* CALL SCREEN 1000.
set screen 0.
leave screen.
* leave to selection-screen 1000.
WHEN OTHERS.
MESSAGE s003(zsl_m) WITH rs_selfield-sel_tab_field
rs_selfield-col_stable
rs_selfield-row_stable
rs_selfield-fieldname.



ENDCASE.


ENDFORM. "user_comma

How to create a Control Table

In the program top, you have to declare the table control and the screen in witch it apears:

CONTROLS table_con TYPE TABLEVIEW USING SCREEN 200.

t_tc is the internal table with the actual content of the table control.
tc is the name of the container of the colums of the table control.

In the screen flow:


PROCESS BEFORE OUTPUT.
MODULE status_0100.

LOOP AT t_tc WITH CONTROL table_con.
MODULE actualiza_tc.
ENDLOOP.
*
PROCESS AFTER INPUT.

LOOP AT t_tc.
MODULE actualiza_t_tc.
ENDLOOP.

MODULE user_command_0200.


*&---------------------------------------------------------------------*
*& Module actualiza_t_tc INPUT
*&---------------------------------------------------------------------*
MODULE actualiza_t_tc INPUT.

READ TABLE t_tc INDEX table_con-current_line.
IF sy-subrc = 0.
MODIFY t_tc FROM tc INDEX table_con-current_line.
ELSE.
APPEND tc TO t_tc.
ENDIF.


*&---------------------------------------------------------------------*
*& Module actualiza_tc OUTPUT
*&---------------------------------------------------------------------*
MODULE actualiza_tc OUTPUT.

MOVE-CORRESPONDING t_tc TO tc.

ENDMODULE. " actualiza_tc OUTPUT

How to create a selection screen in a report with a select option and parameters

IS as simple as this:

SELECTION-SCREEN: BEGIN OF BLOCK myblock WITH FRAME TITLE text-001.

SELECT-OPTIONS: trans FOR sflight-carrid.
PARAMETERS: xpto LIKE sflight-connid.

SELECTION-SCREEN:END OF BLOCK myblock.

sexta-feira, 5 de março de 2010

How to Check Length and Alpha Numeric Variable

*

* Program to Check Length and Alpha Numeric Variable
*
* Is there any simple way or FM to check the string contains other
* than alphanumeric(A-Z and 0-9) and give an error message.
*
* How to check the length size of a variable?
*
* Written by : SAP Basis, ABAP Programming and Other IMG Stuff
* http://www.sap-img.com
*
REPORT ZCHECK_ALPHA_NUMERIC.

* Declare the variable

* For Length
data: serial_length type i.

* For Alpha numeric
data: str type string.
data: valid_characters type string.

* Fill in those valid characters you need to check
concatenate '0123456789' 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
'abcdefghijklmnopqrstuvwxyz' into valid_characters.

* User Input
parameters testchar(10) default '12345abc'.

* Get User Input
str = testchar.

* The Checks
if str co valid_characters.
write: / str, 'Characters are OK'.
else.
write: / str, 'Characters are NOT OK'.
endif.

* Check if length is equal to 10 characters
serial_length = strlen( str ).
if serial_length <> 10.
write: / str, 'Not 10 Digit'.
endif.

*-- End Program

quinta-feira, 4 de março de 2010

How to Access or change variables that are not passed in user exit

Many times we are faced with scenarios that require some SAP standard parameters to be changed or accessed. However, these parameters are not always available in user-exit's interface. This is peculiar to versions earlier than ECC where enhancement spot concept was not available.

In these cases, the parameter can be accessed and the value can be changed using the concept of pointers in ABAP. Here we assume that the parameter is declared either in the parent program, or in some other extrnal program (which has been triggered before user-exit call), but the parameter is not passed to the available user-exit.

The idea is to get the reference of the memory area of the required parameter and then assign a local pointer to access this memory area. Following example demonstrates this method:


data: wv_table(50) type c.

field-symbols: ‹wf_table› type any ,
‹wf_reference› type table.
DATA: wo_ref TYPE REF TO DATA.
wv_table = '(SAPLFPAYM_DE)DTADDEZ[]'.
ASSIGN (wv_table) TO ‹wf_table›.
GET REFERENCE OF ‹wf_table› INTO wo_ref.
ASSIGN wo_ref-›* TO ‹wf_reference›.
IF sy-subrc IS INITIAL.
loop at DTADDEZ.
* Manipulate the value of standard parameter here
* Then update to pass back the value
modify from dtaddez.
endloop.
endif.

How to make entries in database tables without transport request

At times, it is important to provide users with the flexibility to make entries in custom tables in quality/production systems. For example, an email ID being stored in the custom control table may need to be changed when the user wants the email to be sent to another ID. In such cases, its useful to allow users maintain the table entries themselves rather than entries being created in development system and then transported.

To allow users to maintain table entries without transporting, it is necessary that a table maintenance generator has been created for the table. Table delivery class can be chosen as C. Next, in the table maintenance generator, ensure that the user-defined screen option is selected. This attribute decides if the user can maintain the table entries.

Tip: Users generally do not have SM30 authorization in production systems. Hence it may be a good idea to make use of a parameter transaction which calls SM30 with the custom table name and in update mode.

How to find the larger of two numbers

A simple requirement like finding the larger of 2 numbers can make us think when the operators at hand are limited to basic + - * / DIV MOD SQRT.
Maintaining formula in database view V_TC25 is such a case. The formula builder of V_TC25 isn't as rich as expected and does not allow us to use logical conditions or ABSOLUTE values. Hence, the need of converting the logical condition:

IF A > B.
C = A
ELSE.
C = B.
ENDIF.

into an arithmetic expression using simple operators + - * / SQRT.

The arithmetic expression to get the larger of the two numbers is as follows:

(A+B)-(((A+B)-ABS(A-B))/2)

Since ABS is not available, but SQRT is, we convert that too into a simple arithmetic expression as follows:

ABS(A) = SQRT(A*A)

Hence the final expression to find the larger number becomes:

(A+B)-(((A+B)-SQRT((A-B)*(A-B))/2)

Welcome

In this Blog I plan to post SAP ABAP codes, tips & tricks and of course How to's.

Since i'm new to this programing language, I will post something when I learn it :D