Thursday, February 12, 2009

ABAP Programs For Learners: Collect Statement

ABAP Programs For Learners: Collect Statement

Commands & Useful Tips

SHIFT l_dat_enc LEFT DELETING LEADING space.
-------------------------------------------------
READ TABLE p_t_wiqmfe INTO p_wa_wiqmfe INDEX 1.
-------------------------------------------------
DESCRIBE TABLE gt_vpind LINES lv_lines.
-------------------------------------------------
REPLACE ' ' WITH '0' INTO l_kd_rk.
REPLACE ALL OCCURRENCES OF '"' IN v_char WITH space.
CONDENSE v_char.
-------------------------------------------------
set pf-status 'STANDRAD' excluding rt_extab.
-------------------------------------------------
DATA text TYPE string VALUE `I know you know`.
SHIFT text RIGHT DELETING TRAILING 'know'.
output: I know you
------------------------------------------------
modify t2 index idx from st_r
transporting a b c .


FOR EXCLUDING STANDARD BUTTON FROM ALV TOOLBAR
DATA : it_exclude TYPE slis_t_extab,
wa_exclude TYPE slis_extab.
*&---------------------------------------------------------------------*
* FOR EXCLUDING STANDARD BUTTONS FROM ALV TOOLBAR
*&---------------------------------------------------------------------*
wa_exclude-fcode = '&OUP'.
APPEND wa_exclude TO it_exclude.
CLEAR wa_exclude.
wa_exclude-fcode = '&ODN'.
APPEND wa_exclude TO it_exclude.
CLEAR wa_exclude.
wa_exclude-fcode = '&OAD'.
APPEND wa_exclude TO it_exclude.
CLEAR wa_exclude.
wa_exclude-fcode = '&INFO'.
APPEND wa_exclude TO it_exclude.
CLEAR wa_exclude.
*--similarly append other function codes of the buttons that you want to exclude
*--and pass this internal table to FM REUSE_ALV_GRID_DISPLAY
*&---------------------------------------------------------------------*
* DISPLAY RECORDS IN ALV GRID
*&---------------------------------------------------------------------*
CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
EXPORTING
* I_INTERFACE_CHECK = ' '
* I_BYPASSING_BUFFER = ' '
* I_BUFFER_ACTIVE = ' '
i_callback_program = rep_id
* i_callback_pf_status_set = 'PF'
i_callback_user_command = 'COMMAND'
i_callback_top_of_page = 'TOP'
* I_CALLBACK_HTML_TOP_OF_PAGE = ' '
* I_CALLBACK_HTML_END_OF_LIST = ' '
* I_STRUCTURE_NAME =
* I_BACKGROUND_ID = ' '
i_grid_title = wa_title
* I_GRID_SETTINGS =
is_layout = wa_layout
it_fieldcat = it_field
it_excluding = it_exclude "excule buttons
* IT_SPECIAL_GROUPS =

--------------------------------------------------------------------------------------------


DATA : tab TYPE rstabfield.
ranges : r_vbeln for vbap-vbeln.
tab-tablename = 'VBAP'.
tab-fieldname = 'VBELN'.
CALL FUNCTION 'COMPLEX_SELECTIONS_DIALOG'
EXPORTING
title = text-002
text = ' '
signed = 'X'
lower_case = 'X'
no_interval_check = 'X'
* just_display = 'X'
* just_incl = 'X'
* excluded_options = 'X'
* description =
* help_field =
* search_help =
tab_and_field = tab
TABLES
range = r_vbeln
EXCEPTIONS
no_range_tab = 1
cancelled = 2
internal_error = 3
invalid_fieldname = 4
OTHERS = 5.
IF sy-subrc EQ 2.
MESSAGE s899 WITH text-003. "no value selected
ELSEIF sy-subrc = 0 AND sy-subrc = 2.
MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
ENDIF.

read a file from directory using cl_gui_frontend_services

DATA: v_rc TYPE i.
DATA: lt_filetab TYPE filetable.
DATA: ls_filetab LIKE LINE OF lt_filetab.
PARAMETERS: p_file TYPE localfile.
AT SELECTION-SCREEN on VALUE-REQUEST FOR p_file.
CALL METHOD cl_gui_frontend_services=>file_open_dialog
CHANGING
file_table = lt_filetab
rc = v_rc
EXCEPTIONS
file_open_dialog_failed = 1
cntl_error = 2
error_no_gui = 3
not_supported_by_gui = 4
OTHERS = 5.
READ TABLE lt_filetab INTO ls_filetab INDEX 1.
IF sy-subrc = 0.
p_file = ls_filetab-filename.
ENDIF.

Monday, November 24, 2008

Auto-Refreshed ALV Report

REPORT z_alv_auto_refresh.
*---------------------------------------------------------------------*
* This report displays User's info (SM04) using the FM : *
* REUSE_ALV_LIST_DISPLAY *
* The list is auto-refreshed (refresh time : 5 seconds) *
*---------------------------------------------------------------------*
TYPE-POOLS: slis. " ALV Global Types

DATA : gt_user LIKE uinfo OCCURS 0 WITH HEADER LINE. " User info in SM04

START-OF-SELECTION.

PERFORM f_read_data.

PERFORM f_display_data.

*---------------------------------------------------------------------*
* Form F_LIRE_DATA
*---------------------------------------------------------------------*
FORM f_read_data.

REFRESH gt_user.

* Get User's info
CALL FUNCTION 'THUSRINFO'
TABLES
usr_tabl = gt_user.

* Wait in a task
PERFORM f_call_rfc_wait.

ENDFORM. " F_READ_DATA


*---------------------------------------------------------------------*
* Form F_DISPLAY_DATA
*---------------------------------------------------------------------*
FORM f_display_data.

DEFINE m_sort.
add 1 to ls_sort-spos.
ls_sort-fieldname = &1.
append ls_sort to lt_sort.
END-OF-DEFINITION.

DEFINE m_event_exit.
clear ls_event_exit.
ls_event_exit-ucomm = &1.
ls_event_exit-after = 'X'.
append ls_event_exit to lt_event_exit.
END-OF-DEFINITION.

DATA :
ls_layout TYPE slis_layout_alv,
lt_sort TYPE slis_t_sortinfo_alv,
ls_sort TYPE slis_sortinfo_alv,
lt_event_exit TYPE slis_t_event_exit,
ls_event_exit TYPE slis_event_exit.

* Build Sort Table
m_sort 'ZEIT'.

* Build Event Exit Table
m_event_exit '&NTE'. " Refresh

ls_layout-zebra = 'X'.
ls_layout-colwidth_optimize = 'X'.

CALL FUNCTION 'REUSE_ALV_LIST_DISPLAY'
EXPORTING
i_callback_program = sy-cprog
i_callback_user_command = 'USER_COMMAND'
is_layout = ls_layout
i_structure_name = 'UINFO'
it_sort = lt_sort
it_event_exit = lt_event_exit
TABLES
t_outtab = gt_user.

ENDFORM. " F_DISPLAY_DATA


*---------------------------------------------------------------------*
* FORM USER_COMMAND *
*---------------------------------------------------------------------*
FORM user_command USING i_ucomm TYPE syucomm
is_selfield TYPE slis_selfield. "#EC CALLED

CASE i_ucomm.
WHEN '&NTE'.
PERFORM f_read_data.
is_selfield-refresh = 'X'.
SET USER-COMMAND '&OPT'. " Optimize columns width
ENDCASE.

ENDFORM. " USER_COMMAND


*---------------------------------------------------------------------*
* Form F_CALL_RFC_WAIT
*---------------------------------------------------------------------*
FORM f_call_rfc_wait.

DATA lv_mssg(80). "#EC NEEDED

* Wait in a task
CALL FUNCTION 'RFC_PING_AND_WAIT' STARTING NEW TASK '001'
PERFORMING f_task_end ON END OF TASK
EXPORTING
seconds = 5 " Refresh time
busy_waiting = space
EXCEPTIONS
RESOURCE_FAILURE = 1
communication_failure = 2 MESSAGE lv_mssg
system_failure = 3 MESSAGE lv_mssg
OTHERS = 4.

ENDFORM. " F_CALL_RFC_WAIT


*---------------------------------------------------------------------*
* Form F_TASK_END
*---------------------------------------------------------------------*
FORM f_task_end USING u_taskname.
DATA lv_mssg(80). "#EC NEEDED

* Receiving task results
RECEIVE RESULTS FROM FUNCTION 'RFC_PING_AND_WAIT'
EXCEPTIONS
RESOURCE_FAILURE = 1
communication_failure = 2 MESSAGE lv_mssg
system_failure = 3 MESSAGE lv_mssg
OTHERS = 4.

CHECK sy-subrc EQ 0.
SET USER-COMMAND '&NTE'. " Refresh

ENDFORM. " F_TASK_END

Thursday, September 18, 2008

ALV Grid in 3 Steps without Screen Painter

DATA: l_alv TYPE REF TO cl_gui_alv_grid,
lt_sflight TYPE TABLE OF sflight.
SELECTION-SCREEN BEGIN OF SCREEN 1001 .
SELECTION-SCREEN END OF SCREEN 1001.
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 display the grid, NO CONTAINER
*DEFINITION IS REQUIRED !
CREATE OBJECT l_alv EXPORTING i_parent = cl_gui_container=>screen0.
* calling the display of the grid, the system will automatically create
*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.
DATA p_text(5) TYPE c." LENGTH 5.
DO 2 TIMES.
WAIT UP TO '0.5' SECONDS.
p_text(3) = sy-index.
p_text+3 = '%'.
CALL FUNCTION 'SAPGUI_PROGRESS_INDICATOR'
EXPORTING
percentage = sy-index
text = p_text.
ENDDO.
CALL SELECTION-SCREEN 1001." STARTING AT 8 8 ENDING AT 85 22.

Showing a progress bar in ABAP(Timer Clock)

REPORT zdany_progress_bar.
DATA p_text(5) TYPE c." LENGTH 5.
DO 3 TIMES.
WAIT UP TO '0.5' SECONDS.
p_text(3) = sy-index.
p_text+3 = '%'.
CALL FUNCTION 'SAPGUI_PROGRESS_INDICATOR'
EXPORTING
percentage = sy-index
text = p_text.
ENDDO.

ALV Output as E-Mail

*--------------------------------------------------------------------
*& Report ZTESTMAIL *
*& *
*&------------------------------------------------------
*& *
*& *
*&---------------------------------------------------------------------
REPORT ZTESTMAIL .
tables: ekko.
parameters: p_email type somlreci1-receiver ."default
*'kesava.perumal@gmail.com'.
types: begin of t_ekpo,
ebeln type ekpo-ebeln,
ebelp type ekpo-ebelp,
aedat type ekpo-aedat,
matnr type ekpo-matnr,
end of t_ekpo.
data: it_ekpo type standard table of t_ekpo initial size 0,
wa_ekpo type t_ekpo.
types: begin of t_charekpo,
ebeln(10) type c,
ebelp(5) type c,
aedat(8) type c,
matnr(18) type c,
end of t_charekpo.
data: wa_charekpo type t_charekpo.
data: it_message type standard table of solisti1 initial size 0
with header line.
data: it_attach type standard table of solisti1 initial size 0
with header line.

data: t_packing_list like sopcklsti1 occurs 0 with header line,
t_contents like solisti1 occurs 0 with header line,
t_receivers like somlreci1 occurs 0 with header line,
t_attachment like solisti1 occurs 0 with header line,
t_object_header like solisti1 occurs 0 with header line,
w_cnt type i,
w_sent_all(1) type c,
w_doc_data like sodocchgi1,
gd_error type sy-subrc,
gd_reciever type sy-subrc.
t_object_header = 'Text.xls'. append t_object_header.
************************************************************************
*START_OF_SELECTION
start-of-selection.

*Retrieve sample data from table ekpo
perform data_retrieval.

*Populate table with detaisl to be entered into .xls file
perform build_xls_data_table.
************************************************************************
*END-OF-SELECTION
end-of-selection.

*Populate message body text
perform populate_email_message_body.

*Send file by email as .xls speadsheet
perform send_file_as_email_attachment
tables it_message
it_attach
using p_email
'Example .xls documnet attachment'
'XLS'
'filename'
' '
' '
' '
changing gd_error
gd_reciever.

*Instructs mail send program for SAPCONNECT to send email(rsconn01)
perform initiate_mail_execute_program.
*&---------------------------------------------------------------------
*& Form DATA_RETRIEVAL
*&---------------------------------------------------------------------

*Retrieve data form EKPO table and populate itab it_ekko
*----------------------------------------------------------------------
form data_retrieval.
select ebeln ebelp aedat matnr
up to 10 rows
from ekpo
into table it_ekpo.
endform. " DATA_RETRIEVAL
*&---------------------------------------------------------------------
*& Form BUILD_XLS_DATA_TABLE
*&---------------------------------------------------------------------

*Build data table fo,r .xls document
*----------------------------------------------------------------------
form build_xls_data_table.
CONSTANTS: con_cret TYPE x VALUE '0D', "OK for non Unicode
con_tab TYPE x VALUE '09'. "OK for non Unicode
*If you have Unicode check active in program attributes thnen you will
*need to declare constants as follows
*class cl_abap_char_utilities definition load.
*constants:
*con_tab type c value cl_abap_char_utilities=>horizontal_tab,
*con_cret type c value cl_abap_char_utilities=>cr_lf.
concatenate 'EBELN' 'EBELP' 'AEDAT' 'MATNR' into it_attach separated by
con_tab.
concatenate con_cret it_attach into it_attach.
append it_attach.
loop at it_ekpo into wa_charekpo.
concatenate wa_charekpo-ebeln wa_charekpo-ebelp
wa_charekpo-aedat wa_charekpo-matnr into it_attach separated by con_tab.
concatenate con_cret it_attach into it_attach.
append it_attach.
endloop.
endform. " BUILD_XLS_DATA_TABLE

*&---------------------------------------------------------------------
*& Form SEND_FILE_AS_EMAIL_ATTACHMENT
*&---------------------------------------------------------------------

*Send email
*----------------------------------------------------------------------
form send_file_as_email_attachment tables pit_message
pit_attach
using p_email
p_mtitle
p_format
p_filename
p_attdescription
p_sender_address
p_sender_addres_type
changing p_error
p_reciever.
data: ld_error type sy-subrc,
ld_reciever type sy-subrc,
ld_mtitle like sodocchgi1-obj_descr,
ld_email like somlreci1-receiver,
ld_format type so_obj_tp ,
ld_attdescription type so_obj_nam ,
ld_attfilename type so_obj_des ,
ld_sender_address like soextreci1-receiver,
ld_sender_address_type like soextreci1-adr_typ,
ld_receiver like sy-subrc.
ld_email = p_email.
ld_mtitle = p_mtitle.
ld_format = p_format.
ld_attdescription = p_attdescription.
ld_attfilename = p_filename.
ld_sender_address = p_sender_address.
ld_sender_address_type = p_sender_addres_type.


*Fill the document data.
w_doc_data-doc_size = 1.

*Populate the subject/generic message attributes
w_doc_data-obj_langu = sy-langu.
w_doc_data-obj_name = 'SAPRPT'.
w_doc_data-obj_descr = ld_mtitle .
w_doc_data-sensitivty = 'F'.

*Fill the document data and get size of attachment
clear w_doc_data.
read table it_attach index w_cnt.
w_doc_data-doc_size =
( w_cnt - 1 ) * 255 + strlen( it_attach ).
w_doc_data-obj_langu = sy-langu.
w_doc_data-obj_name = 'SAPRPT'.
w_doc_data-obj_descr = ld_mtitle.
w_doc_data-sensitivty = 'F'.
clear t_attachment.
refresh t_attachment.
*t_attachment = pit_attach.
t_attachment[] = pit_attach[].
*Describe the body of the message
clear t_packing_list.
refresh t_packing_list.
t_packing_list-transf_bin = space.
t_packing_list-head_start = 1.
t_packing_list-head_num = 0.
t_packing_list-body_start = 1.
describe table it_message lines t_packing_list-body_num.
t_packing_list-doc_type = 'RAW'.
append t_packing_list.

*Create attachment notification
t_packing_list-transf_bin = 'X'.
t_packing_list-head_start = 1.
t_packing_list-head_num = 1.
t_packing_list-body_start = 1.
describe table t_attachment lines t_packing_list-body_num.
t_packing_list-doc_type = ld_format.
t_packing_list-obj_descr = ld_attdescription.
t_packing_list-obj_name = ld_attfilename.
t_packing_list-doc_size = t_packing_list-body_num * 255.
append t_packing_list.

*Add the recipients email address
clear t_receivers.
refresh t_receivers.
t_receivers-receiver = ld_email.
t_receivers-rec_type = 'U'.
t_receivers-com_type = 'INT'.
t_receivers-notif_del = 'X'.
t_receivers-notif_ndel = 'X'.
append t_receivers.
call function 'SO_DOCUMENT_SEND_API1'
exporting
document_data = w_doc_data
put_in_outbox = 'X'
sender_address = ld_sender_address
sender_address_type = ld_sender_address_type
commit_work = 'X'
importing
sent_to_all = w_sent_all
tables
object_header = t_object_header
packing_list = t_packing_list
contents_bin = t_attachment
contents_txt = it_message
receivers = t_receivers
exceptions
too_many_receivers = 1
document_not_sent = 2
document_type_not_exist = 3
operation_no_authorization = 4
parameter_error = 5
x_error = 6
enqueue_error = 7
others = 8.

*Populate zerror return code
ld_error = sy-subrc.

*Populate zreceiver return code
loop at t_receivers.
ld_receiver = t_receivers-retrn_code.
endloop.
endform.
*&---------------------------------------------------------------------
*& Form INITIATE_MAIL_EXECUTE_PROGRAM
*&---------------------------------------------------------------------

*Instructs mail send program for SAPCONNECT to send email.
*----------------------------------------------------------------------
form initiate_mail_execute_program.
wait up to 2 seconds.
submit rsconn01 with mode = 'INT'
with output = 'X'
and return.
endform. " INITIATE_MAIL_EXECUTE_PROGRAM
*&---------------------------------------------------------------------
*& Form POPULATE_EMAIL_MESSAGE_BODY
*&---------------------------------------------------------------------

*Populate message body text
*----------------------------------------------------------------------
form populate_email_message_body.
refresh it_message.
it_message = 'Please find attached a list test ekpo records'.
append it_message.
endform. " POPULATE_EMAIL_MESSAGE_BODY
Powered By Blogger