Sie sind auf Seite 1von 66

Reusable abap codes :

INDEX
1. To show alternate colors in rows in alv display.
2. TO put x in fieldx for all the entered values of field
3. Choosing presentation server path on selection screen
4. choosing application (unix) server.
5. Perform used to upload the file from the presentation server.
6. Perform used to upload the file from the application server.
7. Downloading the datas to application server
8. Downloading the datas to presentation server
9. For creating dynamic table and using them
10. Good String processing code
11. Idoc programming
12. bdc programming for updating material
13. Give user the status information during execution of program
14. Program to open excel and values get feeded automatically realtime or can be feeded in background jobs
15. Pop up programming
16. Wait time program
17. at line selection event
18. simple bdc program thru excel with good error handling
19. Important tables of SAP
20. To find the creation/modification data of document at unix server
21. Code to compare contents of two internal table
22. schedule jobs programmatically
23. Coding using define & end define.
24. This include allows you to display one or more ALV tables by simply using a local class. Block mode is
supported so you can display several tables on same screen.
25. User exit for invoices - RV60AFZZ
26. Program to download file to application server and then send mail to user to download it in presentation server
27. Program which create job and specify more details
28. Program to convert spool to pdf
29. Subroutine to prevent time out error
To show alternate colors in rows in alv display.
DATA: l_color(1) TYPE c.

l_color = '0'.
LOOP AT it_result INTO wa_result.
IF l_color = '0'.
wa_result-rowcolor = 'C400'.
l_color = 'X'.
ELSE.
wa_result-rowcolor = 'C500'.
l_color = '0'.
ENDIF.

MODIFY it_result FROM wa_result.


ENDLOOP.

wa_layout-info_fieldname = 'ROWCOLOR'.
wa_layout-box_fieldname = 'C_ROW'.

CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'


EXPORTING
i_callback_program = sy-repid
is_layout = wa_layout
it_fieldcat = it_fieldcat
it_events = it_events[]
TABLES
t_outtab = it_final.

It_result is the final internal table which will be used in fm


REUSE_ALV_GRID_DISPLAY. It will contain one additional field ‘ rowcolor’
besides all the necessary fields.

30. To use top of page event in alv :

TO put x in fieldx for all the entered values of field.


PERFORM setdatax USING uom_s uomx.

FORM setdatax USING regdata xdata.

FIELD-SYMBOLS: <fs1>, <fs2>.


DATA: n TYPE i.
DO.
n = n + 1.
ASSIGN COMPONENT n OF STRUCTURE regdata TO <fs1>.
IF sy-subrc <> 0.
EXIT.
ENDIF.

ASSIGN COMPONENT n OF STRUCTURE xdata TO <fs2>.


IF NOT <fs1> IS INITIAL.
<fs2> = 'X'.
ENDIF.
ENDDO.

ENDFORM. " setdatax

Good commands :

WRITE c_kunnr LEFT-JUSTIFIED TO wa_final-kunnr.


SPLIT wa_kna1-name2 AT '' INTO c_name1 c_name2 c_name3.

F4 help in selection screen for address :

(1)

AT SELECTION-SCREEN ON VALUE-REQUEST FOR po_fnm.


PARAMETERS: po_fnm(80) TYPE c MODIF ID md1 DEFAULT
'C:\seedfile.txt' LOWER CASE.

(2)
AT SELECTION-SCREEN ON VALUE-REQUEST FOR outfile.
PARAMETERS: outfile(60) TYPE c MODIF ID md2 DEFAULT
'/usr/sap/trans/data/seedfile.txt'
LOWER CASE.

Choosing presentation server path on selection screen :


PERFORM open_folder.

FORM open_folder .
DATA: l_it_file TYPE STANDARD TABLE OF sdokpath,
l_wa_file TYPE sdokpath,
l_folder TYPE c.
CALL FUNCTION 'TMP_GUI_FILE_OPEN_DIALOG'
TABLES
file_table = l_it_file
EXCEPTIONS
cntl_error = 1
OTHERS = 2.

IF sy-subrc EQ 0.
READ TABLE l_it_file INTO l_wa_file INDEX 1.
IF sy-subrc EQ 0.
po_fnm = l_wa_file-pathname.
ENDIF.
ENDIF.

choosing application (unix) server path on selection screen for


selecting path :

PERFORM open_file.

form open_file .
CALL FUNCTION 'F4_DXFILENAME_4_DYNP'
EXPORTING
* DYNPFIELD_FILETYPE =
dynpfield_filename = 'OUTFILE'
dyname = sy-repid
dynumb = sy-dynnr
filetype = 'P'
location = 'A'
server = ''
.

endform. " open_file

Perform used to upload the file from the presentation server.


PERFORM upload.

FORM upload .
MOVE p_file TO w_path.

CALL FUNCTION 'GUI_UPLOAD'


EXPORTING
filename = w_path
filetype = 'ASC'
has_field_separator =''
TABLES
data_tab = it_material

CLEAR wa_material.
LOOP AT it_material INTO wa_material.
select * from mara where
matnr = wa_material-matnr.
IF sy-subrc = 0.
wa_material-ean11 = mara-ean11.
MODIFY it_material FROM wa_material.
ENDIF.
CLEAR wa_material.
endselect.
ENDLOOP.

ENDFORM. " upload

* Perform used to upload the file from the application server.


PERFORM upload_apps.

FORM upload_apps .
DATA: l_line TYPE string,
l_matnr LIKE mara-matnr.

OPEN DATASET p_file1 FOR INPUT IN TEXT MODE ENCODING DEFAULT.


IF sy-subrc NE 0.
MESSAGE ID 'FB' TYPE 'E' NUMBER '002'
WITH p_file1.
ENDIF.

DO.
CLEAR: l_line,
l_matnr.
READ DATASET p_file1 INTO l_line.
IF sy-subrc <> 0.
EXIT.
ENDIF.
l_matnr = l_line.

CALL FUNCTION 'CONVERSION_EXIT_ALPHA_INPUT'


EXPORTING
input = l_matnr
IMPORTING
output = l_matnr.

APPEND l_matnr TO it_material.


ENDDO.

CLOSE DATASET p_file1.

CLEAR wa_material.
LOOP AT it_material INTO wa_material.
select * from mara where
matnr = wa_material-matnr.
IF sy-subrc = 0.
wa_material-ean11 = mara-ean11.
MODIFY it_material FROM wa_material.
ENDIF.
CLEAR wa_material.
endselect.
ENDLOOP.

ENDFORM. " Upload_apps


** Downloading the datas to application server
PERFORM download_appl.
form download_appl .

OPEN DATASET p_outfil FOR OUTPUT IN TEXT MODE ENCODING DEFAULT.

LOOP AT it_output.
TRANSFER it_output TO p_outfil.
IF sy-subrc NE 0.
it_status-matnr = it_output(12).
it_status-status = text-013.
it_status-message = text-008.
APPEND it_status.
ENDIF.
ENDLOOP.
CLOSE DATASET p_outfil.
IF sy-subrc NE 0.
MESSAGE ID 'FB' TYPE 'E' NUMBER '002'
WITH p_outfil.
ELSE.
MESSAGE ID 'FB' TYPE 'I' NUMBER '001'
WITH p_outfil.
ENDIF.

endform. " download_appl

** Downloading the datas to presentation server


PERFORM download_pc.

form download_pc .

CLEAR w_file.

w_file = p_desfil.

CALL FUNCTION 'GUI_DOWNLOAD'


EXPORTING
filename = w_file
filetype = 'ASC'
trunc_trailing_blanks = space
confirm_overwrite = c_x
trunc_trailing_blanks_eol = space
TABLES
data_tab = it_output
.
IF SY-SUBRC NE 0.
MESSAGE i001 WITH text-022.
LEAVE LIST-PROCESSING.
ELSE.
MESSAGE i001 WITH text-021.
ENDIF.

endform. " download_pc

For creating dynamic table and using them : whole running program

*&---------------------------------------------------------------------*
*& Report Z_T401212 *
*& *
*&---------------------------------------------------------------------*
*& *
*& *
*&---------------------------------------------------------------------*

REPORT Z_T401212 .

type-pools: slis.

field-symbols: <dyn_table> type standard table,


<dyn_wa>,
<dyn_field>.

data: alv_fldcat type slis_t_fieldcat_alv,


it_fldcat type lvc_t_fcat.

type-pools : abap.

data : it_details type abap_compdescr_tab,


wa_details type abap_compdescr.

data : ref_descr type ref to cl_abap_structdescr.

data: new_table type ref to data,


new_line type ref to data,
wa_it_fldcat type lvc_s_fcat.

selection-screen begin of block b1 with frame title text .


parameters: p_table(30) type c.
selection-screen end of block b1.

*Get the structure of the table.


ref_descr ?= cl_abap_typedescr=>describe_by_name( p_table ).
it_details = ref_descr->components.

loop at it_details into wa_details.


clear wa_it_fldcat.
wa_it_fldcat-fieldname = wa_details-name .
wa_it_fldcat-datatype = wa_details-type_kind.
wa_it_fldcat-inttype = wa_details-type_kind.
wa_it_fldcat-intlen = wa_details-length.
wa_it_fldcat-decimals = wa_details-decimals.
append wa_it_fldcat to it_fldcat .
endloop.

*Create dynamic internal table and assign to FS


call method cl_alv_table_create=>create_dynamic_table
exporting
it_fieldcatalog = it_fldcat
importing
ep_table = new_table.

assign new_table->* to <dyn_table>.

*Create dynamic work area and assign to FS


create data new_line like line of <dyn_table>.
assign new_line->* to <dyn_wa>.
*Select Data from table.
select * into corresponding fields of table <dyn_table>
from (p_table).

*Write out data from table.


loop at <dyn_table> into <dyn_wa>.
do.
assign component sy-index of structure <dyn_wa> to <dyn_field>.
if sy-subrc <> 0.
exit.
endif.
if sy-index = 1.
write:/ <dyn_field>.
else.
write: <dyn_field>.
endif.
enddo.
endloop.

Good String processing code :

You can do the following:

DATA: lv_string TYPE string,


lv_firstname TYPE string,
lv_lastname TYPE string,
lv_num TYPE i,
lv_check TYPE c.

lv_string = 'Firstname h g t Lastname'.

lv_check = lv_string(1).

• extract first name

WHILE NOT lv_check IS INITIAL.


CONCATENATE lv_firstname lv_string(1) INTO lv_firstname.
SHIFT lv_string LEFT BY 1 PLACES IN CHARACTER MODE.
lv_check = lv_string(1).
ENDWHILE.

• extract last name

lv_num = STRLEN( lv_string ).


lv_num = lv_num - 1.
lv_check = lv_string+lv_num(1).
WHILE NOT lv_check IS INITIAL.
CONCATENATE lv_string+lv_num(1) lv_lastname INTO lv_lastname.
SHIFT lv_string RIGHT BY 1 PLACES IN CHARACTER MODE.
lv_check = lv_string+lv_num(1).
ENDWHILE.

WRITE: / lv_lastname, ',', lv_firstname.


Regards,
Michael
Date format
write sy-datum to w_cdate YYMMDD.

Defining range :

RANGES: w_tlx FOR adr5-tlx_number.

IF NOT it_kna1[] IS INITIAL.


w_tlx-sign = 'E'.
w_tlx-option = 'CP'.
w_tlx-low = '000042*'.
APPEND w_tlx

Idoc programming :

Program to create idoc, if all configurations are already done. ( purchase order )

check the below sample code for triggering the idoc..The same thing u can do for ur
requirement..slect all then data from ur custom table and finally call the FM
"MASTER_IDOC_DISTRIBUTE".

&---------------------------------------------------------------------
*& Report ZZ_Program_To_Create_Idoc
&---------------------------------------------------------------------

report zz_program_to_create_idoc .
tables: ekko,ekpo.

selection-screen skip 3.
selection-screen begin of block b1 with frame title titl.
selection-screen skip.
select-options s_ebeln for ekko-ebeln.
selection-screen skip.
selection-screen end of block b1.

data: header_segment_name like edidd-segnam value 'Z1EKKO',


item_segment_name like edidd-segnam value 'Z1EKPO',
idoc_name like edidc-idoctp value 'Z19838IDOC1'.

data: header_segment_data like z1ekko,


item_segment_data like z1ekpo.

data: control_record like edidc.

data: messagetyp like edmsg-msgtyp value 'ZZ9838MESG1'.

data: i_communication like edidc occurs 0 with header line,


i_data like edidd occurs 0 with header line.

data: begin of i_ekko occurs 0,


ebeln like ekko-ebeln,
aedat like ekko-aedat,
bukrs like ekko-bukrs,
bsart like ekko-bsart,
lifnr like ekko-lifnr,
end of i_ekko.

data: begin of i_ekpo occurs 0,


ebelp like ekpo-ebelp,
matnr like ekpo-matnr,
menge like ekpo-menge,
meins like ekpo-meins,
netpr like ekpo-netpr,
end of i_ekpo.

start-of-selection.

select ebeln aedat bukrs bsart lifnr from ekko


into table i_ekko where ebeln in s_ebeln.

select ebelp
matnr
menge
meins
netpr
from ekpo
into table i_ekpo
where ebeln in s_ebeln.

control_record-mestyp = messagetyp.
control_record-rcvprt = 'LS'.
control_record-idoctp = idoc_name.
control_record-rcvprn = '0MART800'.

loop at i_ekko.
header_segment_data-ebeln = i_ekko-ebeln.
header_segment_data-aedat = i_ekko-aedat.
header_segment_data-bukrs = i_ekko-bukrs.
header_segment_data-bsart = i_ekko-bsart.
header_segment_data-lifnr = i_ekko-lifnr.
i_data-segnam = header_segment_name.
i_data-sdata = header_segment_data.
append i_data.

select ebelp
matnr
menge
meins
netpr
from ekpo
into table i_ekpo
where ebeln = i_ekko-ebeln.

loop at i_ekpo.
item_segment_data-ebelp = i_ekpo-ebelp.
item_segment_data-matnr = i_ekpo-matnr.
item_segment_data-menge = i_ekpo-menge.
item_segment_data-meins = i_ekpo-meins.
item_segment_data-netpr = i_ekpo-netpr.
i_data-segnam = item_segment_name.
i_data-sdata = item_segment_data.
append i_data.
endloop.
clear i_ekpo.
refresh i_ekpo.
endloop.

call function 'MASTER_IDOC_DISTRIBUTE'


exporting
master_idoc_control = control_record

* OBJ_TYPE = ''
* CHNUM = ''

tables
communication_idoc_control = i_communication
master_idoc_data = i_data
exceptions
error_in_idoc_control = 1
error_writing_idoc_status = 2
error_in_idoc_data = 3
sending_logical_system_unknown = 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.
else.

loop at i_communication.
write: 'IDOC GENERATED', i_communication-docnum.
endloop.
commit work.

endif.

initialization.
titl = 'ENTER THE PURCHASE ORDER NUMBER'.

kindly reward if found helpful.

cheers,
Hema.

Syngenta project standard usage codes :


Report name NO STANDARD PAGE HEADING MESSAGE-ID 38
LINE-COUNT 65 LINE-SIZE 150.
Modifying material number with bdc

LOOP AT it_final INTO wa_final.


PERFORM fill_it_bdcdata.
CALL TRANSACTION 'MM02' USING it_bdcdata MODE c_mod UPDATE c_mod1
MESSAGES INTO it_messages.
* PERFORM bdc_process.
PERFORM error_messages.
PERFORM append_messages.
CLEAR : it_bdcdata,it_messages.
REFRESH : it_bdcdata,it_messages.
CLEAR wa_final.
CLEAR wa_result.
ENDLOOP.

FORM fill_it_bdcdata .
PERFORM bdc_dynpro USING 'SAPLMGMM' '0060'.
PERFORM bdc_field USING 'BDC_CURSOR' 'RMMG1-MATNR'.
PERFORM bdc_field USING 'BDC_OKCODE' '=ENTR'.
PERFORM bdc_field USING 'RMMG1-MATNR'
wa_final-matnr.
MOVE wa_final-matnr1 TO wa_result-mat_no.

PERFORM bdc_dynpro USING 'SAPLMGMM' '0070'.


PERFORM bdc_field USING 'BDC_CURSOR' 'MSICHTAUSW-DYTXT(01)'.
PERFORM bdc_field USING 'BDC_OKCODE' '=ENTR'.
PERFORM bdc_field USING 'MSICHTAUSW-KZSEL(01)' 'X'.
PERFORM bdc_dynpro USING 'SAPLMGMM' '4004'.
PERFORM bdc_field USING 'BDC_OKCODE' '=BU'.
PERFORM bdc_field USING 'BDC_CURSOR' 'MARA-NUMTP'.
* READ TABLE it_material INTO wa_material
* WITH KEY matnr = wa_final-matnr1 .
* IF sy-subrc = 0.
* IF wa_material-ean11 IS INITIAL.
PERFORM bdc_field USING 'MARA-EAN11' wa_final-gtn.

MOVE wa_final-gtn TO wa_result-gtn.

PERFORM bdc_field USING 'MARA-NUMTP' c_nmtp.


PERFORM bdc_dynpro USING 'SAPLSP01' '0300'.
PERFORM bdc_field USING 'BDC_OKCODE' '=YES'.
flag = 1.
MOVE wa_final-gtn TO wa_result-gtn.
CLEAR wa_material.
* ENDIF.
ENDFORM.

FORM bdc_field USING fnam LIKE bdcdata-fnam


fval.
it_bdcdata-fnam = fnam.
it_bdcdata-fval = fval.
APPEND it_bdcdata.
CLEAR it_bdcdata.

ENDFORM. " BDC_FIELD


FORM bdc_dynpro USING program LIKE bdcdata-program
dynpro LIKE bdcdata-dynpro.
it_bdcdata-program = program.
it_bdcdata-dynpro = dynpro.
it_bdcdata-dynbegin = 'X'.
APPEND it_bdcdata.
CLEAR it_bdcdata.

ENDFORM. " BDC_DYNPRO


Give user the status information during execution of
program :
CALL FUNCTION 'SAPGUI_PROGRESS_INDICATOR'
EXPORTING
* PERCENTAGE = 0
TEXT = TEXT-007
EXCEPTIONS
OTHERS = 1.

Reference code : RSDEMO01

Program to open excel and values get feeded automatically realtime or can be
feeded in background jobs.

INCLUDE OLE2INCL.
* handles for OLE objects
DATA: H_EXCEL TYPE OLE2_OBJECT, " Excel object
H_MAPL TYPE OLE2_OBJECT, " list of workbooks
H_MAP TYPE OLE2_OBJECT, " workbook
H_ZL TYPE OLE2_OBJECT, " cell
H_F TYPE OLE2_OBJECT. " font

* start Excel
CREATE OBJECT H_EXCEL 'EXCEL.APPLICATION'.
PERFORM ERR_HDL.
SET PROPERTY OF H_EXCEL 'Visible' = 1.
PERFORM ERR_HDL.

* output column headings to active Excel sheet


PERFORM FILL_CELL USING 1 1 1 'Flug'(001).
PERFORM FILL_CELL USING 1 2 1 'Nr'(002).
PERFORM FILL_CELL USING 1 3 1 'Von'(003).
PERFORM FILL_CELL USING 1 4 1 'Nach'(004).
PERFORM FILL_CELL USING 1 5 1 'Zeit'(005).

* disconnect from Excel


FREE OBJECT H_EXCEL.
PERFORM ERR_HDL.

FORM FILL_CELL USING I J BOLD VAL.


CALL METHOD OF H_EXCEL 'Cells' = H_ZL EXPORTING #1 = I #2 = J.
PERFORM ERR_HDL.
SET PROPERTY OF H_ZL 'Value' = VAL .
PERFORM ERR_HDL.
GET PROPERTY OF H_ZL 'Font' = H_F.
PERFORM ERR_HDL.
SET PROPERTY OF H_F 'Bold' = BOLD .
PERFORM ERR_HDL.
ENDFORM.

FORM ERR_HDL.
IF SY-SUBRC <> 0.
WRITE: / 'Fehler bei OLE-Automation:'(010), SY-SUBRC.
STOP.
ENDIF.
ENDFORM. " ERR_HDL

Pop up programming
* Warn user about over-selecting...
DESCRIBE TABLE ZVEXT_ORDERS LINES READ_COUNT.
IF READ_COUNT GT 500.
WRITE READ_COUNT TO TEMP_TEXT LEFT-JUSTIFIED.
CONCATENATE 'You have selected' TEMP_TEXT 'orders...'
INTO TEMP_TEXT SEPARATED BY SPACE.
CALL FUNCTION 'POPUP_TO_DECIDE'
EXPORTING
DEFAULTOPTION = '2'
TEXTLINE1 = TEMP_TEXT
TEXTLINE2 = '(processing may be slow)'
TEXTLINE3 = 'Are you sure you want to continue?'
TEXT_OPTION1 = 'Continue'
TEXT_OPTION2 = 'Cancel'
ICON_TEXT_OPTION1 = 'ICON_CHECKED'
ICON_TEXT_OPTION2 = 'ICON_CANCEL'
TITEL = 'Are you sure?'
* START_COLUMN = 25
* START_ROW =6
CANCEL_DISPLAY =''
IMPORTING
ANSWER = TEMP_ANSWER.
IF TEMP_ANSWER = 2.
STOP.
ENDIF.
ENDIF.

Wait time program

REPORT ZPAUSE.
parameters: p_sec type i.
data: new_time like sy-uzeit.

if p_sec > 3600.


move 3600 to p_sec.
endif.

get time.
new_time = sy-uzeit + p_sec.
while sy-uzeit < new_time.
get time.
endwhile.

At line selection code :


AT LINE-SELECTION.
CLEAR : w_cacnt, w_ccode, w_augbl, w_belnr.
SPLIT sy-lisel AT sy-vline INTO w_ccode w_cacnt w_vname
w_regio w_augbl w_belnr.
PERFORM conversion USING w_cacnt.
PERFORM conversion USING w_augbl.
PERFORM conversion USING w_belnr.
SORT i_bsak BY lifnr augbl bukrs belnr.
CLEAR wa_bsak.
READ TABLE i_bsak INTO wa_bsak WITH KEY lifnr = w_cacnt
augbl = w_augbl
bukrs = w_ccode
belnr = w_belnr
BINARY SEARCH
TRANSPORTING gjahr.
with this command wa_bsak will be populated with only
field ghahr.
SET PARAMETER ID bln FIELD w_belnr.
SET PARAMETER ID buk FIELD w_ccode.
SET PARAMETER ID gjr FIELD wa_bsak-gjahr.

CALL TRANSACTION FB03 AND SKIP FIRST SCREEN.

Simple bdc program with good error handling


*----------------------------------------------------------------------*
* Case Id : *
* PROGRAM ID/ Include ID : ZF_FB09 *
* PROGRAM TITLE : Upload from excel in fb09 thru excel *
*
* DEVELOPED BY : T401212 *
* CREATION DATE : 18-MAR-2008 *

*----------------------------------------------------------------------*
* MODIFICATION HISTORY *
*----------------------------------------------------------------------*
*Ver|DATE |PGMR |TRANSPORT |DESCRIPTION OF CHANGES *
*No | | | | *
* ---------------------------------------------------------------------*
*001|18-MAR-2008|T401212 |CL1K972236 |Initial *
*----------------------------------------------------------------------*

REPORT zf_fb09
NO STANDARD PAGE HEADING
LINE-COUNT 65 LINE-SIZE 150.
DATA: it_excel LIKE alsmex_tabline OCCURS 0 WITH HEADER LINE.
DATA : bdc_suc_count TYPE i,
bdc_err_count TYPE i,
bdc_count TYPE i.

DATA : BEGIN OF it_fb09 OCCURS 0,


belnr LIKE rf05l-belnr,
bukrs LIKE rf05l-bukrs,
gjahr LIKE rf05l-gjahr,
buzei LIKE rf05l-buzei,
zterm LIKE bseg-zterm,
zfbdt(10),
END OF it_fb09.

DATA: BEGIN OF it_bdcdata OCCURS 0.


INCLUDE STRUCTURE bdcdata.
DATA : END OF it_bdcdata.

DATA : c_mod(1) TYPE c VALUE 'N',


c_mod1 TYPE c VALUE 'S',
it_messages LIKE bdcmsgcoll OCCURS 0 WITH HEADER LINE.

DATA : BEGIN OF it_result OCCURS 0,


belnr LIKE rf05l-belnr,
status(20),
message(250),
END OF it_result.
TABLES : t100.

SELECTION-SCREEN BEGIN OF BLOCK block1 .

PARAMETERS:
p_file LIKE rlgrap-filename OBLIGATORY.

SELECTION-SCREEN END OF BLOCK block1.

AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_file.

* Perform used to support the location of file.


PERFORM open_folder.

START-OF-SELECTION.

* Perform used to upload the file from the presentation server.


PERFORM upload.

PERFORM fb09_transaction.

PERFORM write_error_message.

*&---------------------------------------------------------------------*
*& Form bdc
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
FORM fb09_transaction .

CLEAR it_fb09.
LOOP AT it_fb09.

PERFORM fill_it_bdcdata.
CALL TRANSACTION 'FB09' USING it_bdcdata MODE c_mod UPDATE c_mod1
MESSAGES INTO it_messages.
IF sy-subrc EQ 0.
ADD 1 TO bdc_suc_count.
*it_result-status = 'SUCCESS'.
*it_result-belnr = it_fb09-belnr.
*append it_result.
ELSE.
ADD 1 TO bdc_err_count.
PERFORM error_messages.
ENDIF.
ADD 1 TO bdc_count.
CLEAR it_fb09.
ENDLOOP.

ENDFORM. " fb09-transaction.


*&---------------------------------------------------------------------*
*& Form ERROR_MESSAGES
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
FORM error_messages .
DATA : lv_string TYPE string.
LOOP AT it_messages WHERE msgtyp = 'E'.

CLEAR t100.
SELECT SINGLE * FROM t100 WHERE sprsl = it_messages-msgspra
AND arbgb = it_messages-msgid
AND msgnr = it_messages-msgnr.

IF sy-subrc EQ 0.
CLEAR lv_string.
lv_string = t100-text.
IF lv_string CS '&1'.
REPLACE '&1' WITH it_messages-msgv1 INTO lv_string.
"Variable part of a message
REPLACE '&2' WITH it_messages-msgv2 INTO lv_string.
REPLACE '&3' WITH it_messages-msgv3 INTO lv_string.
REPLACE '&4' WITH it_messages-msgv4 INTO lv_string.
ELSE.
REPLACE '&' WITH it_messages-msgv1 INTO lv_string.
REPLACE '&' WITH it_messages-msgv2 INTO lv_string.
REPLACE '&' WITH it_messages-msgv3 INTO lv_string.
REPLACE '&' WITH it_messages-msgv4 INTO lv_string.
ENDIF.
CONDENSE lv_string.
CONCATENATE it_messages-msgtyp lv_string INTO lv_string
SEPARATED BY space.
ELSE.
lv_string = it_messages.
ENDIF.
CLEAR it_messages.
ENDLOOP.
it_result-belnr = it_fb09.
it_result-status = 'FAILED'.
it_result-message = lv_string.
APPEND it_result.

ENDFORM.
*&---------------------------------------------------------------------*
*& Form FILL_IT_BDCDATA
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
FORM fill_it_bdcdata .
REFRESH it_bdcdata.
PERFORM bdc_dynpro USING 'SAPMF05L' '0102'.

PERFORM bdc_field USING 'BDC_CURSOR'


'RF05L-BUZEI'.

PERFORM bdc_field USING 'BDC_OKCODE'


'/00'.

PERFORM bdc_field USING 'RF05L-BELNR'


it_fb09-belnr.

PERFORM bdc_field USING 'RF05L-BUKRS'


it_fb09-bukrs.

PERFORM bdc_field USING 'RF05L-GJAHR'


it_fb09-gjahr.

PERFORM bdc_field USING 'RF05L-BUZEI'


it_fb09-buzei.

PERFORM bdc_dynpro USING 'SAPMF05L' '0301'.

PERFORM bdc_field USING 'BDC_CURSOR'


it_fb09-zfbdt.

PERFORM bdc_field USING 'BDC_OKCODE'


'=AE'.

*perform bdc_field using 'BSEG-SKFBT'


* record-SKFBT_005.
*
PERFORM bdc_field USING 'BSEG-ZTERM'
it_fb09-zterm.

PERFORM bdc_field USING 'BSEG-ZFBDT'


it_fb09-zfbdt.

ENDFORM. " FILL_IT_BDCDATA

*&---------------------------------------------------------------------*
*& Form BDC_DYNPRO
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* -->P_0701 text
* -->P_0702 text
*----------------------------------------------------------------------*
FORM bdc_dynpro USING program LIKE bdcdata-program
dynpro LIKE bdcdata-dynpro.
it_bdcdata-program = program.
it_bdcdata-dynpro = dynpro.
it_bdcdata-dynbegin = 'X'.
APPEND it_bdcdata.
CLEAR it_bdcdata.

ENDFORM. " BDC_DYNPRO


*&---------------------------------------------------------------------*
*& Form BDC_FIELD
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* -->P_0706 text
* -->P_0707 text
*----------------------------------------------------------------------*
FORM bdc_field USING fnam LIKE bdcdata-fnam
fval.
it_bdcdata-fnam = fnam.
it_bdcdata-fval = fval.
APPEND it_bdcdata.
CLEAR it_bdcdata.

ENDFORM. " BDC_FIELD


*&---------------------------------------------------------------------*
*& Form upload
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
FORM upload .
DATA : w_path LIKE rlgrap-filename.
MOVE p_file TO w_path.

CALL FUNCTION 'ALSM_EXCEL_TO_INTERNAL_TABLE'


EXPORTING
filename = w_path
i_begin_col = 1
i_begin_row = 2
i_end_col = 6
i_end_row = 6000
TABLES
intern = it_excel.

PERFORM process_excel.

ENDFORM. " upload

*&---------------------------------------------------------------------
*& Form PROCESS
*&---------------------------------------------------------------------
*text
*----------------------------------------------------------------------
*
*--> p1 text
*<-- p2 text
*----------------------------------------------------------------------
FORM process_excel .

LOOP AT it_excel.
CASE it_excel-col.
WHEN '001'.
it_fb09-belnr = it_excel-value.

WHEN '002'.
it_fb09-bukrs = it_excel-value.
WHEN '003'.
it_fb09-gjahr = it_excel-value.
WHEN '004'.
it_fb09-buzei = it_excel-value.
WHEN '005'.
it_fb09-zterm = it_excel-value.
WHEN '006'.
CALL FUNCTION 'CONVERT_DATE_TO_INTERNAL'
EXPORTING
date_external = it_excel-value
IMPORTING
date_internal = it_excel-value
EXCEPTIONS
date_external_is_invalid = 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.
DATA : lv_date TYPE dats.

lv_date = it_excel-value.
*IT_FB09-ZFBDT = lv_date.
WRITE lv_date TO it_fb09-zfbdt MM/DD/YYYY.
ENDCASE.

AT END OF row.
APPEND it_fb09.
CLEAR it_fb09.
ENDAT.
CLEAR it_excel.
ENDLOOP.

ENDFORM. " PROCESS_EXCEL

*&---------------------------------------------------------------------*
*& Form open_folder
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
FORM open_folder .

DATA: l_it_file TYPE STANDARD TABLE OF sdokpath,


l_wa_file TYPE sdokpath,
l_folder TYPE c.

CALL FUNCTION 'TMP_GUI_FILE_OPEN_DIALOG'


TABLES
file_table = l_it_file
EXCEPTIONS
cntl_error = 1
OTHERS = 2.

IF sy-subrc EQ 0.
READ TABLE l_it_file INTO l_wa_file INDEX 1.
IF sy-subrc EQ 0.
p_file = l_wa_file-pathname.
ENDIF.
ENDIF.

ENDFORM. " open_folder


*&---------------------------------------------------------------------*
*& Form WRITE_ERROR_MESSAGE
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
FORM write_error_message.
WRITE:/ 'Total no. of rows uploaded from excel',bdc_count.
WRITE:/'Total no. of rows updated to SAP', bdc_suc_count.
WRITE:/ 'Total no. of rows in error stage', bdc_err_count.
SKIP.
WRITE:/ 'Rows in error stage as follows:'.
SKIP.
LOOP AT it_result.
WRITE:/ 'Document Number ',it_result-belnr.
WRITE:/ 'Status : ',it_result-status.
WRITE:/ 'Message :',it_result-message.
CLEAR it_result.
ENDLOOP.

ENDFORM. " WRITE_ERROR_MESSAGE


Important tables of SAP
1. TBTCO – CONTAINS ALL SAP’S JOB INFO
2. LIPS- Delivery item level info
3. LIKP- delivery header level info
4. vbak – sales order header data
5. vbap – sales order item data
6. vbrp – billing item data
7. vbrk – billing header data
8. VBFA- sales document flow
9. EKPO- purchase order – item
10. EKKO- purchase order – header
11. TSTC –tcode data
12. E070 transport request header info
13. KONP condition record info
14. TNAPR processing program for output
15. AUFM goods movement for order
16. RBCO invoice document,gl document, account assignment
17. VBKD sales data : business data
18. TSPAT Organizational Unit: Sales Divisions: Texts
19. C001 : Customer grp/material grp/ account key
20. VTTK : shipment header level data
21. VTTP : shipment item level data
22. VTTS : Stage of Shipment
23. VTPA : SHIPMENT PARTNER

To find the creation/modification data of document at unix


server
PERFORM get_date_time USING dir file1.
CONCATENATE file-mod_time+0(2)
file-mod_time+3(2)
file-mod_time+6(2) INTO last_mod_time.

FORM get_date_time USING P_DIR P_FILE1.

CONCATENATE P_DIR P_FILE1 into filepath.

OPEN DATASET filepath FOR input IN text MODE ENCODING default.


IF sy-subrc EQ 0.
PERFORM show_data USING P_DIR P_FILE1.
ENDIF.
CLOSE DATASET filepath.

ENDFORM. " get_date_time

FORM show_data USING DIR_NAME FILE_NAME.

DATA: timezone_sec(5) TYPE p, " seconds local time is later than GMT
timezone_name(7) TYPE c.

DATA: BEGIN OF searchpoints OCCURS 10,


dirname(75) TYPE c, " name of directory.
sp_name(75) TYPE c, " name of entry. (may end with
sp_cs(10) TYPE c, " ContainsString pattern for name
END OF searchpoints.

* prepare time zone correction.


CALL 'C_GET_TIMEZONE' ID 'NAME' FIELD timezone_name
ID 'SEC' FIELD timezone_sec.
timezone_sec = 0 - sy-tzone.
IF sy-dayst = 'X'.
subtract 3600 from timezone_sec.
ENDIF.

searchpoints-dirname = DIR_NAME.
searchpoints-sp_name = FILE_NAME.

* get in directory

CALL 'C_DIR_READ_START' ID 'DIR' FIELD searchpoints-dirname


ID 'FILE' FIELD searchpoints-sp_name
ID 'ERRNO' FIELD file-errno
ID 'ERRMSG' FIELD file-errmsg.

CLEAR file.
CHECK sy-subrc = 0.

CALL 'C_DIR_READ_NEXT' ID 'NAME' FIELD file-name


ID 'MTIME' FIELD file-mtime
ID 'ERRNO' FIELD file-errno
ID 'ERRMSG' FIELD file-errmsg.

IF SY-SUBRC <> 0.
WRITE: / 'C_DIR_READ_NEXT', 'SUBRC', SY-SUBRC.
EXIT.
ENDIF.
PERFORM P6_TO_DATE_TIME(RSTR0400) USING file-mtime
timezone_sec
file-mod_time
file-mod_date.

CALL 'C_DIR_READ_FINISH' ID 'ERRNO' FIELD file-errno


ID 'ERRMSG' FIELD file-errmsg.

IF SY-SUBRC <> 0.
EXIT.
ENDIF.

ENDFORM. " show_data

Code to compare contents of two internal table

WRITE:/ 'Customer Partnership'.


SORT aknvp BY kunnr vkorg vtweg spart parvw kunn2 ASCENDING.
SORT sknvp BY kunnr vkorg vtweg spart parvw kunn2 ASCENDING.
LOOP AT sknvp.
MOVE sknvp TO aknvp.
READ TABLE aknvp WITH KEY kunnr = sknvp-kunnr
vkorg = sknvp-vkorg
vtweg = sknvp-vtweg
spart = sknvp-spart
parvw = sknvp-parvw
kunn2 = sknvp-kunn2 BINARY SEARCH
COMPARING kunnr parvw kunn2
sales_year gtcb_status rralfalfa_status. "Ins M002
* IF sy-subrc > 2 OR ( sy-subrc = 2 AND aknvp-zprocessin = '06' ).
"Del M003
IF sy-subrc > 2 OR ( sy-subrc = 2 AND aknvp-zprocessin = '02' ).
"Ins M003
icust-kunnr = sknvp-kunnr.
icust-erdat = sy-datum.
icust-erzet = sy-uzeit.
icust-loevm = ' '.
COLLECT icust.
WRITE:/ sknvp-kunnr, sknvp-vkorg, sknvp-vtweg, sknvp-spart,
sknvp-parvw, sknvp-kunn2,
sknvp-sales_year,sknvp-gtcb_status, "Ins M002
sknvp-rralfalfa_status. "Ins M002
CLEAR icust.
ENDIF.
ENDLOOP.

schedule jobs programmatically


DESCRIBE TABLE i_transports LINES lv_lines.
IF lv_lines GT 10.
MESSAGE i016(z1) WITH
'This may take long time online!running in background'.
lv_jobname = sy-repid .

CALL FUNCTION 'JOB_OPEN'


EXPORTING
jobname = lv_jobname
sdlstrtdt = sy-datum
sdlstrttm = sy-uzeit
IMPORTING
jobcount = lv_count
EXCEPTIONS
cant_create_job = 1
invalid_job_data = 2
jobname_missing = 3
OTHERS = 4.
IF sy-subrc <> 0.
ENDIF.

OPEN DATASET lv_file IN TEXT MODE FOR OUTPUT ENCODING DEFAULT.


LOOP AT i_transports.
TRANSFER i_transports TO lv_file.
ENDLOOP.
CLOSE DATASET lv_file.

SUBMIT ztrconflict TO SAP-SPOOL WITHOUT SPOOL DYNPRO


WITH p_upd EQ 'X'
USER sy-uname VIA JOB lv_jobname NUMBER lv_count
AND RETURN.
CALL FUNCTION 'JOB_CLOSE'
EXPORTING
jobname = lv_jobname
jobcount = lv_count
strtimmed = 'X'
EXCEPTIONS
cant_create_job = 1
invalid_job_data = 2
jobname_missing = 3
OTHERS = 4.
IF sy-subrc <> 0.
ENDIF.
WRITE:/ 'Background job scheduled'.

Coding using define & end define


CLEAR wa_komk.
wa_komk-belnr = svbak-vbeln.
wa_komk-knumv = svbak-knumv.
wa_komk-vkorg = svbak-vkorg.
wa_komk-vtweg = svbak-vtweg.
wa_komk-spart = svbak-spart.
get_price_cond wa_komk itkomv.
SORT itkomv BY kposn kschl.

DEFINE get_price_cond.
call function 'RV_PRICE_PRINT_REFRESH'
tables
tkomv = &2.

call function 'RV_KONV_SELECT'


exporting
comm_head_i = &1
* GENERAL_READ =''
* READ_CONDITION_RECORD =''
* IMPORTING
* COMM_HEAD_E =
tables
tkomv = &2
.
END-OF-DEFINITION.

This include allows you to display one or more ALV tables by


simply using a local class. Block mode is supported so you can
display several tables on same screen.
[edit]
Language and Platform
This is pure ABAP and will work on releases from 4.6 onwards.

[edit]
Version
V2 with block mode.

[edit]
Code
*&---------------------------------------------------------------------*
*& Include Z_TABLE_DISPLAY
*&---------------------------------------------------------------------*
* VERSION 2
*
* Code from François Henrotte (EPONA Solutions)
* http://www.eponasolutions.com
* Belgium
* Please keep reference !
*********************************************************************
* HOW TO
*
* Display an ALV grid :
* CALL METHOD lcl_table_display=>display_grid( 'tabname' ).
*
* Display a hierarchical list with header and detail :
* CALL METHOD lcl_table_display=>display_hier( in_header = 'table1'
* in_detail = 'table2' ).
*
* Display a block list with two tables :
* CALL METHOD lcl_table_display=>set_block_mode( 'X' ).
* CALL METHOD lcl_table_display=>display_grid( 'table1' ).
* CALL METHOD lcl_table_display=>display_grid( 'table2' ).
* CALL METHOD lcl_table_display=>end_block_list( ).
*
* You never have to deal with field catalog of tables !!
*
* What if field catalog has to be changed anyway ?
*
* ob_table = lcl_table_display=>create_table( 'tabname' ).
* CALL METHOD ob_table->set_alv_fieldtext( in_field = field
* in_ftext = 'text' ).
* CALL METHOD lcl_table_display=>display_grid( 'tabname' ).
*********************************************************************

type-pools: abap, slis.

*----------------------------------------------------------------------*
* LCL_TABLE_DISPLAY DEFINITION
*----------------------------------------------------------------------*
class lcl_table_display definition.

public section.

class-methods: display_list importing in_tabname type any,


display_grid importing in_tabname type any,
display_hier importing in_header type any
in_detail type any
in_level type i optional,

set_block_mode importing in_mode type c,


set_block_text importing in_text type any,
end_block_list importing in_print
type slis_print_alv optional
exceptions display_error,

create_table
importing
in_tabname type tabname
returning
value(out_table) type ref to lcl_table_display
exceptions
create_error,

get_existing_table
importing
in_tabname type any optional
in_repid type any optional
in_struc type any optional
returning
value(out_table) type ref to lcl_table_display
exceptions
no_parameter
not_found,

refresh_objects.

methods: constructor importing in_data type standard table


exceptions casting_error
empty_fieldcat.

methods: get_alv_fieldcat returning value(out_fieldcat)


type slis_t_fieldcat_alv.

methods: set_table_name importing in_tabname type any,


set_alv_title importing in_title type any,
set_alv_fieldcat importing in_fieldcat
type slis_t_fieldcat_alv,
set_alv_fieldnoout importing in_field type any
in_noout type c optional
in_tech type c optional,
set_alv_fieldedit importing in_field type any
in_edit type c optional,
set_alv_fieldtext importing in_field type any
in_ftext type any,
set_alv_fieldsum importing in_field type any
in_dosum type c optional,
set_alv_linebreak importing in_field type any,
set_alv_settings importing in_settings type any,
set_alv_layout importing in_layout type any,
set_alv_print importing in_print type any,
set_alv_sorting importing in_field type any
in_desc type c optional
in_group type any optional
in_subtot type c optional,
set_alv_keys importing in_level type i
in_key type c optional,
set_alv_event importing in_name type any
in_form type any,
set_all_events.

protected section.

data: g_table_type type slis_list_type.


data: g_title type lvc_title,
gt_fcat type slis_t_fieldcat_alv,
gs_sett type lvc_s_glay,
gs_layo type slis_layout_alv,
gt_sort type slis_t_sortinfo_alv,
gt_evnt type slis_t_event,
gs_prin type slis_print_alv.

class-methods: output_hierarchy exceptions display_error.

methods: output_table importing mode type c


exceptions display_error,
output_list,
output_grid.

private section.

class-data: gt_table_obj type table of ref to lcl_table_display,


g_header_table type ref to lcl_table_display,
g_detail_table type ref to lcl_table_display.

class-data: g_variant_level type i.

class-data: g_block_mode type c,


g_block_text type slis_text40.

types: begin of ty_defin,


fieldname type fieldname,
ref_tabname type tabname,
ref_fieldname type fieldname,
end of ty_defin.

data: g_repid type repid,


g_struc type tabname,
g_table type tabname.

data: gt_data type ref to data.

data: gt_defin type table of ty_defin,


g_level type tabname.

methods: init_block_list,
fill_fieldcat importing repid type repid
struc type tabname
changing fcat type slis_t_fieldcat_alv
exceptions no_definition,
get_definition importing repid type repid
struc type tabname
changing abap type rsfb_source,
recursive_definition importing repid type repid
changing abap type rsfb_source,
map_structure importing source type any
changing destin type any,
get_default_variant changing out_variant type disvariant.
endclass. "lcl_table_display DEFINITION

*----------------------------------------------------------------------*
* LCL_TABLE_DISPLAY IMPLEMENTATION
*----------------------------------------------------------------------*
class lcl_table_display implementation.
***
* Display table in ALV list
***
method display_list.
data: l_object type ref to lcl_table_display,
l_tabname type tabname,
l_found type c.

l_tabname = in_tabname.
loop at gt_table_obj into l_object.
if l_object->g_table = l_tabname.
l_found = 'X'.
exit.
endif.
endloop.
if l_found is initial.
l_object = lcl_table_display=>create_table( l_tabname ).
if g_block_mode is initial.
l_object->g_table_type = 4.
else.
l_object->g_table_type = 2.
endif.
call method l_object->set_all_events.
endif.
call method l_object->output_list.
endmethod. "display_list
***
* Display table in ALV grid
***
method display_grid.
data: l_object type ref to lcl_table_display,
l_tabname type tabname,
l_found type c.

l_tabname = in_tabname.
loop at gt_table_obj into l_object.
if l_object->g_table = l_tabname.
l_found = 'X'.
exit.
endif.
endloop.
if l_found is initial.
l_object = lcl_table_display=>create_table( l_tabname ).
if g_block_mode is initial.
l_object->g_table_type = 4.
else.
l_object->g_table_type = 2.
endif.
call method l_object->set_all_events.
endif.
if g_block_mode is initial.
call method l_object->output_grid.
else.
call method l_object->output_list.
endif.
endmethod. "display_grid
***
* Display tables in ALV hierarchy
***
method display_hier.
data: l_tabnam1 type tabname,
l_tabnam2 type tabname,
lt_fcat1 type slis_t_fieldcat_alv,
lt_fcat2 type slis_t_fieldcat_alv,
ls_fcat1 type slis_fieldcat_alv,
ls_fcat2 type slis_fieldcat_alv.

l_tabnam1 = in_header.
l_tabnam2 = in_detail.
call method lcl_table_display=>get_existing_table
exporting
in_tabname = l_tabnam1
receiving
out_table = g_header_table
exceptions
not_found = 1.
if sy-subrc ne 0.
g_header_table = lcl_table_display=>create_table( l_tabnam1 ).
if g_block_mode is initial.
g_header_table->g_table_type = 1.
else.
g_header_table->g_table_type = 3.
endif.
call method g_header_table->set_all_events.
endif.
call method lcl_table_display=>get_existing_table
exporting
in_tabname = l_tabnam2
receiving
out_table = g_detail_table
exceptions
not_found = 1.
if sy-subrc ne 0.
g_detail_table = lcl_table_display=>create_table( l_tabnam2 ).
endif.

* Set key fields


if in_level is initial.
lt_fcat1 = g_header_table->get_alv_fieldcat( ).
lt_fcat2 = g_detail_table->get_alv_fieldcat( ).
loop at lt_fcat1 into ls_fcat1.
ls_fcat1-key = space.
loop at lt_fcat2 into ls_fcat2
where fieldname = ls_fcat1-fieldname.
ls_fcat2-key = space.
ls_fcat2-key_sel = 'X'.
ls_fcat2-tech = 'X'.
modify lt_fcat2 from ls_fcat2 transporting key.
endloop.
if sy-subrc = 0.
ls_fcat1-key = 'X'.
endif.
modify lt_fcat1 from ls_fcat1 transporting key.
endloop.
call method g_header_table->set_alv_fieldcat
exporting
in_fieldcat = lt_fcat1.
call method g_detail_table->set_alv_fieldcat
exporting
in_fieldcat = lt_fcat2.
else.
call method g_header_table->set_alv_keys
exporting
in_level = in_level.
call method g_detail_table->set_alv_keys
exporting
in_level = in_level
in_key = space.
endif.

call method output_hierarchy.


endmethod. "display_hier
***
* Set block mode
***
method set_block_mode.
g_block_mode = in_mode.
endmethod. "set_block_mode
***
* Set block text
***
method set_block_text.
g_block_text = in_text.
endmethod. "set_block_text
***
* Create new table
***
method create_table.
data: l_object type ref to lcl_table_display.

field-symbols: <local> type standard table.

assign (in_tabname) to <local>.


if not <local> is assigned.
raise create_error.
endif.

create object l_object exporting in_data = <local>


exceptions casting_error = 1
empty_fieldcat = 2.
if sy-subrc ne 0.
raise create_error.
endif.
call method l_object->set_table_name
exporting
in_tabname = in_tabname.

* Default print options


l_object->gs_prin-no_print_selinfos = 'X'.
l_object->gs_prin-no_coverpage = 'X'.
l_object->gs_prin-no_print_listinfos = 'X'.

out_table = l_object.
endmethod. "create_table
***
* Get existing table
***
method get_existing_table.
data: l_object type ref to lcl_table_display,
l_tabname type tabname,
l_repid type repid,
l_struc type tabname,
l_found type c.

l_tabname = in_tabname.
l_repid = in_repid.
l_struc = in_struc.
if l_tabname is initial.
if l_repid is initial and
l_struc is initial.
raise no_parameter.
else.
* Get last existing table with same definition
loop at gt_table_obj into l_object.
if l_object->g_repid = l_repid and
l_object->g_struc = l_struc.
l_found = 'X'.
exit.
endif.
endloop.
endif.
else.
* Get last existing table with same name
loop at gt_table_obj into l_object.
if l_object->g_table = l_tabname.
l_found = 'X'.
exit.
endif.
endloop.
endif.

if l_found is initial.
raise not_found.
else.
out_table = l_object.
endif.
endmethod. "get_existing_table
***
* Create table display
***
method constructor.
data: l_object type ref to lcl_table_display.

data: ls_data type ref to data.

data: ob_desc type ref to cl_abap_structdescr.

data: l_found type c,


l_absol type char200,
l_repid type repid,
l_struc type tabname.

field-symbols: <table> type standard table,


<struc> type any.

* Get data and store it into attribute


create data me->gt_data like in_data.
assign me->gt_data->* to <table>.
<table> = in_data.

* Get global data definition


create data ls_data like line of <table>.
assign ls_data->* to <struc>.
catch system-exceptions assign_casting_illegal_cast = 1.
ob_desc ?= cl_abap_typedescr=>describe_by_data( <struc> ).
endcatch.
if sy-subrc = 1.
raise casting_error.
endif.

* Get program name and main type used to define table


l_absol = ob_desc->absolute_name.
split l_absol at '\TYPE=' into l_repid l_struc.
shift l_repid up to '='.
shift l_repid.

check l_struc np '%_*'.

* Set attributes
me->g_repid = l_repid.
me->g_struc = l_struc.

me->g_table = l_struc.
replace 'TY' with 'WT' into me->g_table.

* Field catalog
call method lcl_table_display=>get_existing_table
exporting
in_repid = l_repid
in_struc = l_struc
receiving
out_table = l_object
exceptions
not_found = 1.
if sy-subrc = 0.
me->gt_fcat = l_object->get_alv_fieldcat( ).
call method set_table_name
exporting
in_tabname = me->g_table.
else.
call method fill_fieldcat
exporting
repid = l_repid
struc = l_struc
changing
fcat = me->gt_fcat.
if me->gt_fcat is initial.
raise empty_fieldcat.
endif.
endif.

* Keep list of tables


append me to gt_table_obj.
endmethod. "constructor
***
* Output list
***
method output_list.
call method output_table
exporting
mode = 'L'.
endmethod. "output_list
***
* Output grid
***
method output_grid.
call method output_table
exporting
mode = 'G'.
endmethod. "output_grid
***
* Output table
***
method output_table.
data: l_object type ref to lcl_table_display.

data: ls_vari type disvariant.

field-symbols: <table> type standard table.

assign me->gt_data->* to <table>.

if not g_block_mode is initial.


read table gt_table_obj into l_object index 1.
if sy-subrc = 0.
if l_object->g_table = me->g_table.
call method init_block_list.
endif.
endif.
endif.

* Get default user variant


call method get_default_variant
changing
out_variant = ls_vari.

* Display table contents


if mode = 'G'.
call function 'REUSE_ALV_GRID_DISPLAY'
exporting
i_callback_program = me->g_repid
i_grid_title = me->g_title
i_grid_settings = me->gs_sett
is_layout = me->gs_layo
it_fieldcat = me->gt_fcat
it_sort = me->gt_sort
i_save = 'U'
is_variant = ls_vari
it_events = me->gt_evnt
is_print = me->gs_prin
tables
t_outtab = <table>
exceptions
program_error = 1
others = 2.
if sy-subrc <> 0.
raise display_error.
endif.
call method refresh_objects.
else.
if g_block_mode is initial.
call function 'REUSE_ALV_LIST_DISPLAY'
exporting
i_callback_program = me->g_repid
is_layout = me->gs_layo
it_fieldcat = me->gt_fcat
it_sort = me->gt_sort
i_save = 'U'
is_variant = ls_vari
it_events = me->gt_evnt
is_print = me->gs_prin
tables
t_outtab = <table>
exceptions
others = 0.
if sy-subrc <> 0.
raise display_error.
endif.
call method refresh_objects.
else.
call function 'REUSE_ALV_BLOCK_LIST_APPEND'
exporting
is_layout = me->gs_layo
it_fieldcat = me->gt_fcat
i_tabname = me->g_table
it_events = me->gt_evnt
it_sort = me->gt_sort
i_text = g_block_text
tables
t_outtab = <table>
exceptions
program_error = 1
maximum_of_appends_reached = 2
others = 3.
if sy-subrc <> 0.
raise display_error.
endif.
endif.
endif.
endmethod. "output_table
***
* Output hierarchy
***
method output_hierarchy.
data: l_object type ref to lcl_table_display.

data: lt_fcat type slis_t_fieldcat_alv,


lt_sort type slis_t_sortinfo_alv,
ls_fcat type slis_fieldcat_alv,
ls_vari type disvariant,
ls_keyi type slis_keyinfo_alv.

data: l_index type numc2,


l_field type fieldname.

field-symbols: <head> type standard table,


<deta> type standard table,
<targ> type any.

assign g_header_table->gt_data->* to <head>.


assign g_detail_table->gt_data->* to <deta>.

* Set key fields as common fields between header and detail


loop at g_header_table->gt_fcat into ls_fcat
where key = 'X'.
l_index = l_index + 1.
* Create link
concatenate 'HEADER' l_index into l_field.
assign component l_field of structure ls_keyi to <targ>.
if sy-subrc = 0.
<targ> = ls_fcat-fieldname.
unassign <targ>.
else.
exit.
endif.
endloop.

append lines of g_header_table->gt_fcat to lt_fcat.


append lines of g_detail_table->gt_fcat to lt_fcat.

append lines of g_header_table->gt_sort to lt_sort.


append lines of g_detail_table->gt_sort to lt_sort.

if not g_block_mode is initial.


read table gt_table_obj into l_object index 1.
if sy-subrc = 0.
if l_object->g_table = g_header_table->g_table.
call method g_header_table->init_block_list.
endif.
endif.
endif.

* Get default user variant


call method g_header_table->get_default_variant
changing
out_variant = ls_vari.

if g_block_mode is initial.
call function 'REUSE_ALV_HIERSEQ_LIST_DISPLAY'
exporting
i_callback_program = g_header_table->g_repid
is_layout = g_header_table->gs_layo
it_fieldcat = lt_fcat
it_sort = lt_sort
i_save = 'U'
is_variant = ls_vari
it_events = g_header_table->gt_evnt
i_tabname_header = g_header_table->g_table
i_tabname_item = g_detail_table->g_table
is_keyinfo = ls_keyi
is_print = g_header_table->gs_prin
tables
t_outtab_header = <head>
t_outtab_item = <deta>
exceptions
program_error = 1
others = 2.
if sy-subrc <> 0.
raise display_error.
endif.
call method refresh_objects.
else.
call function 'REUSE_ALV_BLOCK_LIST_HS_APPEND'
exporting
is_layout = g_header_table->gs_layo
it_fieldcat = lt_fcat
is_keyinfo = ls_keyi
i_header_tabname = g_header_table->g_table
i_item_tabname = g_detail_table->g_table
it_events = g_header_table->gt_evnt
it_sort = lt_sort
i_text = g_block_text
tables
t_outtab_header = <head>
t_outtab_item = <deta>
exceptions
program_error = 1
maximum_of_appends_reached = 2
others = 3.
if sy-subrc <> 0.
raise display_error.
endif.
endif.
endmethod. "output_hierarchy
***
* Init block list
***
method init_block_list.
data: ls_evnt1 type slis_alv_event,
ls_evnt2 type slis_alv_event.

* Events for whole list display


concatenate 'F_' slis_ev_pf_status_set '_BLOCK'
into ls_evnt1-form.
concatenate 'F_' slis_ev_user_command '_BLOCK'
into ls_evnt2-form.

* Initialization of block list


call function 'REUSE_ALV_BLOCK_LIST_INIT'
exporting
i_callback_program = me->g_repid
i_callback_pf_status_set = ls_evnt1-form
i_callback_user_command = ls_evnt2-form.
endmethod. "init_block_list
***
* End of block list
***
method end_block_list.
data: l_object type ref to lcl_table_display,
ls_print type slis_print_alv.

check not g_block_mode is initial.


if in_print is supplied.
ls_print = in_print.
else.
read table gt_table_obj into l_object index 1.
ls_print = l_object->gs_prin.
endif.
call function 'REUSE_ALV_BLOCK_LIST_DISPLAY'
exporting
is_print = ls_print
exceptions
program_error = 1
others = 2.
if sy-subrc <> 0.
raise display_error.
endif.
call method refresh_objects.
endmethod. "end_block_list
***
* Refresh table of objects
***
method refresh_objects.
free: gt_table_obj.
endmethod.
***
* Fill field catalog
***
method fill_fieldcat.
data: lt_abap type rsfb_source.

data: ls_defin type ty_defin.


data: lt_dfies type table of dfies,
ls_dfies type dfies,
ls_dd04v type dd04v,
ls_dd01v type dd01v,
l_flong type dfies-lfieldname,
l_dname type dfies-domname.

data: ls_fcat type slis_fieldcat_alv,


ls_fcat2 type slis_fieldcat_alv.

data: l_index type i,


l_nbfld type i.

free: me->gt_defin.

* Process data definition


call method get_definition
exporting
repid = repid
struc = struc
changing
abap = lt_abap.

* Process sub levels if required


call method recursive_definition
exporting
repid = repid
changing
abap = lt_abap.

if me->gt_defin is initial.
raise no_definition.
endif.

loop at me->gt_defin into ls_defin.


clear: ls_fcat.
move-corresponding ls_defin to ls_fcat.
* Retrieve info about this field
free: ls_dfies, ls_dd04v, ls_dd01v, l_dname.
l_flong = ls_fcat-ref_fieldname.
set locale language 'E'.
translate: ls_fcat-ref_tabname to upper case,
ls_fcat-ref_fieldname to upper case,
l_flong to upper case.
if not ls_fcat-ref_tabname is initial.
* Try to get info about field in table
call function 'DDIF_FIELDINFO_GET'
exporting
tabname = ls_fcat-ref_tabname
fieldname = ls_fcat-ref_fieldname
lfieldname = l_flong
importing
dfies_wa = ls_dfies
exceptions
not_found = 1
internal_error = 2
others = 3.
if sy-subrc = 0.
move-corresponding ls_dfies to ls_fcat.
ls_fcat-fieldname = ls_defin-fieldname.
move: ls_dfies-keyflag to ls_fcat-key,
ls_dfies-scrtext_m to ls_fcat-seltext_l,
ls_dfies-domname to l_dname.
endif.
else.
* Try to get info about structure
ls_defin-ref_tabname = ls_defin-ref_fieldname.
call function 'DDIF_FIELDINFO_GET'
exporting
tabname = ls_defin-ref_tabname
tables
dfies_tab = lt_dfies
exceptions
others = 0.
if not lt_dfies is initial.
* Process fields of this structure
loop at lt_dfies into ls_dfies.
clear: ls_fcat.
move-corresponding ls_dfies to ls_fcat.
if ls_defin-fieldname ne 'INCLUDE'.
concatenate ls_defin-fieldname ls_fcat-fieldname
into ls_fcat-fieldname
separated by '-'.
endif.
move ls_dfies-keyflag to ls_fcat-key.
move ls_dfies-scrtext_m to ls_fcat-seltext_l.
ls_fcat-tabname = me->g_table.
clear: ls_fcat-col_pos,
ls_fcat-offset.
if ls_fcat-ref_tabname is initial.
ls_fcat-ddictxt = 'L'.
endif.
* Display Yes/No fields as checkboxes
if ls_dfies-domname = 'XFELD'.
ls_fcat-checkbox = 'X'.
endif.

* Add field to field catalog


append ls_fcat to fcat.
endloop.
continue.
else.
* Try to get info about data element
call function 'DDIF_DTEL_GET'
exporting
name = ls_fcat-ref_fieldname
langu = sy-langu
importing
dd04v_wa = ls_dd04v
exceptions
illegal_input = 1
others = 2.
if sy-subrc = 0.
move-corresponding ls_dd04v to ls_fcat.
move: ls_dd04v-scrtext_m to ls_fcat-seltext_l,
ls_dd04v-domname to l_dname.
else.
* Finally try to get info about domain
call function 'DDIF_DOMA_GET'
exporting
name = ls_fcat-ref_fieldname
langu = sy-langu
importing
dd01v_wa = ls_dd01v
exceptions
illegal_input = 1
others = 2.
if sy-subrc = 0.
move-corresponding ls_dd01v to ls_fcat.
move: ls_dd01v-ddtext to ls_fcat-seltext_l,
ls_dd01v-domname to l_dname.
endif.
endif.
endif.
endif.
* Table name must be internal table containing data
ls_fcat-tabname = g_table.
* No offset
clear: ls_fcat-offset.
* Default text is stored in long text
if ls_fcat-ref_tabname is initial.
ls_fcat-ddictxt = 'L'.
endif.
* Display Yes/No fields as checkboxes
if l_dname = 'XFELD'.
ls_fcat-checkbox = 'X'.
endif.

* Add field to field catalog


append ls_fcat to fcat.
endloop.
* Positions
loop at fcat into ls_fcat.
ls_fcat-row_pos = 1.
ls_fcat-col_pos = sy-tabix.
modify fcat from ls_fcat transporting row_pos col_pos.
endloop.
* Link between fields
describe table fcat lines l_nbfld.
loop at fcat into ls_fcat.
if sy-tabix ne l_nbfld.
l_index = sy-tabix + 1.
read table fcat into ls_fcat2 index l_index.
if sy-subrc = 0.
if ls_fcat-datatype = 'CURR'.
* Currency unit
if ls_fcat2-datatype = 'CUKY'.
ls_fcat-cfieldname = ls_fcat2-fieldname.
ls_fcat-ctabname = ls_fcat2-tabname.
modify fcat from ls_fcat.
else.
loop at fcat into ls_fcat2
from l_index
where datatype = 'CUKY'.
* First currency unit after field
ls_fcat-cfieldname = ls_fcat2-fieldname.
ls_fcat-ctabname = ls_fcat2-tabname.
modify fcat from ls_fcat.
exit.
endloop.
if sy-subrc ne 0.
* No currency unit after field, try before
read table fcat into ls_fcat2
with key datatype = 'CUKY'.
if sy-subrc = 0.
ls_fcat-cfieldname = ls_fcat2-fieldname.
ls_fcat-ctabname = ls_fcat2-tabname.
modify fcat from ls_fcat.
else.
* Default is EURO
ls_fcat-currency = 'EUR'.
endif.
endif.
endif.
endif.
if ls_fcat-datatype = 'QUAN'.
* Quantity unit
if ls_fcat2-datatype = 'UNIT'.
ls_fcat-cfieldname = ls_fcat2-fieldname.
ls_fcat-ctabname = ls_fcat2-tabname.
modify fcat from ls_fcat.
endif.
endif.
endif.
endif.
endloop.
endmethod. "fill_fieldcat
***
* Get definition of type from code source
***
method get_definition.
data: l_strng type rssource,
ls_abap type rssource,
l_fdpos type i,
l_first type i,
l_lastr type i.

data: lt_incl type table of repid,


ls_incl type repid.

* Get program code


read report repid into abap.
check sy-subrc eq 0.

* Get first line of definition


concatenate 'BEGIN OF' struc into l_strng
separated by space.
loop at abap into ls_abap.
if ls_abap cs l_strng.
l_fdpos = strlen( l_strng ) + sy-fdpos.
if ls_abap(1) = '*' or ls_abap(sy-fdpos) cs '"'.
continue.
endif.
if ls_abap+l_fdpos(1) ca ',. "'.
l_first = sy-tabix.
exit.
endif.
endif.
endloop.
if l_first is initial.
* Table is defined in an include
call function 'RS_GET_ALL_INCLUDES'
exporting
program = repid
tables
includetab = lt_incl
exceptions
others = 1.
if sy-subrc = 0.
loop at lt_incl into ls_incl.
* Try to find definition in this include
read report ls_incl into abap.
loop at abap into ls_abap.
if ls_abap cs l_strng.
l_fdpos = strlen( l_strng ) + sy-fdpos.
if ls_abap(1) = '*' or ls_abap(sy-fdpos) cs '"'.
continue.
endif.
if ls_abap+l_fdpos(1) ca ',. "'.
l_first = sy-tabix.
exit.
endif.
endif.
endloop.
if not l_first is initial.
exit.
endif.
endloop.
endif.
endif.

* Get last line of definition


concatenate 'END OF' struc into l_strng
separated by space.
loop at abap into ls_abap.
if ls_abap cs l_strng.
l_fdpos = strlen( l_strng ) + sy-fdpos.
if ls_abap(1) = '*' or ls_abap(sy-fdpos) cs '"'.
continue.
endif.
if ls_abap+l_fdpos(1) ca ',. "'.
l_lastr = sy-tabix - l_first.
exit.
endif.
endif.
endloop.

* Keep only relevant code lines


if l_first le 0
or l_lastr le 0.
refresh abap.
else.
delete abap to l_first.
delete abap from l_lastr.
endif.
endmethod. "get_definition
***
* Get definition of type recursively
***
method recursive_definition.
data: lt_token type table of stokex,
ls_token type stokex,
lt_state type table of sstmnt,
ls_state type sstmnt.

data: ls_defin type ty_defin,


l_reffld type fieldname.

data: lt_recu type rsfb_source.

* Retrieve tokens
scan abap-source abap
tokens into lt_token
statements into lt_state.

loop at lt_state into ls_state.


clear: ls_defin.
* Field name
read table lt_token into ls_token
index ls_state-from.
ls_defin-fieldname = ls_token-str.
* Reference type
read table lt_token into ls_token
index ls_state-to.
l_reffld = ls_token-str.
* Check if this type is defined in program
free: lt_recu.
call method get_definition
exporting
repid = repid
struc = l_reffld
changing
abap = lt_recu.
if lt_recu is initial.
if not g_level is initial.
concatenate g_level ls_defin-fieldname
into ls_defin-fieldname separated by '-'.
condense ls_defin-fieldname.
endif.
if l_reffld cs '-'.
split l_reffld at '-'
into ls_defin-ref_tabname
ls_defin-ref_fieldname.
if ls_defin-ref_tabname = 'SY'.
ls_defin-ref_tabname = 'SYST'.
endif.
else.
ls_defin-ref_fieldname = ls_token-str.
endif.
append ls_defin to me->gt_defin.
else.
* Process sub levels
if me->g_level is initial.
me->g_level = ls_defin-fieldname.
else.
concatenate me->g_level ls_defin-fieldname into me->g_level
separated by '-'.
endif.
call method recursive_definition
exporting
repid = repid
changing
abap = lt_recu.
if me->g_level cs '-'.
shift me->g_level right up to '-'.
shift me->g_level right.
shift me->g_level left deleting leading space.
else.
clear: me->g_level.
endif.
endif.
endloop.
endmethod. "recursive_definition
***
* Get fieldcat
***
method get_alv_fieldcat.
out_fieldcat = me->gt_fcat.
endmethod. "get_alv_fieldcat
***
* Set table name
***
method set_table_name.
data: l_fcat type slis_fieldcat_alv.

loop at me->gt_fcat into l_fcat.


l_fcat-tabname = in_tabname.
modify me->gt_fcat from l_fcat.
endloop.
me->g_table = in_tabname.
endmethod. "set_table_name
***
* Set title
***
method set_alv_title.
me->g_title = in_title.
endmethod. "set_alv_title
***
* Set fieldcat
***
method set_alv_fieldcat.
me->gt_fcat = in_fieldcat.
endmethod. "set_alv_fieldcat
***
* Set field invisible
***
method set_alv_fieldnoout.
data: l_field type fieldname,
l_noout type c,
l_tech type c,
ls_fcat type slis_fieldcat_alv.

l_field = in_field.
if in_noout is supplied.
l_noout = in_noout.
else.
l_noout = 'X'.
endif.
if in_tech is supplied.
l_tech = in_tech.
endif.

loop at me->gt_fcat into ls_fcat


where fieldname = l_field.
ls_fcat-no_out = l_noout.
ls_fcat-tech = l_tech.
modify gt_fcat from ls_fcat transporting no_out tech.
endloop.
endmethod. "set_alv_fieldnoout
***
* Set field editable
***
method set_alv_fieldedit.
data: l_field type fieldname,
l_edit type c,
ls_fcat type slis_fieldcat_alv.

l_field = in_field.
if in_edit is supplied.
l_edit = in_edit.
else.
l_edit = 'X'.
endif.

loop at me->gt_fcat into ls_fcat


where fieldname = l_field.
ls_fcat-edit = l_edit.
modify gt_fcat from ls_fcat transporting edit.
endloop.
endmethod. "set_alv_fieldedit
***
* Set field text
***
method set_alv_fieldtext.
data: l_field type fieldname,
ls_fcat type slis_fieldcat_alv.

l_field = in_field.
loop at me->gt_fcat into ls_fcat
where fieldname = l_field.
ls_fcat-seltext_m = in_ftext.
ls_fcat-ddictxt = 'M'.
modify gt_fcat from ls_fcat transporting seltext_m ddictxt.
endloop.
endmethod. "set_alv_fieldtext
***
* Set field sum
***
method set_alv_fieldsum.
data: l_field type fieldname,
l_dosum type c,
ls_fcat type slis_fieldcat_alv.

l_field = in_field.
if in_dosum is supplied.
l_dosum = in_dosum.
else.
l_dosum = 'X'.
endif.

loop at me->gt_fcat into ls_fcat


where fieldname = l_field.
ls_fcat-do_sum = l_dosum.
modify gt_fcat from ls_fcat transporting do_sum.
endloop.
endmethod. "set_alv_fieldsum
***
* Set line break in field catalog
***
method set_alv_linebreak.
data: l_field type fieldname,
ls_fcat type slis_fieldcat_alv,
l_tabix type i.

l_field = in_field.
read table me->gt_fcat into ls_fcat
with key fieldname = l_field.
if sy-subrc = 0.
l_tabix = sy-tabix.
else.
exit.
endif.

loop at me->gt_fcat into ls_fcat


from l_tabix.
ls_fcat-row_pos = ls_fcat-row_pos + 1.
modify gt_fcat from ls_fcat transporting row_pos.
endloop.
endmethod. "set_alv_linebreak
***
* Set settings
***
method set_alv_settings.
call method map_structure
exporting
source = in_settings
changing
destin = me->gs_sett.
endmethod. "set_alv_settings
***
* Set layout
***
method set_alv_layout.
call method map_structure
exporting
source = in_layout
changing
destin = me->gs_layo.
endmethod. "set_alv_layout
***
* Set printing options
***
method set_alv_print.
call method map_structure
exporting
source = in_print
changing
destin = me->gs_prin.
endmethod. "set_alv_print
***
* Set sortings
***
method set_alv_sorting.
data: l_desc type alvdynp-sortdown,
l_group type alvdynp-grouplevel,
l_subtot type alvdynp-subtotals.
data: ls_sort type slis_sortinfo_alv,
l_index type i.

if in_desc is supplied.
l_desc = in_desc.
endif.
if in_group is supplied.
l_group = in_group.
else.
l_group = '*'.
endif.
if in_subtot is supplied.
l_subtot = in_subtot.
else.
l_subtot = 'X'.
endif.

describe table me->gt_sort lines l_index.


l_index = l_index + 1.

ls_sort-spos = l_index.
ls_sort-fieldname = in_field.
ls_sort-tabname = me->g_table.
if l_desc is initial.
ls_sort-up = 'X'.
else.
ls_sort-down = 'X'.
endif.
ls_sort-group = l_group.
ls_sort-subtot = l_subtot.

append ls_sort to me->gt_sort.


endmethod. "set_alv_sorting
***
* Set key fields
***
method set_alv_keys.
data: l_key type c,
ls_fcat type slis_fieldcat_alv.

if in_key is supplied.
l_key = in_key.
else.
l_key = 'X'.
endif.
loop at me->gt_fcat into ls_fcat from 1 to in_level.
ls_fcat-key = l_key.
modify gt_fcat from ls_fcat transporting key.
endloop.
endmethod. "set_alv_keys
***
* Add event
***
method set_alv_event.
data: ls_evnt type slis_alv_event.

loop at gt_evnt into ls_evnt


where name = in_name.
ls_evnt-form = in_form.
modify gt_evnt from ls_evnt transporting form.
endloop.
if sy-subrc ne 0.
ls_evnt-name = in_name.
ls_evnt-form = in_form.
append ls_evnt to gt_evnt.
endif.
endmethod. "set_alv_event
***
* Add event
***
method set_all_events.
data: lt_trig type table of rtrig,
ls_evnt type slis_alv_event.

call function 'REUSE_ALV_EVENTS_GET'


exporting
i_list_type = g_table_type
importing
et_events = gt_evnt
exceptions
list_type_wrong = 1
others = 2.
if sy-subrc = 0.
* Get program form routines
load report g_repid part 'TRIG' into lt_trig.
* List of valid form routines
loop at gt_evnt into ls_evnt.
concatenate 'F_' ls_evnt-name into ls_evnt-form.
if not g_block_mode is initial.
concatenate ls_evnt-form me->g_table into ls_evnt-form
separated by '_'.
endif.
read table lt_trig with key exto = ls_evnt-form
fform = 'X'
transporting no fields.
if sy-subrc = 0.
modify gt_evnt from ls_evnt transporting form.
else.
delete gt_evnt.
endif.
endloop.
endif.

endmethod. "set_all_events
***
* Map fields from incoming structure into attribute
***
method map_structure.
data: ob_desc type ref to cl_abap_structdescr,
ls_compo type abap_compdescr.

field-symbols: <field> type any,


<struc> type any.

ob_desc ?= cl_abap_typedescr=>describe_by_data( destin ).

loop at ob_desc->components into ls_compo.


assign component ls_compo-name of structure source to <field>.
if <field> is assigned.
assign component ls_compo-name of structure destin to <struc>.
catch system-exceptions conversion_errors = 1.
move <field> to <struc>.
endcatch.
unassign <field>.
endif.
endloop.
endmethod. "map_structure
***
* Get default variant
***
method get_default_variant.
g_variant_level = g_variant_level + 1.

out_variant-report = me->g_repid.
out_variant-handle = me->g_variant_level.
out_variant-username = sy-uname.
call function 'REUSE_ALV_VARIANT_DEFAULT_GET'
exporting
i_save = 'U'
changing
cs_variant = out_variant
exceptions
others = 0.
endmethod. "get_default_variant
endclass. "lcl_table_display IMPLEMENTATION

[edit]

Links

26. Program to download file to application server and


then send mail to user to download it in presentation
server

*&---------------------------------------------------------------------*
*& Form download_to_application
*& download to application server, attach to mail and send to user
*&---------------------------------------------------------------------*
FORM download_to_application.

data: l_title type SO_OBJ_DES.

l_title = sy-repid.

CALL FUNCTION 'ZSEND_REPORT_MAIL'


EXPORTING
i_title = l_title
tables
it_text_data = it_download
EXCEPTIONS
INVALID_USER = 1
MAIL_SEND_ERROR = 2
OPEN_FILE = 3
FILE_GET_NAME = 4
OTHERS = 5
.
IF sy-subrc <> 0.
MESSAGE e368(00) with 'ZSEND_REPORT_MAIL fail -'
sy-subrc.
ENDIF.

ENDFORM. " download_to_application


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

FUNCTION Zsend_report_mail.
*"----------------------------------------------------------------------
*"*"Local interface:
*" IMPORTING
*" REFERENCE(I_UNAME) TYPE SYUNAME DEFAULT SY-UNAME
*" REFERENCE(I_TITLE) TYPE SO_OBJ_DES
*" TABLES
*" IT_TEXT_DATA
*" EXCEPTIONS
*" INVALID_USER
*" MAIL_SEND_ERROR
*" OPEN_FILE
*" FILE_GET_NAME
*"----------------------------------------------------------------------

* This function is used to store a file (report result) on the file


* system of the application server and to send an "active" SAP mail
* to the user.
* The "active" SAP mail calls function ZMAIL_DOWNLOAD to
* download the file to the presentation server.

DATA: ls_document_data LIKE sodocchgi1.


DATA: lt_object_para LIKE soparai1 OCCURS 5 WITH HEADER LINE.
DATA: lt_object_parb LIKE soparbi1 OCCURS 0 WITH HEADER LINE.
DATA: lt_object_cont LIKE solisti1 OCCURS 0 WITH HEADER LINE.
DATA: lt_reclist LIKE somlreci1 OCCURS 5 WITH HEADER LINE.

DATA: l_filename(200) TYPE c.

CHECK NOT it_text_data[] IS INITIAL.

* terminate if name not suitable.


IF i_uname IS INITIAL OR
i_uname = 'WF-BATCH' OR
i_uname = 'DDIC' OR
i_uname = 'SAP*'.
RAISE invalid_user.
ENDIF.

* get physical file from logical filename( optional)

CALL FUNCTION 'FILE_GET_NAME'


EXPORTING
logical_filename = 'MAILFILE'
parameter_1 = sy-uname
parameter_2 = sy-datum
parameter_3 = sy-uzeit
* USE_PRESENTATION_SERVER = ' '
* WITH_FILE_EXTENSION = ' '
* USE_BUFFER = ' '
* ELEMINATE_BLANKS = 'X'
IMPORTING
* EMERGENCY_FLAG =
* FILE_FORMAT =
file_name = l_filename
EXCEPTIONS
file_not_found = 1
OTHERS = 2.
IF sy-subrc NE 0.
RAISE file_get_name.
ENDIF.

* save file
OPEN DATASET l_filename FOR OUTPUT IN TEXT MODE.
IF sy-subrc NE 0.
RAISE open_file.
ENDIF.
LOOP AT it_text_data. " into l_text_data.
TRANSFER it_text_data TO l_filename.
ENDLOOP.
CLOSE DATASET l_filename.

* SAP mail header (execute function)


CLEAR: ls_document_data.
ls_document_data-obj_descr = i_title.
ls_document_data-proc_type = 'F'. " function call
ls_document_data-proc_name = 'ZMAIL_DOWNLOAD'.
ls_document_data-no_change = 'X'.
* SAP mail receiver
REFRESH lt_reclist.
CLEAR lt_reclist.
lt_reclist-receiver = i_uname.
lt_reclist-rec_type = 'B'.
* gt_reclist-express = 'X'.
APPEND lt_reclist.
* message text
REFRESH lt_object_cont.
CLEAR lt_object_cont.
lt_object_cont-line =
'The result of the following report has been saved.'.
APPEND lt_object_cont.
* report name
lt_object_cont-line = 'Report:'.
lt_object_cont-line+15 = sy-cprog.
CONDENSE lt_object_cont-line.
APPEND lt_object_cont.
* date & time
lt_object_cont-line = 'Date/Time:'.
WRITE sy-datum TO lt_object_cont-line+15.
WRITE sy-uzeit TO lt_object_cont-line+27.
CONDENSE lt_object_cont-line.
APPEND lt_object_cont.
*
lt_object_cont-line =
'Please execute (Ctrl-F6) this mail to download the result.'.
APPEND lt_object_cont.

* mail parameters
REFRESH lt_object_parb.
CLEAR lt_object_parb.
lt_object_parb-name = 'FUNCTION'. " mail identifier
lt_object_parb-value = 'FILE_DOWNLOAD'. " mail identifier
APPEND lt_object_parb.
lt_object_parb-name = 'FILENAME'.
lt_object_parb-value = l_filename.
APPEND lt_object_parb.

*call SAPOffice API


CALL FUNCTION 'SO_NEW_DOCUMENT_SEND_API1'
EXPORTING
document_data = ls_document_data
TABLES
object_content = lt_object_cont
object_para = lt_object_para
object_parb = lt_object_parb
receivers = lt_reclist
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.
IF sy-subrc NE 0.
RAISE mail_send_error.
ENDIF.

ENDFUNCTION.
========================================================
===================
FUNCTION zmail_download.
*"----------------------------------------------------------------------
*"*"Local interface:
*" TABLES
*" MSGDIAL STRUCTURE SOPARBI1
*"----------------------------------------------------------------------
* This function is called in a SAP mail to download a file from the
* application server file system.
* Function ZSEND_REPORT_MAIL is used to save report result
* on application server file system and to send SAP mail to user.
* Based on UK COM solution by Damian Norton.

DATA: ls_msgdial TYPE soparbi1.


DATA: l_filename TYPE filep.
* DATA: l_filename_local TYPE filep.
DATA: l_operation(30) TYPE c.
DATA: BEGIN OF lt_text_data OCCURS 10,
line(2000),
END OF lt_text_data.

* read parameters
LOOP AT msgdial INTO ls_msgdial.
CASE ls_msgdial-name.
WHEN 'FUNCTION'.
l_operation = ls_msgdial-value.
WHEN 'FILENAME'.
l_filename = ls_msgdial-value.
WHEN OTHERS.
MESSAGE e368(00) WITH 'Invalid parameter' ls_msgdial-name.
ENDCASE. " ls_msgdial-name
ENDLOOP. " msgdial

IF l_operation = 'FILE_DOWNLOAD'.
* check, whether file exists on presentation server
REFRESH lt_text_data.
OPEN DATASET l_filename FOR INPUT IN TEXT MODE.
IF sy-subrc = 0.
DO.
READ DATASET l_filename INTO lt_text_data-line.
IF sy-subrc = 0.
APPEND lt_text_data.
ELSE.
EXIT.
ENDIF.
ENDDO.
CLOSE DATASET l_filename.
* request filename on presentation server - or GUI_DOWNLOAD??
CALL FUNCTION 'DOWNLOAD'
TABLES
data_tab = lt_text_data
EXCEPTIONS
invalid_filesize = 1
invalid_table_width = 2
invalid_type = 3
no_batch = 4
unknown_error = 5
gui_refuse_filetransfer = 6
customer_error = 7
OTHERS = 8.
IF sy-subrc NE 0.
MESSAGE e688(00) WITH 'File download error' sy-subrc.
ENDIF.
ELSE.
MESSAGE e398(00) WITH 'File open error' l_filename.
ENDIF. " sy-subrc = 0 (OPEN DATASET)
ENDIF. " l_operation = 'FILE_DOWNLOAD'
ENDFUNCTION.

Program which create job and specify more details

REPORT ZT401212.
*DATA : BEGDATE TYPE DATUM VALUE '20091001'.
*data : lr_value1 type ztsdmp_result,
* lr_uname type ztsdmp_result with header line,
* wa_value1 TYPE zlsdmp_result.
*
*
*SELECT VALUE1 AS low FROM ZENHDATA
*INTO CORRESPONDING FIELDS OF TABLE lr_value1
*WHERE ZENH = 'E000000198'
*AND active EQ 'X'.
*clear wa_value1.
*
*LOOP AT lr_value1 INTO wa_value1.
*
* lr_uname-sign = 'I'. lr_uname-option = 'EQ'. lr_uname-low = wa_val
ue1-low.
* append lr_uname.
*ENDLOOP.
*IF sy-uname IN lr_uname.
*BEGDATE(4) = BEGDATE(4) - 1.
*ENDIF.
data : wa_control_rec type EDIDC,
lv_pathname type EDI_PATH-PTHNAM.
wa_control_rec-docnum = '0000000000767117'.
wa_control_rec-mandt = sy-mandt.

CALL FUNCTION 'EDI_PATH_NAME_OUT'


EXPORTING
PORT = 'ZCROP'
CONTROLREC = wa_control_rec
IMPORTING
PATHNAME = lv_pathname.
data : i_sel1 TYPE TABLE OF rsparams,
wa_sel type rsparams.

wa_sel-selname = 'HOST'. "Parameter P_object


wa_sel-kind = 'P'. "Parameter
wa_sel-low = '168.246.56.45'.
append wa_sel to i_sel1.
wa_sel-selname = 'USER'. "Parameter P_object
wa_sel-kind = 'P'. "Parameter
wa_sel-low = 'anonymous'.
append wa_sel to i_sel1.
wa_sel-selname = 'PASSWD'. "Parameter P_object
wa_sel-kind = 'P'. "Parameter
wa_sel-low = 'anonymous'.
append wa_sel to i_sel1.
wa_sel-selname = 'CMDS'. "Parameter P_object
wa_sel-kind = 'S'.
wa_sel-sign = 'I'.
wa_sel-option = 'EQ'.
If sy-sysid eq 'MSP'.
wa_sel-low = 'cd seedsftp/ShipNoticeIDOCs'.
ELSEIF SY-SYSID EQ 'DEV' OR SY-SYSID EQ 'TRI'.
wa_sel-low = 'cd seedsftp/ShipNoticeIDOCs_test'.
endif.
append wa_sel to i_sel1.
wa_sel-selname = 'CMDS'. "Parameter P_object
wa_sel-kind = 'S'. "Parameter
wa_sel-sign = 'I'.
wa_sel-option = 'EQ'.
wa_sel-low = 'append[]'.
append wa_sel to i_sel1.
wa_sel-selname = 'CMDS'. "Parameter P_object
wa_sel-kind = 'S'. "Parameter
wa_sel-sign = 'I'.
wa_sel-option = 'EQ'.
CONCATENATE lv_pathname(22) '[' into wa_sel-low.
append wa_sel to i_sel1.
wa_sel-selname = 'CMDS'. "Parameter P_object
wa_sel-kind = 'S'. "Parameter
wa_sel-sign = 'I'.
wa_sel-option = 'EQ'.
CONCATENATE lv_pathname+22(40) '[' into wa_sel-low.
append wa_sel to i_sel1.
wa_sel-selname = 'CMDS'. "Parameter P_object
wa_sel-kind = 'S'. "Parameter
wa_sel-sign = 'I'.
wa_sel-option = 'EQ'.
move '[]' to wa_sel-low.
append wa_sel to i_sel1.
wa_sel-selname = 'CMDS'. "Parameter P_object
wa_sel-kind = 'S'. "Parameter
wa_sel-sign = 'I'.
wa_sel-option = 'EQ'.
concatenate 'shipment_idoc-' lv_pathname+40(21) '.dat' into wa_
sel-low.
append wa_sel to i_sel1.
data : lv_count LIKE tbtcjob-jobcount.

CALL FUNCTION 'JOB_OPEN'


EXPORTING
* DELANFREP = ' '
* JOBGROUP = ' '
JOBNAME = 'zftpcall_ship_notice'
SDLSTRTDT = sy-datum
SDLSTRTTM = sy-uzeit
* JOBCLASS =
IMPORTING
JOBCOUNT = lv_count
* CHANGING
* RET =
* EXCEPTIONS
* CANT_CREATE_JOB = 1
* INVALID_JOB_DATA = 2
* JOBNAME_MISSING = 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.

*submit zftpcall_ship_notice to sap-spool without spool dynpro


*with selection-table i_sel1 user 'ZNAFTA' VIA JOB 'zftpcall_ship_notic
e' number lv_count
*with operatingsystem ''
*and return.
data : ls_varid LIKE varid,
lit_vari_text LIKE varit OCCURS 0 WITH HEADER LINE.

CLEAR lit_vari_text.
lit_vari_text-langu = sy-langu.
lit_vari_text-report = 'ZFTPCALL'.
lit_vari_text-variant = 'ship_notice'.
lit_vari_text-vtext = 'ship notice variant'.
APPEND lit_vari_text.

*.check variant
DATA: rc LIKE sy-subrc.
CALL FUNCTION 'RS_VARIANT_EXISTS'
EXPORTING
report = 'ZFTPCALL'
variant = 'ship_notice'
IMPORTING
r_c = rc
EXCEPTIONS
not_authorized = 01
no_report = 02
report_not_existent = 03
report_not_supplied = 04.

IF sy-subrc <> 0.
rc = 8.
ENDIF.

if rc = 0.

CLEAR ls_varid.
ls_varid-report = 'ZFTPCALL'.
ls_varid-variant = 'ship_notice'.
* ls_varid-transport = 'F'.
* ls_varid-environmnt = 'A'.
* ADD 1 TO ls_varid-version .

ls_varid-ename = sy-uname.
ls_varid-edat = sy-datum.
ls_varid-etime = sy-uzeit.
ls_varid-mlangu = sy-langu.
ADD 1 TO ls_varid-version.
CALL FUNCTION 'RS_CHANGE_CREATED_VARIANT'
EXPORTING
curr_report = 'ZFTPCALL'
curr_variant = 'ship_notice'
vari_desc = ls_varid
ONLY_CONTENTS = 'Y'
TABLES
vari_contents = i_sel1
vari_text = lit_vari_text
EXCEPTIONS
illegal_report_or_variant = 01
illegal_variantname = 02
not_authorized = 03
not_executed = 04
report_not_existent = 05
report_not_supplied = 06
variant_doesnt_exist = 07
variant_locked = 08
selections_no_match = 09.

COMMIT WORK.

else.

CALL FUNCTION 'RS_CREATE_VARIANT'


EXPORTING
CURR_REPORT = 'ZFTPCALL'
CURR_VARIANT = 'ship_notice'
VARI_DESC = ls_varid
TABLES
VARI_CONTENTS = i_sel1
VARI_TEXT = lit_vari_text
* VSCREENS =
* EXCEPTIONS
* ILLEGAL_REPORT_OR_VARIANT = 1
* ILLEGAL_VARIANTNAME = 2
* NOT_AUTHORIZED = 3
* NOT_EXECUTED = 4
* REPORT_NOT_EXISTENT = 5
* REPORT_NOT_SUPPLIED = 6
* VARIANT_EXISTS = 7
* VARIANT_LOCKED = 8
* OTHERS = 9
.

endif.
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 'JOB_SUBMIT'
EXPORTING
* ARCPARAMS =
AUTHCKNAM = 'ZBTCNAFTA'
* COMMANDNAME = ' '
* OPERATINGSYSTEM = 'chwsxd34_DEV_00'
* EXTPGM_NAME = ' '
* EXTPGM_PARAM = ' '
* EXTPGM_SET_TRACE_ON = ' '
* EXTPGM_STDERR_IN_JOBLOG = 'X'
* EXTPGM_STDOUT_IN_JOBLOG = 'X'
* EXTPGM_SYSTEM = 'chwsxd34_DEV_00'
* EXTPGM_RFCDEST = 'chwsxd34_DEV_00'
* EXTPGM_WAIT_FOR_TERMINATION = 'X'
JOBCOUNT = lv_count
JOBNAME = 'zftpcall_ship_notice'
LANGUAGE = sy-langu
* PRIPARAMS = ' '
REPORT = 'ZFTPCALL'
VARIANT = 'ship_notice'
* IMPORTING
* STEP_NUMBER =
* EXCEPTIONS
* BAD_PRIPARAMS = 1
* BAD_XPGFLAGS = 2
* INVALID_JOBDATA = 3
* JOBNAME_MISSING = 4
* JOB_NOTEX = 5
* JOB_SUBMIT_FAILED = 6
* LOCK_FAILED = 7
* PROGRAM_MISSING = 8
* PROG_ABAP_AND_EXTPG_SET = 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.
data : target_server like BTCTGTSRVR-SRVNAME.
if sy-sysid eq 'MSP'.
target_server = 'sapmspci_MSP_00'.
elseif sy-sysid eq 'TRI'.
target_server = 'saptria1_TRI_01'.
elseif sy-sysid eq 'DEV'.
target_server = 'chwsxd34_DEV_00'.
endif.

CALL FUNCTION 'JOB_CLOSE'


EXPORTING
* AT_OPMODE = ' '
* AT_OPMODE_PERIODIC = ' '
* CALENDAR_ID = ' '
* EVENT_ID = ' '
* EVENT_PARAM = ' '
* EVENT_PERIODIC = ' '
JOBCOUNT = lv_count
JOBNAME = 'zftpcall_ship_notice'
* LASTSTRTDT = NO_DATE
* LASTSTRTTM = NO_TIME
* PRDDAYS = 0
* PRDHOURS = 0
* PRDMINS = 0
* PRDMONTHS = 0
* PRDWEEKS = 0
* PREDJOB_CHECKSTAT = ' '
* PRED_JOBCOUNT = ' '
* PRED_JOBNAME = ' '
SDLSTRTDT = SY-DATUM
SDLSTRTTM = SY-UZEIT
* STARTDATE_RESTRICTION = BTC_PROCESS_ALWAYS
STRTIMMED = 'X'
TARGETSYSTEM = 'X'
* START_ON_WORKDAY_NOT_BEFORE = SY-DATUM
* START_ON_WORKDAY_NR = 0
* WORKDAY_COUNT_DIRECTION = 0
* RECIPIENT_OBJ =
TARGETSERVER = target_server
* DONT_RELEASE = ' '
* TARGETGROUP = ' '
* DIRECT_START =
* IMPORTING
* JOB_WAS_RELEASED =
* CHANGING
* RET =
* EXCEPTIONS
* CANT_START_IMMEDIATE = 1
* INVALID_STARTDATE = 2
* JOBNAME_MISSING = 3
* JOB_CLOSE_FAILED = 4
* JOB_NOSTEPS = 5
* JOB_NOTEX = 6
* LOCK_FAILED = 7
* INVALID_TARGET = 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.

Program to convert spool to pdf


RSTXPDFT4
REPORT RSTXPDFT4 line-size 80.
*
* Read spool job contents (OTF or ABAP list) and convert
* to PDF, download PDF
* B20K8A0IKH replace WS_DOWNLOAD with GUI_DOWNLOAD
*
PARAMETERS:
SPOOLNO LIKE TSP01-RQIDENT,
DOWNLOAD AS CHECKBOX DEFAULT 'X',
P_FILE LIKE RLGRAP-FILENAME DEFAULT 'C:\temp\file.pdf'. "#EC NO
TEXT
DATA otf like itcoo occurs 100 with header line.
DATA CANCEL.
DATA PDF LIKE TLINE OCCURS 100 WITH HEADER LINE.
DATA DOCTAB LIKE DOCS OCCURS 1 WITH HEADER LINE.
DATA: NUMBYTES TYPE I,
ARC_IDX LIKE TOA_DARA,
pdfspoolid like tsp01-rqident,
jobname like tbtcjob-jobname,
jobcount like tbtcjob-jobcount,
is_otf.
data: client like tst01-dclient,
name like tst01-dname,
objtype like rststype-type,
type like rststype-type.
tables: tsp01.

select single * from tsp01 where rqident = spoolno.


if sy-subrc <> 0.
WRITE: / 'Spoolauftrag existiert nicht'(003)
COLOR COL_negative.
exit.
endif.
client = tsp01-rqclient.
name = tsp01-rqo1name.
CALL FUNCTION 'RSTS_GET_ATTRIBUTES'
EXPORTING
AUTHORITY = 'SP01'
CLIENT = client
NAME = name
PART = 1
IMPORTING
* CHARCO =
* CREATER =
* CREDATE =
* DELDATE =
* MAX_CREDATE =
* MAX_DELDATE =
* NON_UNIQ =
* NOOF_PARTS =
* RECTYP =
* SIZE =
* STOTYP =
TYPE = type
OBJTYPE = objtype
EXCEPTIONS
FB_ERROR = 1
FB_RSTS_OTHER = 2
NO_OBJECT = 3
NO_PERMISSION = 4.
if objtype(3) = 'OTF'.
is_otf = 'X'.
else.
is_otf = space.
endif.
if is_otf = 'X'.
CALL FUNCTION 'CONVERT_OTFSPOOLJOB_2_PDF'
EXPORTING
SRC_SPOOLID = spoolno
NO_DIALOG = ' '
* DST_DEVICE =
* PDF_DESTINATION =
IMPORTING
PDF_BYTECOUNT = numbytes
PDF_SPOOLID = pdfspoolid
* OTF_PAGECOUNT =
BTC_JOBNAME = jobname
BTC_JOBCOUNT = jobcount
TABLES
PDF = pdf
EXCEPTIONS
ERR_NO_OTF_SPOOLJOB = 1
ERR_NO_SPOOLJOB = 2
ERR_NO_PERMISSION = 3
ERR_CONV_NOT_POSSIBLE = 4
ERR_BAD_DSTDEVICE = 5
USER_CANCELLED = 6
ERR_SPOOLERROR = 7
ERR_TEMSEERROR = 8
ERR_BTCJOB_OPEN_FAILED = 9
ERR_BTCJOB_SUBMIT_FAILED = 10
ERR_BTCJOB_CLOSE_FAILED = 11.
case sy-subrc.
when 0.
WRITE: / 'Funktion CONVERT_OTFSPOOLJOB_2_PDF erfolgreich'(001)
COLOR COL_POSITIVE.
when 1.
WRITE: / 'Kein OTF- und kein ABAP-Spoolauftrag'(002)
COLOR COL_negative.
exit.
when 2.
WRITE: / 'Spoolauftrag existiert nicht'(003)
COLOR COL_negative.
exit.
when 3.
WRITE: / 'Keine Berechtigung zum Lesen Spoolauftrag'(004)
COLOR COL_negative.
exit.
when others.
WRITE: / 'Fehler bei Funktion CONVERT_OTFSPOOLJOB_2_PDF'(005)
COLOR COL_negative.
exit.
endcase.
else.
CALL FUNCTION 'CONVERT_ABAPSPOOLJOB_2_PDF'
EXPORTING
SRC_SPOOLID = spoolno
NO_DIALOG = ' '
* DST_DEVICE =
* PDF_DESTINATION =
IMPORTING
PDF_BYTECOUNT = numbytes
PDF_SPOOLID = pdfspoolid
* LIST_PAGECOUNT =
BTC_JOBNAME = jobname
BTC_JOBCOUNT = jobcount
TABLES
PDF = pdf
EXCEPTIONS
ERR_NO_ABAP_SPOOLJOB = 1
ERR_NO_SPOOLJOB = 2
ERR_NO_PERMISSION = 3
ERR_CONV_NOT_POSSIBLE = 4
ERR_BAD_DESTDEVICE = 5
USER_CANCELLED = 6
ERR_SPOOLERROR = 7
ERR_TEMSEERROR = 8
ERR_BTCJOB_OPEN_FAILED = 9
ERR_BTCJOB_SUBMIT_FAILED = 10
ERR_BTCJOB_CLOSE_FAILED = 11.
case sy-subrc.
when 0.
WRITE: / 'Funktion CONVERT_ABAPSPOOLJOB_2_PDF erfolgreich'(006
)
COLOR COL_POSITIVE.
when 1.
WRITE: / 'Kein OTF- und kein ABAP-Spoolauftrag'(002)
COLOR COL_negative.
exit.
when 2.
WRITE: / 'Spoolauftrag existiert nicht'(003)
COLOR COL_negative.
exit.
when 3.
WRITE: / 'Keine Berechtigung zum Lesen Spoolauftrag'(004)
COLOR COL_negative.
exit.
when others.
WRITE: / 'Fehler bei Funktion CONVERT_ABAPSPOOLJOB_2_PDF'(007)
COLOR COL_negative.
exit.
endcase.
endif.
*************** download PDF file ***********
check download = 'X'.
if not ( jobname is initial ).
WRITE: / 'Konvertierung per Hintergrundjob'(008)
COLOR COL_normal,
jobname, jobcount.
exit.
endif.
perform download_w_ext(RSTXPDFT) tables pdf
using p_file
'.pdf'
'BIN'
numbytes
cancel.
if cancel = space.
WRITE: / NUMBYTES, 'Bytes heruntergeladen in Datei'(009), P_FIL
E.
endif.

Subroutine to prevent time out error


PERFORM set_progress USING text-m01
PERFORM set_progress USING text-m03.
FORM set_progress USING p_text.

MESSAGE s032 WITH p_text.


set_progress sy-msgid sy-msgno sy-msgv1
sy-msgv2 sy-msgv3 sy-msgv4
progress_count max_progress_count step_size.
COMMIT WORK.

ENDFORM. " set_progress