在某项目中,根据客户需求,需要把计划订单批量转化为PR,本程序是利用BDC方式实现的.
*&---------------------------------------------------------------------*
*& Report ZMM_PLO_TO_PR
*&
*&---------------------------------------------------------------------*
*&
*&
*&---------------------------------------------------------------------*
REPORT ZMM_PLO_TO_PR.
TYPES:BEGIN OF TY_ITAB,
PLNUM type plaf-PLNUM,
GSMNG(13),
END OF TY_ITAB.
DATA:ITAB TYPE TABLE OF TY_ITAB WITH HEADER LINE.
TYPES:BEGIN OF TY_RESULT,
PLNUM type plaf-PLNUM,
GSMNG(13),
MESSAGE(100),
END OF TY_RESULT.
DATA:IT_RESULT TYPE TABLE OF TY_RESULT with header line.
data: g_info type string.
* BDC 表定义.
DATA:it_bdc LIKE bdcdata OCCURS 0 WITH HEADER LINE.
DATA:it_messtab LIKE bdcmsgcoll OCCURS 0 WITH HEADER LINE.
selection-screen begin of block shi with frame title text-001.
parameters:p_file like rlgrap-filename memory id zy1 obligatory.
selection-screen end of block shi.
at selection-screen on value-request for p_file.
CALL FUNCTION 'F4_FILENAME'
EXPORTING
program_name = syst-cprog
dynpro_number = syst-dynnr
IMPORTING
file_name = p_file.
START-OF-SELECTION.
PERFORM FRM_GETDATA.
PERFORM FRM_BDC.
PERFORM FRM_DOWNLOAD.
*&---------------------------------------------------------------------*
*& Form FRM_GETDATA
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
FORM FRM_GETDATA .
CALL FUNCTION 'WS_UPLOAD'
EXPORTING
* CODEPAGE = ' '
FILENAME = P_FILE
FILETYPE = 'DAT'
* HEADLEN = ' '
* LINE_EXIT = ' '
* TRUNCLEN = ' '
* USER_FORM = ' '
* USER_PROG = ' '
* DAT_D_FORMAT = ' '
* IMPORTING
* FILELENGTH =
TABLES
DATA_TAB = ITAB
* EXCEPTIONS
* CONVERSION_ERROR = 1
* FILE_OPEN_ERROR = 2
* FILE_READ_ERROR = 3
* INVALID_TYPE = 4
* NO_BATCH = 5
* UNKNOWN_ERROR = 6
* INVALID_TABLE_WIDTH = 7
* GUI_REFUSE_FILETRANSFER = 8
* CUSTOMER_ERROR = 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.
ENDFORM. " FRM_GETDATA
*&---------------------------------------------------------------------*
*& Form FRM_BDC
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
FORM FRM_BDC .
LOOP AT ITAB.
MOVE-corresponding itab to it_result.
PERFORM frm_fill_bdc_tab USING :
'X' 'SAPMM61P' '0200',
' ' 'BDC_OKCODE' '/00',
' ' 'RM61P-PLNUM' ITAB-PLNUM.
PERFORM frm_fill_bdc_tab USING :
'X' 'SAPLM61H' '0210',
' ' 'BDC_OKCODE' '=HZBA',
' ' 'MDBA-MENGE' ITAB-GSMNG.
REFRESH IT_MESSTAB . CLEAR IT_MESSTAB.
CALL TRANSACTION 'MD14'
USING IT_BDC
MODE 'N'
UPDATE 'S'
MESSAGES INTO IT_MESSTAB.
IF SY-SUBRC <> 0 .
READ TABLE IT_MESSTAB WITH KEY MSGTYP = 'E'.
IF SY-SUBRC = 0.
ELSE.
READ TABLE IT_MESSTAB INDEX 1.
ENDIF.
call function 'MESSAGE_TEXT_BUILD'
exporting
msgid = IT_MESSTAB-MSGID
msgnr = IT_MESSTAB-MSGNR
msgv1 = IT_MESSTAB-msgv1
msgv2 = IT_MESSTAB-msgv2
msgv3 = IT_MESSTAB-msgv3
msgv4 = IT_MESSTAB-msgv4
importing
message_text_output = g_info.
IT_RESULT-MESSAGE = G_INFO.
ELSE.
READ TABLE IT_MESSTAB WITH KEY MSGTYP = 'S'
MSGNR = '039'.
call function 'MESSAGE_TEXT_BUILD'
exporting
msgid = IT_MESSTAB-MSGID
msgnr = IT_MESSTAB-MSGNR
msgv1 = IT_MESSTAB-msgv1
msgv2 = IT_MESSTAB-msgv2
msgv3 = IT_MESSTAB-msgv3
msgv4 = IT_MESSTAB-msgv4
importing
message_text_output = g_info.
IT_RESULT-MESSAGE = G_INFO.
ENDIF.
APPEND IT_RESULT.
CLEAR it_RESULT.
CLEAR ITAB.
REFRESH IT_BDC.
CLEAR IT_BDC.
CLEAR G_INFO.
ENDLOOP.
ENDFORM. " FRM_BDC
*&---------------------------------------------------------------------*
*& Form FRM_FILL_BDC_TAB
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* -->PV_FLAG text
* -->PV_VAR1 text
* -->PV_VAR2 text
*----------------------------------------------------------------------*
FORM frm_fill_bdc_tab USING pv_flag
pv_var1
pv_var2.
CLEAR it_bdc.
IF pv_flag = 'X'.
it_bdc-dynbegin = 'X'.
it_bdc-program = pv_var1.
it_bdc-dynpro = pv_var2.
ELSE.
it_bdc-fnam = pv_var1.
it_bdc-fval = pv_var2.
ENDIF.
APPEND it_bdc.
ENDFORM. "FRM_FILL_BDC_TAB
*&---------------------------------------------------------------------*
*& Form FRM_DOWNLOAD
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
FORM FRM_DOWNLOAD .
CALL FUNCTION 'GUI_DOWNLOAD'
EXPORTING
* BIN_FILESIZE =
FILENAME = 'C:/结果表.TXT'
FILETYPE = 'ASC'
APPEND = ' '
* WRITE_FIELD_SEPARATOR = ' '
* HEADER = '00'
* TRUNC_TRAILING_BLANKS = ' '
* WRITE_LF = 'X'
* COL_SELECT = ' '
* COL_SELECT_MASK = ' '
* DAT_MODE = ' '
* CONFIRM_OVERWRITE = ' '
* NO_AUTH_CHECK = ' '
* CODEPAGE = ' '
* IGNORE_CERR = ABAP_TRUE
* REPLACEMENT = '#'
* WRITE_BOM = ' '
* TRUNC_TRAILING_BLANKS_EOL = 'X'
* WK1_N_FORMAT = ' '
* WK1_N_SIZE = ' '
* WK1_T_FORMAT = ' '
* WK1_T_SIZE = ' '
* IMPORTING
* FILELENGTH =
TABLES
DATA_TAB = IT_result
* FIELDNAMES =
* EXCEPTIONS
* FILE_WRITE_ERROR = 1
* NO_BATCH = 2
* GUI_REFUSE_FILETRANSFER = 3
* INVALID_TYPE = 4
* NO_AUTHORITY = 5
* UNKNOWN_ERROR = 6
* HEADER_NOT_ALLOWED = 7
* SEPARATOR_NOT_ALLOWED = 8
* FILESIZE_NOT_ALLOWED = 9
* HEADER_TOO_LONG = 10
* DP_ERROR_CREATE = 11
* DP_ERROR_SEND = 12
* DP_ERROR_WRITE = 13
* UNKNOWN_DP_ERROR = 14
* ACCESS_DENIED = 15
* DP_OUT_OF_MEMORY = 16
* DISK_FULL = 17
* DP_TIMEOUT = 18
* FILE_NOT_FOUND = 19
* DATAPROVIDER_EXCEPTION = 20
* CONTROL_FLUSH_ERROR = 21
* OTHERS = 22
.
IF SY-SUBRC <> 0.
* MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
* WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
ENDIF.
WRITE:'数据成功保存到C盘根目录下,请查看!'.
ENDFORM. " FRM_DOWNLOAD