Update price planning T-code KP26 with the BDC - Call Transaction Method
"""""""""""""""""""""""""""""""""""" SAMPLE CODE BDC """"""""""""""""""""""
REPORT zkp26
NO STANDARD PAGE HEADING LINE-SIZE 255.
TYPES: BEGIN OF ty_final,
version TYPE versn,
from_period TYPE kpla_perbl,
to_period TYPE kpla_perbl,
fiscal_year TYPE gjahr,
cost_cntr TYPE kostl,
act_typ TYPE lstar,
variable_price TYPE cost-tog012,
END OF ty_final.
DATA : it_final TYPE STANDARD TABLE OF ty_final, it_final2 TYPE STANDARD TABLE OF ty_final,
wa_final TYPE ty_final.
DATA : wa_fieldcat TYPE slis_fieldcat_alv, "Field catalog work area
it_fieldcat TYPE slis_t_fieldcat_alv. "field catalog internal table
DATA: it_type TYPE truxs_t_text_data,
opt TYPE ctu_params,
lv_opt LIKE ctu_params.
DATA: it_bdcdata TYPE TABLE OF bdcdata,
wa_bdcdata TYPE bdcdata,
ctu TYPE ctu_params,
t_values TYPE vrm_values,
w_values TYPE vrm_value.
DATA: messtab LIKE bdcmsgcoll OCCURS 0 WITH HEADER LINE.
**DATA: L_MSTRING(480).
DATA: l_subrc LIKE sy-subrc,
tcode TYPE tcode VALUE 'KP26'.
TYPES : BEGIN OF ty_output,
object_number TYPE cost-objnr,
year TYPE gjahr,
desc(40) TYPE c,
END OF ty_output.
DATA: t_output TYPE STANDARD TABLE OF ty_output,
wa_output TYPE ty_output.
DATA: lv_name(6) TYPE c VALUE 'KL1000'.
DATA: lv_object TYPE cost-objnr.
INITIALIZATION.
PERFORM initialization.
SELECTION-SCREEN : BEGIN OF BLOCK b1 WITH FRAME TITLE TEXT-001.
PARAMETERS : p_file TYPE rlgrap-filename,
* PARAMETERS : p_file TYPE rlgrap-filename DEFAULT 'C:\Users\abapsu\Desktop\KP26.xlsx',
p_mode(1) AS LISTBOX VISIBLE LENGTH 20 DEFAULT 'E'.
* P_DEFSIZE LIKE CTU_PARAMS-DEFSIZE DEFAULT 'X'.
SELECTION-SCREEN : END OF BLOCK b1.
AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_mode.
PERFORM dismode.
AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_file.
PERFORM f4_file.
START-OF-SELECTION.
PERFORM file_upload.
PERFORM validate_data.
PERFORM upload_data.
PERFORM disp.
*&---------------------------------------------------------------------*
*& Form FILE_UPLOAD
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*& --> p1 text
*& <-- p2 text
*&---------------------------------------------------------------------*
FORM file_upload .
CALL FUNCTION 'TEXT_CONVERT_XLS_TO_SAP'
EXPORTING
i_field_seperator = 'X'
i_line_header = 'X'
i_tab_raw_data = it_type
i_filename = p_file
TABLES
i_tab_converted_data = it_final[]
EXCEPTIONS
conversion_failed = 1
OTHERS = 2.
IF sy-subrc <> 0.
MESSAGE 'TABLE IS INITIAL' TYPE 'E'.
ELSE.
ctu-dismode = p_mode. "DISPLAY MODE.
ctu-updmode = 'S'. "UPDATE MODE.
ctu-defsize = 'X'. "DEFAULT SIZE.
ENDIF.
ENDFORM.
*&---------------------------------------------------------------------*
*& Form UPLOAD_DATA
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*& --> p1 text
*& <-- p2 text
*&---------------------------------------------------------------------*
FORM upload_data .
LOOP AT it_final INTO wa_final.
*
* perform bdc_dynpro using 'SAPLSPO4' '0300'.
* perform bdc_field using 'BDC_CURSOR'
* 'SVALD-VALUE(01)'.
* perform bdc_field using 'BDC_OKCODE'
* '=FURT'.
* perform bdc_field using 'SVALD-VALUE(01)'
* '1000'.
PERFORM bdc_dynpro USING 'SAPLKPP0' '1000'.
PERFORM bdc_field USING 'BDC_CURSOR'
'KPP0B-VALUE(09)'.
PERFORM bdc_field USING 'BDC_OKCODE'
'=CSUB'.
PERFORM bdc_field USING 'KPP1B-ONLY'
'X'.
PERFORM bdc_field USING 'KPP0B-VALUE(01)'
wa_final-version.
PERFORM bdc_field USING 'KPP0B-VALUE(02)'
wa_final-from_period.
PERFORM bdc_field USING 'KPP0B-VALUE(03)'
wa_final-to_period .
PERFORM bdc_field USING 'KPP0B-VALUE(04)'
wa_final-fiscal_year.
PERFORM bdc_field USING 'KPP0B-VALUE(06)'
wa_final-cost_cntr .
PERFORM bdc_field USING 'KPP0B-VALUE(09)'
wa_final-act_typ .
PERFORM bdc_dynpro USING 'SAPLKPP2' '0112'.
PERFORM bdc_field USING 'BDC_CURSOR'
'Z-BDC08(01)'.
PERFORM bdc_field USING 'BDC_OKCODE'
'=CBUC'.
PERFORM bdc_field USING 'Z-BDC08(01)'
wa_final-variable_price.
PERFORM bdc_field USING 'BDC_OKCODE'
'=CBUC'.
REFRESH messtab.
CALL TRANSACTION tcode USING it_bdcdata OPTIONS FROM ctu MESSAGES INTO messtab.
IF messtab[] IS NOT INITIAL.
loop at messtab.
SELECT SINGLE * FROM T100 into @data(t_msg) WHERE ARBGB = @messtab-msgid AND MSGNR = @messtab-MSGNR.
CONCATENATE lv_name wa_final-cost_cntr wa_final-act_typ INTO lv_object.
wa_output-object_number = lv_object.
wa_output-year = wa_final-fiscal_year.
wa_output-desc = t_msg-text.
APPEND wa_output TO t_output.
CLEAR: wa_output.
ENDLOOP.
ELSE.
CONCATENATE lv_name wa_final-cost_cntr wa_final-act_typ INTO lv_object.
wa_output-object_number = lv_object.
wa_output-year = wa_final-fiscal_year.
wa_output-desc = 'Sucess'.
APPEND wa_output TO t_output.
CLEAR: wa_output.
ENDIF.
REFRESH : it_bdcdata[].
CLEAR wa_final.
ENDLOOP.
ENDFORM.
*&---------------------------------------------------------------------*
*& Form DISP
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*& --> p1 text
*& <-- p2 text
*&---------------------------------------------------------------------*
FORM disp .
wa_fieldcat-fieldname = 'OBJECT_NUMBER'.
wa_fieldcat-seltext_m = 'OBJECT NO.'.
wa_fieldcat-tabname = 'T_OUTPUT'.
wa_fieldcat-outputlen = 25.
APPEND wa_fieldcat TO it_fieldcat.
CLEAR wa_fieldcat.
wa_fieldcat-fieldname = 'YEAR'.
wa_fieldcat-seltext_m = 'FISCAL YEAR'.
wa_fieldcat-tabname = 'T_OUTPUT'.
wa_fieldcat-outputlen = 15.
APPEND wa_fieldcat TO it_fieldcat.
CLEAR wa_fieldcat.
wa_fieldcat-fieldname = 'DESC'.
wa_fieldcat-seltext_m = 'Status'.
wa_fieldcat-tabname = 'T_OUTPUT'.
wa_fieldcat-outputlen = 40.
APPEND wa_fieldcat TO it_fieldcat.
CLEAR wa_fieldcat.
CALL FUNCTION 'REUSE_ALV_GRID_DISPLAY'
EXPORTING
i_callback_program = sy-repid
it_fieldcat = it_fieldcat
TABLES
t_outtab = t_output[].
IF sy-subrc <> 0.
MESSAGE 'ALV List Can not be generated!!!' TYPE 'E'.
ENDIF.
ENDFORM.
*&---------------------------------------------------------------------*
*& Form DISMODE
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*& --> p1 text
*& <-- p2 text
*&---------------------------------------------------------------------*
FORM dismode .
CALL FUNCTION 'VRM_SET_VALUES'
EXPORTING
id = 'P_MODE'
values = t_values.
ENDFORM.
*&---------------------------------------------------------------------*
*& Form F4_FILE
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*& --> p1 text
*& <-- p2 text
*&---------------------------------------------------------------------*
FORM f4_file .
CALL FUNCTION 'F4_FILENAME'
EXPORTING
program_name = syst-cprog
dynpro_number = syst-dynnr
field_name = 'P_FILE'
IMPORTING
file_name = p_file.
ENDFORM.
*&---------------------------------------------------------------------*
*& Form INITIALIZATION
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*& --> p1 text
*& <-- p2 text
*&---------------------------------------------------------------------*
FORM initialization .
w_values-key = 'A'.
w_values-text = 'FOREGROUND'.
APPEND w_values TO t_values.
CLEAR w_values.
w_values-key = 'N'.
w_values-text = 'BACKGROUND'.
APPEND w_values TO t_values.
CLEAR w_values.
w_values-key = 'E'.
w_values-text = 'ONLY ERRORS'.
APPEND w_values TO t_values.
CLEAR w_values.
ENDFORM.
*----------------------------------------------------------------------*
* START NEW SCREEN *
*----------------------------------------------------------------------*
FORM bdc_dynpro USING program dynpro.
CLEAR wa_bdcdata.
wa_bdcdata-program = program.
wa_bdcdata-dynpro = dynpro.
wa_bdcdata-dynbegin = 'X'.
APPEND wa_bdcdata TO it_bdcdata.
ENDFORM.
*----------------------------------------------------------------------*
* INSERT FIELD *
*----------------------------------------------------------------------*
FORM bdc_field USING fnam fval.
* IF FVAL <> NODATA.
CLEAR wa_bdcdata.
wa_bdcdata-fnam = fnam.
wa_bdcdata-fval = fval.
SHIFT wa_bdcdata-fval LEFT DELETING LEADING space.
APPEND wa_bdcdata TO it_bdcdata.
* ENDIF.
ENDFORM.
*&---------------------------------------------------------------------*
*& Form VALIDATE_DATA
*&---------------------------------------------------------------------*
*& text
*&---------------------------------------------------------------------*
*& --> p1 text
*& <-- p2 text
*&---------------------------------------------------------------------*
FORM validate_data .
it_final2[] = it_final[].
REFRESH it_final[].
SELECT objnr, gjahr FROM cost INTO TABLE @DATA(t_cost).
LOOP AT it_final2 INTO wa_final.
CONCATENATE lv_name wa_final-cost_cntr wa_final-act_typ INTO lv_object.
READ TABLE t_cost INTO DATA(w_cost) WITH KEY objnr = lv_object gjahr = wa_final-fiscal_year.
IF sy-subrc = 0.
APPEND wa_final TO it_final.
ELSE.
CONCATENATE lv_name wa_final-cost_cntr wa_final-act_typ INTO lv_object.
wa_output-object_number = lv_object.
wa_output-year = wa_final-fiscal_year.
wa_output-desc = 'Object No. Does Not Exists'.
APPEND wa_output TO t_output.
ENDIF.
CLEAR: wa_final, wa_output.
ENDLOOP.
ENDFORM.
No comments:
Post a Comment