REPORT ZSAPN_BDC
NO STANDARD PAGE HEADING LINE-SIZE 255.
TYPES: BEGIN OF TY_MARA, "user defined types as per flat file
MATNR TYPE MARA-MATNR,
MBRSH TYPE MARA-MBRSH,
MTART TYPE MARA-MTART,
MEINS TYPE MARA-MEINS,
MAKTX TYPE MAKT-MAKTX,
END OF TY_MARA.
DATA : IT_MARA TYPE TABLE OF TY_MARA, "mara internal table
WA_MARA TYPE TY_MARA. "mara work area
DATA: IT_BDCDATA TYPE TABLE OF BDCDATA . "BDCDATA
DATA: WA_BDCDATA TYPE BDCDATA . "work area BDCDATA
DATA : BDCMSG TYPE TABLE OF BDCMSGCOLL. "BDC message table
DATA:FILE TYPE STRING. "file name
PARAMETERS : P_FILE TYPE RLGRAP-FILENAME. "input parameter for file upload
START-OF-SELECTION.
IF P_FILE IS NOT INITIAL.
FILE = P_FILE.
CALL FUNCTION 'GUI_UPLOAD' "upload flat file
EXPORTING
FILENAME = FILE "file name
FILETYPE = 'ASC' "file type
HAS_FIELD_SEPARATOR = 'X' "is tab delimited
TABLES
DATA_TAB = IT_MARA.
IF SY-SUBRC <> 0.
* Implement suitable error handling here
ENDIF.
ENDIF.
LOOP AT IT_MARA INTO WA_MARA. "loop all records and pass one by one to BDC
PERFORM BDC_DYNPRO USING 'SAPLMGMM' '0060'.
PERFORM BDC_FIELD USING 'BDC_CURSOR'
'RMMG1-MTART'.
PERFORM BDC_FIELD USING 'BDC_OKCODE'
'ENTR'.
PERFORM BDC_FIELD USING 'RMMG1-MATNR'
WA_MARA-MATNR. "pass material no
PERFORM BDC_FIELD USING 'RMMG1-MBRSH'
WA_MARA-MBRSH. "pass indistry sector
PERFORM BDC_FIELD USING 'RMMG1-MTART'
WA_MARA-MTART. "pass material type
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)' "select basic view1
'X'.
PERFORM BDC_DYNPRO USING 'SAPLMGMM' '4004'.
PERFORM BDC_FIELD USING 'BDC_OKCODE'
'=BU'.
PERFORM BDC_FIELD USING 'MAKT-MAKTX'
WA_MARA-MAKTX. "pass material description
PERFORM BDC_FIELD USING 'BDC_CURSOR'
'MARA-MEINS'.
PERFORM BDC_FIELD USING 'MARA-MEINS'
WA_MARA-MEINS. "pass base unit oe meassure
CALL TRANSACTION 'MM01' USING IT_BDCDATA "call transaction
MODE 'N' "N-no screen mode, A-all screen mode, E-error screen mode
UPDATE 'A' "A-assynchronous, S-synchronous
MESSAGES INTO BDCMSG. "messages
IF SY-SUBRC EQ 0.
WRITE :/ WA_MARA-MATNR, 'submitted to BDC'.
ENDIF.
ENDLOOP.
DATA : WA_BDCMSG LIKE LINE OF BDCMSG.
IF BDCMSG IS NOT INITIAL. "display messages
LOOP AT BDCMSG INTO WA_BDCMSG.
WRITE:/ WA_BDCMSG-TCODE, WA_BDCMSG-MSGTYP, WA_BDCMSG-MSGV1, WA_BDCMSG-FLDNAME .
CLEAR WA_BDCMSG.
ENDLOOP.
ENDIF.
FORM BDC_DYNPRO USING PROGRAM DYNPRO.
CLEAR WA_BDCDATA.
WA_BDCDATA-PROGRAM = PROGRAM. "program
WA_BDCDATA-DYNPRO = DYNPRO. "screen
WA_BDCDATA-DYNBEGIN = 'X'. "begin
APPEND WA_BDCDATA TO IT_BDCDATA..
ENDFORM.
FORM BDC_FIELD USING FNAM FVAL.
* IF FVAL <> NODATA.
CLEAR WA_BDCDATA.
WA_BDCDATA-FNAM = FNAM. "field name ex: matnr
WA_BDCDATA-FVAL = FVAL. "field value ex: testmat001
APPEND WA_BDCDATA TO IT_BDCDATA.
* ENDIF.
ENDFORM.