Catatan belajar ABAP.
Terkadang functional meminta ABAPer untuk membuat program berdasarkan data output keluaran dari report standard SAP. Jika membuat program dengan logika sendiri dari awal tentu sangat memakan waktu dan usaha yang banyak. Solusi yang paling cepat adalah dengan melakukan teknik pengambilan data dari report standard yang bersangkutan dan memindahkannya ke program kita. Apabila report standard tersebut berupa ALV, maka solusi yang bisa digunakan adalah menggunakan teknik SUBMIT PROGRAM and RETURN dan memanfaatkan class CL_SALV_BS_RUNTIME_INFO.
Mengenai Class CL_SALV_BS_RUNTIME_INFO.
Saat report ALV dijalankan, informasi ALV runtime (layout, fieldcat, keyfield, filter, dll ditambah tabel data) disimpan oleh metode kelas CL_SALV_BS_RUNTIME_INFO untuk digunakan di proses berikutnya. Melalui statement SUBMIT dan beberapa pemanggilan method, kita bisa melakukan akses langsung ke data table (itab data) dari report tersebut TANPA harus mengubah report aslinya. menarik bukan?
Contoh penggalan kode program untuk mengambil nilai output dari report h99cwtr0 :
FIELD-SYMBOLS <lt_pay_data> TYPE ANY TABLE.
DATA lr_pay_data TYPE REF TO data.
cl_salv_bs_runtime_info=>set(
EXPORTING display = abap_false
metadata = abap_false
data = abap_true ).
* Submit Wage Type Reporter
SUBMIT h99cwtr0
WITH SELECTION-TABLE lt_selscreen
AND RETURN.
TRY.
cl_salv_bs_runtime_info=>get_data_ref(
IMPORTING r_data = lr_pay_data ).
ASSIGN lr_pay_data->* TO <lt_pay_data>.
CATCH cx_salv_bs_sc_runtime_info.
MESSAGE `Unable to retrieve ALV data` TYPE 'E'.
ENDTRY.
cl_salv_bs_runtime_info=>clear_all( ).
Berdasarkan contoh diatas berikut adalah contoh program untuk mengambil data pada standard report tcode S_P00_07000139.
REPORT zerv01.
TABLES : mara, mseg, bkpf, mkpf.
TYPE-POOLS : slis.
DATA: BEGIN OF ty_data OCCURS 100,
bukrs LIKE t001-bukrs,
bwkey LIKE mbew-bwkey,
werks LIKE mseg-werks,
matnr LIKE mseg-matnr,
lgort LIKE mseg-lgort,
endmenge LIKE mard-labst, "stock to 'datum-high'
anfmenge LIKE mard-labst, "stock to 'datum-low'
jendmenge LIKE mard-labst, "Step back 'datum-high'
janfmenge LIKE mard-labst, "Step back 'datum-low'
mtart LIKE mara-mtart,
sobkz LIKE mseg-sobkz,
prcwert LIKE mbew-salk3,
rtnmenge LIKE mard-labst,
trnmenge LIKE mard-labst,
totmenge LIKE mard-labst,
maktx LIKE makt-maktx,
meins LIKE mara-meins, "UOM
endwert LIKE mbew-salk3, "value to 'datum-high'
anfwert LIKE mbew-salk3, "value to 'datum-low'
soll LIKE mseg-menge,
haben LIKE mseg-menge,
sollwert LIKE mbew-salk3,
habenwert LIKE mbew-salk3,
waers LIKE t001-waers,
END OF ty_data.
FIELD-SYMBOLS <lt_pay_data> TYPE ANY TABLE.
DATA :lr_pay_data TYPE REF TO data,
lt_selscreen TYPE TABLE OF rsparams WITH HEADER LINE,
lt_data LIKE ty_data OCCURS 0 WITH HEADER LINE.
DATA: lt_fc TYPE slis_t_fieldcat_alv.
DATA: wa_fc TYPE slis_fieldcat_alv.
SELECTION-SCREEN BEGIN OF BLOCK b1 WITH FRAME TITLE txt_1.
SELECTION-SCREEN BEGIN OF LINE.
SELECTION-SCREEN COMMENT (10) txt_2 FOR FIELD so_matnr.
SELECT-OPTIONS : so_matnr FOR mara-matnr OBLIGATORY.
SELECTION-SCREEN END OF LINE.
SELECTION-SCREEN BEGIN OF LINE.
SELECTION-SCREEN COMMENT (10) txt_3 FOR FIELD so_werks.
SELECT-OPTIONS : so_werks FOR mseg-werks OBLIGATORY.
SELECTION-SCREEN END OF LINE.
SELECTION-SCREEN BEGIN OF LINE.
SELECTION-SCREEN COMMENT (10) txt_4 FOR FIELD so_bukrs.
SELECT-OPTIONS : so_bukrs FOR bkpf-bukrs OBLIGATORY.
SELECTION-SCREEN END OF LINE.
SELECTION-SCREEN BEGIN OF LINE.
SELECTION-SCREEN COMMENT (10) txt_5 FOR FIELD so_datum.
SELECT-OPTIONS : so_datum FOR mkpf-budat OBLIGATORY NO-EXTENSION.
SELECTION-SCREEN END OF LINE.
SELECTION-SCREEN END OF BLOCK b1.
INITIALIZATION.
txt_1 = 'Parameter'.
txt_2 = 'Material'.
txt_3 = 'Plant'.
txt_4 = 'Company'.
txt_5 = 'Date'.
START-OF-SELECTION.
PERFORM fm_set_param.
PERFORM fm_submit_program.
PERFORM fm_set_fieldcat.
PERFORM fm_display_alv.
*&---------------------------------------------------------------------*
*& Form fm_display_alv
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
FORM fm_display_alv.
CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
EXPORTING
i_callback_program = sy-repid
i_save = 'X'
it_fieldcat = lt_fc
TABLES
t_outtab = lt_data
EXCEPTIONS
program_error = 1
OTHERS = 2.
ENDFORM. "fm_display_alv
*&---------------------------------------------------------------------*
*& Form fm_set_param
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
FORM fm_set_param.
lt_selscreen-selname = 'BM_MATNR'.
lt_selscreen-kind = 'S'.
LOOP AT so_matnr.
lt_selscreen-sign = so_matnr-sign.
lt_selscreen-option = so_matnr-option.
lt_selscreen-low = so_matnr-low.
lt_selscreen-high = so_matnr-high.
APPEND lt_selscreen. CLEAR lt_selscreen.
ENDLOOP.
lt_selscreen-selname = 'BM_WERKS'.
lt_selscreen-kind = 'S'.
LOOP AT so_werks.
lt_selscreen-sign = so_werks-sign.
lt_selscreen-option = so_werks-option.
lt_selscreen-low = so_werks-low.
lt_selscreen-high = so_werks-high.
APPEND lt_selscreen. CLEAR lt_selscreen.
ENDLOOP.
lt_selscreen-selname = 'BM_BUKRS'.
lt_selscreen-kind = 'S'.
LOOP AT so_bukrs.
lt_selscreen-sign = 'I'.
lt_selscreen-option = 'EQ'.
lt_selscreen-low = so_bukrs-low.
lt_selscreen-high = so_bukrs-high.
APPEND lt_selscreen. CLEAR lt_selscreen.
ENDLOOP.
lt_selscreen-selname = 'DATUM'.
lt_selscreen-kind = 'S'.
lt_selscreen-sign = 'I'.
lt_selscreen-option = 'BT'.
lt_selscreen-low = so_datum-low.
lt_selscreen-high = so_datum-high.
APPEND lt_selscreen. CLEAR lt_selscreen.
lt_selscreen-selname = 'BWBST'.
lt_selscreen-kind = 'P'.
lt_selscreen-option = 'EQ'.
lt_selscreen-low = ''.
APPEND lt_selscreen. CLEAR lt_selscreen.
lt_selscreen-selname = 'S_RPT2'.
lt_selscreen-kind = 'P'.
lt_selscreen-option = 'EQ'.
lt_selscreen-low = 'X'.
APPEND lt_selscreen. CLEAR lt_selscreen.
ENDFORM. "fm_submit_param
*&---------------------------------------------------------------------*
*& Form fm_submit_program
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
FORM fm_submit_program.
cl_salv_bs_runtime_info=>set(
EXPORTING display = abap_false
metadata = abap_false
data = abap_true ).
* Submit Wage Type Reporter
SUBMIT j_1hstcd
WITH SELECTION-TABLE lt_selscreen
AND RETURN.
TRY.
cl_salv_bs_runtime_info=>get_data_ref(
IMPORTING r_data = lr_pay_data ).
ASSIGN lr_pay_data->* TO <lt_pay_data>.
CATCH cx_salv_bs_sc_runtime_info.
MESSAGE `Unable to retrieve ALV data` TYPE 'E'.
ENDTRY.
cl_salv_bs_runtime_info=>clear_all( ).
lt_data[] = <lt_pay_data>[].
ENDFORM. "fm_submit_program
*&---------------------------------------------------------------------*
*& Form fm_set_fieldcat
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
FORM fm_set_fieldcat.
REFRESH lt_fc.
CLEAR wa_fc.
wa_fc-reptext_ddic = 'Company'.
wa_fc-fieldname = 'BUKRS'.
wa_fc-tabname = 'LT_DATA'.
* wa_fc-outputlen = '18'.
APPEND wa_fc TO lt_fc.
CLEAR wa_fc.
wa_fc-reptext_ddic = 'Mat. no'.
wa_fc-fieldname = 'MATNR'.
wa_fc-tabname = 'LT_DATA'.
wa_fc-ref_fieldname = 'MATNR'.
wa_fc-ref_tabname = 'MARA'.
wa_fc-outputlen = '18'.
APPEND wa_fc TO lt_fc.
CLEAR wa_fc.
wa_fc-reptext_ddic = 'Mat. Desc'.
wa_fc-fieldname = 'MAKTX'.
wa_fc-tabname = 'LT_DATA'.
wa_fc-outputlen = '40'.
APPEND wa_fc TO lt_fc.
CLEAR wa_fc.
wa_fc-reptext_ddic = 'Price Per Unit'.
wa_fc-fieldname = 'PRCWERT'.
wa_fc-tabname = 'LT_DATA'.
wa_fc-cfieldname = 'WAERS'.
* wa_fc-outputlen = '18'.
APPEND wa_fc TO lt_fc.
CLEAR wa_fc.
wa_fc-reptext_ddic = 'Curr'.
wa_fc-fieldname = 'WAERS'.
wa_fc-tabname = 'LT_DATA'.
* wa_fc-outputlen = '18'.
APPEND wa_fc TO lt_fc.
ENDFORM. "fm_set_fieldcat
Reference :
No comments:
Post a Comment