Create Material by BDC and BAPI
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
*&---------------------------------------------------------------------*
*& Report Z03_CREATE_MATERIAL
*&
*&---------------------------------------------------------------------*
*&
*&
*&---------------------------------------------------------------------*
REPORT z03_create_material.
TABLES: mara,
marc,
makt,
mbew.
TABLES: bapimathead,
bapi_makt,
bapi_mara,
bapi_marax,
bapi_marc,
bapi_marcx,
bapi_mbew,
bapi_mbewx.
DATA:BEGIN OF material,
mbrsh TYPE mara-mbrsh,
mtart TYPE mara-mtart,
werks TYPE marc-werks,
maktx TYPE makt-maktx,
meins TYPE mara-meins,
matkl TYPE mara-matkl,
normt TYPE mara-normt,
wrkst TYPE mara-wrkst,
ekgrp TYPE marc-ekgrp,
bklas TYPE mbew-bklas,
vprsv TYPE mbew-vprsv,
verpr TYPE mbew-verpr,
stprs TYPE mbew-stprs,
END OF material.
DATA: it_material LIKE STANDARD TABLE OF material,
wa_material LIKE material.
DATA: ex TYPE REF TO cx_root.
DATA:BEGIN OF zmsg,
type TYPE c,
message(100) TYPE C,
END OF zmsg.
DATA: it_zmsg LIKE STANDARD TABLE OF zmsg.
DATA: bdcdata LIKE bdcdata OCCURS 0 WITH HEADER LINE,
nodata TYPE c VALUE '/'.
DATA: it_return TYPE bapiret2,
it_bapi TYPE bapi_makt OCCURS 0 WITH HEADER LINE.
SELECTION-SCREEN: BEGIN OF BLOCK b1 WITH FRAME TITLE text-001.
* parameters: p_file like rlgrap-filename memory id mfolder obligatory
" logic file
SELECTION-SCREEN BEGIN OF LINE.
SELECTION-SCREEN COMMENT 1(25) text-003.
PARAMETERS: r_seq RADIOBUTTON GROUP r1 USER-COMMAND r_seq_click.
SELECTION-SCREEN END OF LINE.
SELECTION-SCREEN BEGIN OF LINE.
SELECTION-SCREEN COMMENT 1(25) text-004.
PARAMETERS:p_logic TYPE filename-fileintern
DEFAULT '\\dlces1006\sapmnt\trans\Z03_SEQ_FILE.txt'
MODIF ID m10.
SELECTION-SCREEN END OF LINE.
" Local file
SELECTION-SCREEN BEGIN OF LINE.
SELECTION-SCREEN COMMENT 1(25) text-005.
PARAMETERS: r_local RADIOBUTTON GROUP r1 DEFAULT 'X'.
SELECTION-SCREEN END OF LINE.
SELECTION-SCREEN BEGIN OF LINE.
SELECTION-SCREEN COMMENT 1(25) text-006.
PARAMETERS:p_local TYPE rlgrap-filename MODIF ID m11.
SELECTION-SCREEN END OF LINE.
SELECTION-SCREEN: END OF BLOCK b1.
SELECTION-SCREEN BEGIN OF BLOCK b2 WITH FRAME TITLE text-002.
PARAMETERS: r_bdc RADIOBUTTON GROUP r2 DEFAULT 'X',
r_bapi RADIOBUTTON GROUP r2 .
SELECTION-SCREEN END OF BLOCK b2.
AT SELECTION-SCREEN OUTPUT.
LOOP AT SCREEN.
IF r_seq = 'X' AND screen-group1 = 'M11'.
screen-input = '0'.
ELSEIF r_local = 'X' AND screen-group1 = 'M10'.
screen-input = '0'.
ELSE.
screen-input = '1'.
ENDIF.
MODIFY SCREEN.
ENDLOOP.
AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_local.
CALL FUNCTION 'KD_GET_FILENAME_ON_F4'
EXPORTING
mask = ',*.txt,*.*'
static = 'X'
CHANGING
file_name = p_local.
START-OF-SELECTION.
TRY .
PERFORM dataexistscheck.
PERFORM datavalidation.
PERFORM insertmaterial.
CATCH cx_root INTO ex.
zmsg-type = 'E'.
zmsg-message = ex->get_text( ).
APPEND zmsg TO it_zmsg.
ENDTRY.
IF LINES( it_zmsg ) <> 0.
PERFORM displayinfoalv TABLES it_zmsg.
ENDIF.
*&---------------------------------------------------------------------*
*& Form DATAEXISTSCHECK
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
FORM dataexistscheck .
IF r_seq = 'X'.
OPEN DATASET p_logic FOR INPUT IN TEXT MODE ENCODING UTF-8.
DO.
READ DATASET p_logic INTO wa_material.
APPEND wa_material TO it_material.
IF sy-subrc <> 0.
EXIT.
ENDIF.
ENDDO.
CLOSE DATASET p_logic.
ELSEIF r_local = 'X'.
DATA: path TYPE string.
path = p_local.
CALL FUNCTION 'GUI_UPLOAD'
EXPORTING
filename = path
filetype = 'ASC'
has_field_separator = 'X'
* HEADER_LENGTH = 0
* READ_BY_LINE = 'X'
* DAT_MODE = ' '
* CODEPAGE = ' '
* IGNORE_CERR = ABAP_TRUE
* REPLACEMENT = '#'
* CHECK_BOM = ' '
* VIRUS_SCAN_PROFILE =
* NO_AUTH_CHECK = ' '
* IMPORTING
* FILELENGTH =
* HEADER =
TABLES
data_tab = it_material.
ENDIF.
ENDFORM. " DATAEXISTSCHECK
*&---------------------------------------------------------------------*
*& Form DISPLAYINFOALV
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
FORM displayinfoalv TABLES it_zmsg.
* data: f_cat type lvc_t_fcat,
* wa_f_cat type lvc_s_fcat.
* data:l_alv TYPE REF TO cl_gui_alv_grid.
* wa_f_cat-FIELDNAME = 'type'.
* wa_f_cat-SCRTEXT_L = 'Type'.
* wa_f_cat-SCRTEXT_M = 'Type'.
* wa_f_cat-SCRTEXT_S = 'Type'.
* append wa_f_cat to f_cat.
* wa_f_cat-FIELDNAME = 'message'.
* wa_f_cat-SCRTEXT_L = 'Message'.
* wa_f_cat-SCRTEXT_M = 'Message'.
* wa_f_cat-SCRTEXT_S = 'Message'.
* append wa_f_cat to f_cat.
*
* CREATE OBJECT l_alv
* EXPORTING
* i_parent = cl_gui_container=>screen0.
* CALL METHOD l_alv->set_table_for_first_display
** EXPORTING
** i_structure_name = 'MSG'
* CHANGING
* it_outtab = it_msg
* it_fieldcatalog = f_cat.
DATA: wa_zmsg LIKE zmsg.
LOOP AT it_zmsg INTO wa_zmsg.
WRITE /: wa_zmsg-message.
ENDLOOP.
ENDFORM. " DISPLAYINFOALV
*&---------------------------------------------------------------------*
*& Form DATAVALIDATION
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
FORM datavalidation .
DATA: lstring type string.
lstring = text-008.
FIELD-SYMBOLS <fs> TYPE C.
LOOP AT it_material INTO wa_material.
assign '' to <fs>.
SELECT SINGLE msehi FROM t006 INTO <fs>
WHERE msehi = wa_material-meins.
IF sy-subrc <> 0.
zmsg-type = 'E'.
replace '&' with wa_material-maktx into zmsg-message.
APPEND zmsg TO it_zmsg.
ENDIF.
SELECT SINGLE matkl FROM t023 INTO <fs>
WHERE matkl = wa_material-matkl.
IF sy-subrc <> 0.
zmsg-type = 'E'.
replace '&' with wa_material-maktx into zmsg-message.
APPEND zmsg TO it_zmsg.
ENDIF.
SELECT SINGLE ekgrp FROM t024 INTO <fs>
WHERE ekgrp = wa_material-ekgrp.
IF sy-subrc <> 0.
zmsg-type = 'E'.
replace '&' with wa_material-maktx into zmsg-message.
APPEND zmsg TO it_zmsg.
ENDIF.
SELECT SINGLE bklas FROM mbew INTO <fs>
WHERE bklas = wa_material-bklas.
IF sy-subrc <> 0.
zmsg-type = 'E'.
replace '&' with wa_material-maktx into zmsg-message.
APPEND zmsg TO it_zmsg.
ENDIF.
ENDLOOP.
ENDFORM. " DATAVALIDATION
*&---------------------------------------------------------------------*
*& Form INSERTMATERIAL
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
FORM insertmaterial .
TRY .
IF r_bdc = 'X'.
PERFORM insertmaterialbybdc.
ELSEIF r_bapi = 'X'.
PERFORM insertmaterialbybapi.
ENDIF.
CATCH cx_root INTO ex.
zmsg-type = 'E'.
zmsg-message = ex->get_text( ).
APPEND zmsg TO it_zmsg.
ENDTRY.
ENDFORM. " INSERTMATERIAL
*&---------------------------------------------------------------------*
*& Form BDC_TRANSACTION
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* -->P_0637 text
*----------------------------------------------------------------------*
FORM bdc_transaction USING tcode.
CALL TRANSACTION tcode USING bdcdata
MODE 'A'.
REFRESH bdcdata.
ENDFORM. " BDC_TRANSACTION
*&---------------------------------------------------------------------*
*& Form BDC_DYNPRO
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* -->PROGRAM text
* -->DYNPRO text
*----------------------------------------------------------------------*
FORM bdc_dynpro USING program dynpro.
CLEAR bdcdata.
bdcdata-program = program.
bdcdata-dynpro = dynpro.
bdcdata-dynbegin = 'X'.
APPEND bdcdata.
ENDFORM. "BDC_DYNPRO
*----------------------------------------------------------------------*
* Insert field *
*----------------------------------------------------------------------*
FORM bdc_field USING fnam fval.
CLEAR bdcdata.
bdcdata-fnam = fnam.
bdcdata-fval = fval.
APPEND bdcdata.
ENDFORM. "BDC_FIELD
*&---------------------------------------------------------------------*
*& Form INSERTMATERIALBYBDC
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
FORM insertmaterialbybdc .
PERFORM data_create_for_plant.
LOOP AT it_material INTO wa_material.
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-MBRSH'
wa_material-mbrsh.
PERFORM bdc_field USING 'RMMG1-MTART'
wa_material-mtart.
PERFORM bdc_dynpro USING 'SAPLMGMM' '0070'.
PERFORM bdc_field USING 'BDC_CURSOR'
'MSICHTAUSW-DYTXT(14)'.
PERFORM bdc_field USING 'BDC_OKCODE'
'=ENTR'.
* perform bdc_field using 'MSICHTAUSW-KZSEL(01)'
* record-KZSEL_01_003.
* perform bdc_field using 'MSICHTAUSW-KZSEL(02)'
* record-KZSEL_02_004.
* perform bdc_field using 'MSICHTAUSW-KZSEL(03)'
* record-KZSEL_03_005.
* perform bdc_field using 'MSICHTAUSW-KZSEL(12)'
* record-KZSEL_12_006.
* perform bdc_field using 'MSICHTAUSW-KZSEL(13)'
* record-KZSEL_13_007.
* perform bdc_field using 'MSICHTAUSW-KZSEL(14)'
* record-KZSEL_14_008.
PERFORM bdc_dynpro USING 'SAPLMGMM' '0080'.
PERFORM bdc_field USING 'BDC_CURSOR'
'RMMG1-WERKS'.
PERFORM bdc_field USING 'BDC_OKCODE'
'=ENTR'.
PERFORM bdc_field USING 'RMMG1-WERKS'
wa_material-werks.
PERFORM bdc_dynpro USING 'SAPLMGMM' '4004'.
PERFORM bdc_field USING 'BDC_OKCODE'
'=SP02'.
PERFORM bdc_field USING 'MAKT-MAKTX'
wa_material-maktx.
PERFORM bdc_field USING 'BDC_CURSOR'
'MARA-BISMT'.
PERFORM bdc_field USING 'MARA-MEINS'
wa_material-meins.
PERFORM bdc_field USING 'MARA-MATKL'
wa_material-matkl.
PERFORM bdc_dynpro USING 'SAPLMGMM' '4004'.
PERFORM bdc_field USING 'BDC_OKCODE'
'=SP09'.
PERFORM bdc_field USING 'MAKT-MAKTX'
wa_material-maktx.
PERFORM bdc_field USING 'BDC_CURSOR'
'MARA-WRKST'.
PERFORM bdc_field USING 'MARA-NORMT'
wa_material-normt.
PERFORM bdc_field USING 'MARA-WRKST'
wa_material-wrkst.
PERFORM bdc_dynpro USING 'SAPLMGMM' '4000'.
PERFORM bdc_field USING 'BDC_OKCODE'
'=SP24'.
PERFORM bdc_field USING 'BDC_CURSOR'
'MAKT-MAKTX'.
PERFORM bdc_field USING 'MAKT-MAKTX'
wa_material-maktx.
PERFORM bdc_field USING 'MARA-MEINS'
wa_material-meins.
PERFORM bdc_field USING 'MARA-MATKL'
wa_material-matkl.
PERFORM bdc_dynpro USING 'SAPLMGMM' '4000'.
PERFORM bdc_field USING 'BDC_OKCODE'
'=BU'.
PERFORM bdc_field USING 'MAKT-MAKTX'
wa_material-maktx.
PERFORM bdc_field USING 'MARA-MEINS'
wa_material-meins.
PERFORM bdc_field USING 'BDC_CURSOR'
'MBEW-BKLAS'.
PERFORM bdc_field USING 'MBEW-BKLAS'
wa_material-bklas.
PERFORM bdc_field USING 'MBEW-VPRSV'
wa_material-vprsv.
* perform bdc_field using 'MBEW-PEINH'
* wa_material-PEINH.
IF wa_material-vprsv = 'V'.
PERFORM bdc_field USING 'MBEW-VERPR'
wa_material-verpr.
ELSEIF wa_material-vprsv = 'S'.
PERFORM bdc_field USING 'MBEW-STPRS'
wa_material-stprs.
ENDIF.
PERFORM bdc_transaction USING 'MM01'.
ENDLOOP.
ENDFORM. " INSERTMATERIALBYBDC
*&---------------------------------------------------------------------*
*& Form INSERTMATERIALBYBAPI
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
FORM insertmaterialbybapi .
LOOP AT it_material INTO wa_material.
DATA: mara TYPE matnr.
CALL FUNCTION 'NUMBER_GET_NEXT'
EXPORTING
nr_range_nr = '01'
object = 'MATERIALNR'
IMPORTING
number = mara.
PERFORM callbapi USING wa_material mara.
IF wa_material-werks = '1000'.
wa_material-werks = '2000'.
ELSEIF wa_material-werks = '2000'.
wa_material-werks = '1000'.
ENDIF.
PERFORM callbapi USING wa_material mara.
ENDLOOP.
ENDFORM. " INSERTMATERIALBYBAPI
*&---------------------------------------------------------------------*
*& Form DATA_CREATE_FOR_PLANT
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
FORM data_create_for_plant .
DATA: wa_m LIKE LINE OF it_material.
LOOP AT it_material INTO wa_material.
IF wa_material-werks = '1000'.
MOVE wa_material TO wa_m.
wa_m-werks = '2000'.
APPEND wa_m TO it_material.
ELSEIF wa_material-werks = '2000'.
MOVE wa_material TO wa_m.
wa_m = '1000'.
APPEND wa_m TO it_material.
ENDIF.
CLEAR wa_material.
ENDLOOP.
ENDFORM. " DATA_CREATE_FOR_PLANT
*&---------------------------------------------------------------------*
*& Form CALLBAPI
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* -->P_WA_MATERIAL text
* -->P_MARA text
*----------------------------------------------------------------------*
FORM callbapi USING wa_material LIKE LINE OF it_material
mara.
bapimathead-material = mara.
bapimathead-ind_sector = wa_material-mbrsh.
bapimathead-matl_type = wa_material-mtart.
bapimathead-basic_view = 'X'.
bapimathead-purchase_view = 'X'.
bapimathead-account_view = 'X'.
bapi_mara-matl_group = wa_material-matkl.
bapi_marax-matl_group = 'X'.
bapi_mara-base_uom = wa_material-meins.
bapi_marax-base_uom = 'X'.
bapi_mara-std_descr = wa_material-normt.
bapi_marax-std_descr = 'X'.
bapi_mara-basic_matl = wa_material-wrkst.
bapi_marax-basic_matl = 'X'.
bapi_marc-pur_group = wa_material-ekgrp.
bapi_marcx-pur_group = 'X'.
bapi_marc-plant = wa_material-werks.
bapi_marcx-plant = wa_material-werks.
bapi_mbew-val_class = wa_material-bklas.
bapi_mbewx-val_class = 'X'.
bapi_mbew-price_ctrl = wa_material-vprsv.
bapi_mbewx-price_ctrl = 'X'.
IF wa_material-vprsv = 'V'.
bapi_mbew-moving_pr = wa_material-verpr.
bapi_mbewx-moving_pr = 'X'.
ELSEIF wa_material-vprsv = 'S'.
bapi_mbew-std_price = wa_material-stprs.
bapi_mbewx-std_price = 'X'.
ENDIF.
bapi_mbew-val_area = wa_material-werks.
bapi_mbewx-val_area = wa_material-werks.
it_bapi-langu = sy-langu.
it_bapi-langu_iso = sy-langu.
it_bapi-matl_desc = wa_material-maktx.
APPEND it_bapi.
CLEAR it_bapi.
CALL FUNCTION 'BAPI_MATERIAL_SAVEDATA'
EXPORTING
headdata = bapimathead
clientdata = bapi_mara
clientdatax = bapi_marax
plantdata = bapi_marc
plantdatax = bapi_marcx
valuationdata = bapi_mbew
valuationdatax = bapi_mbewx
IMPORTING
return = it_return
TABLES
materialdescription = it_bapi.
CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'.
zmsg-type = it_return-type.
zmsg-message = it_return-message.
APPEND zmsg TO it_zmsg.
ENDFORM. " CALLBAPI
*& Report Z03_CREATE_MATERIAL
*&
*&---------------------------------------------------------------------*
*&
*&
*&---------------------------------------------------------------------*
REPORT z03_create_material.
TABLES: mara,
marc,
makt,
mbew.
TABLES: bapimathead,
bapi_makt,
bapi_mara,
bapi_marax,
bapi_marc,
bapi_marcx,
bapi_mbew,
bapi_mbewx.
DATA:BEGIN OF material,
mbrsh TYPE mara-mbrsh,
mtart TYPE mara-mtart,
werks TYPE marc-werks,
maktx TYPE makt-maktx,
meins TYPE mara-meins,
matkl TYPE mara-matkl,
normt TYPE mara-normt,
wrkst TYPE mara-wrkst,
ekgrp TYPE marc-ekgrp,
bklas TYPE mbew-bklas,
vprsv TYPE mbew-vprsv,
verpr TYPE mbew-verpr,
stprs TYPE mbew-stprs,
END OF material.
DATA: it_material LIKE STANDARD TABLE OF material,
wa_material LIKE material.
DATA: ex TYPE REF TO cx_root.
DATA:BEGIN OF zmsg,
type TYPE c,
message(100) TYPE C,
END OF zmsg.
DATA: it_zmsg LIKE STANDARD TABLE OF zmsg.
DATA: bdcdata LIKE bdcdata OCCURS 0 WITH HEADER LINE,
nodata TYPE c VALUE '/'.
DATA: it_return TYPE bapiret2,
it_bapi TYPE bapi_makt OCCURS 0 WITH HEADER LINE.
SELECTION-SCREEN: BEGIN OF BLOCK b1 WITH FRAME TITLE text-001.
* parameters: p_file like rlgrap-filename memory id mfolder obligatory
" logic file
SELECTION-SCREEN BEGIN OF LINE.
SELECTION-SCREEN COMMENT 1(25) text-003.
PARAMETERS: r_seq RADIOBUTTON GROUP r1 USER-COMMAND r_seq_click.
SELECTION-SCREEN END OF LINE.
SELECTION-SCREEN BEGIN OF LINE.
SELECTION-SCREEN COMMENT 1(25) text-004.
PARAMETERS:p_logic TYPE filename-fileintern
DEFAULT '\\dlces1006\sapmnt\trans\Z03_SEQ_FILE.txt'
MODIF ID m10.
SELECTION-SCREEN END OF LINE.
" Local file
SELECTION-SCREEN BEGIN OF LINE.
SELECTION-SCREEN COMMENT 1(25) text-005.
PARAMETERS: r_local RADIOBUTTON GROUP r1 DEFAULT 'X'.
SELECTION-SCREEN END OF LINE.
SELECTION-SCREEN BEGIN OF LINE.
SELECTION-SCREEN COMMENT 1(25) text-006.
PARAMETERS:p_local TYPE rlgrap-filename MODIF ID m11.
SELECTION-SCREEN END OF LINE.
SELECTION-SCREEN: END OF BLOCK b1.
SELECTION-SCREEN BEGIN OF BLOCK b2 WITH FRAME TITLE text-002.
PARAMETERS: r_bdc RADIOBUTTON GROUP r2 DEFAULT 'X',
r_bapi RADIOBUTTON GROUP r2 .
SELECTION-SCREEN END OF BLOCK b2.
AT SELECTION-SCREEN OUTPUT.
LOOP AT SCREEN.
IF r_seq = 'X' AND screen-group1 = 'M11'.
screen-input = '0'.
ELSEIF r_local = 'X' AND screen-group1 = 'M10'.
screen-input = '0'.
ELSE.
screen-input = '1'.
ENDIF.
MODIFY SCREEN.
ENDLOOP.
AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_local.
CALL FUNCTION 'KD_GET_FILENAME_ON_F4'
EXPORTING
mask = ',*.txt,*.*'
static = 'X'
CHANGING
file_name = p_local.
START-OF-SELECTION.
TRY .
PERFORM dataexistscheck.
PERFORM datavalidation.
PERFORM insertmaterial.
CATCH cx_root INTO ex.
zmsg-type = 'E'.
zmsg-message = ex->get_text( ).
APPEND zmsg TO it_zmsg.
ENDTRY.
IF LINES( it_zmsg ) <> 0.
PERFORM displayinfoalv TABLES it_zmsg.
ENDIF.
*&---------------------------------------------------------------------*
*& Form DATAEXISTSCHECK
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
FORM dataexistscheck .
IF r_seq = 'X'.
OPEN DATASET p_logic FOR INPUT IN TEXT MODE ENCODING UTF-8.
DO.
READ DATASET p_logic INTO wa_material.
APPEND wa_material TO it_material.
IF sy-subrc <> 0.
EXIT.
ENDIF.
ENDDO.
CLOSE DATASET p_logic.
ELSEIF r_local = 'X'.
DATA: path TYPE string.
path = p_local.
CALL FUNCTION 'GUI_UPLOAD'
EXPORTING
filename = path
filetype = 'ASC'
has_field_separator = 'X'
* HEADER_LENGTH = 0
* READ_BY_LINE = 'X'
* DAT_MODE = ' '
* CODEPAGE = ' '
* IGNORE_CERR = ABAP_TRUE
* REPLACEMENT = '#'
* CHECK_BOM = ' '
* VIRUS_SCAN_PROFILE =
* NO_AUTH_CHECK = ' '
* IMPORTING
* FILELENGTH =
* HEADER =
TABLES
data_tab = it_material.
ENDIF.
ENDFORM. " DATAEXISTSCHECK
*&---------------------------------------------------------------------*
*& Form DISPLAYINFOALV
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
FORM displayinfoalv TABLES it_zmsg.
* data: f_cat type lvc_t_fcat,
* wa_f_cat type lvc_s_fcat.
* data:l_alv TYPE REF TO cl_gui_alv_grid.
* wa_f_cat-FIELDNAME = 'type'.
* wa_f_cat-SCRTEXT_L = 'Type'.
* wa_f_cat-SCRTEXT_M = 'Type'.
* wa_f_cat-SCRTEXT_S = 'Type'.
* append wa_f_cat to f_cat.
* wa_f_cat-FIELDNAME = 'message'.
* wa_f_cat-SCRTEXT_L = 'Message'.
* wa_f_cat-SCRTEXT_M = 'Message'.
* wa_f_cat-SCRTEXT_S = 'Message'.
* append wa_f_cat to f_cat.
*
* CREATE OBJECT l_alv
* EXPORTING
* i_parent = cl_gui_container=>screen0.
* CALL METHOD l_alv->set_table_for_first_display
** EXPORTING
** i_structure_name = 'MSG'
* CHANGING
* it_outtab = it_msg
* it_fieldcatalog = f_cat.
DATA: wa_zmsg LIKE zmsg.
LOOP AT it_zmsg INTO wa_zmsg.
WRITE /: wa_zmsg-message.
ENDLOOP.
ENDFORM. " DISPLAYINFOALV
*&---------------------------------------------------------------------*
*& Form DATAVALIDATION
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
FORM datavalidation .
DATA: lstring type string.
lstring = text-008.
FIELD-SYMBOLS <fs> TYPE C.
LOOP AT it_material INTO wa_material.
assign '' to <fs>.
SELECT SINGLE msehi FROM t006 INTO <fs>
WHERE msehi = wa_material-meins.
IF sy-subrc <> 0.
zmsg-type = 'E'.
replace '&' with wa_material-maktx into zmsg-message.
APPEND zmsg TO it_zmsg.
ENDIF.
SELECT SINGLE matkl FROM t023 INTO <fs>
WHERE matkl = wa_material-matkl.
IF sy-subrc <> 0.
zmsg-type = 'E'.
replace '&' with wa_material-maktx into zmsg-message.
APPEND zmsg TO it_zmsg.
ENDIF.
SELECT SINGLE ekgrp FROM t024 INTO <fs>
WHERE ekgrp = wa_material-ekgrp.
IF sy-subrc <> 0.
zmsg-type = 'E'.
replace '&' with wa_material-maktx into zmsg-message.
APPEND zmsg TO it_zmsg.
ENDIF.
SELECT SINGLE bklas FROM mbew INTO <fs>
WHERE bklas = wa_material-bklas.
IF sy-subrc <> 0.
zmsg-type = 'E'.
replace '&' with wa_material-maktx into zmsg-message.
APPEND zmsg TO it_zmsg.
ENDIF.
ENDLOOP.
ENDFORM. " DATAVALIDATION
*&---------------------------------------------------------------------*
*& Form INSERTMATERIAL
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
FORM insertmaterial .
TRY .
IF r_bdc = 'X'.
PERFORM insertmaterialbybdc.
ELSEIF r_bapi = 'X'.
PERFORM insertmaterialbybapi.
ENDIF.
CATCH cx_root INTO ex.
zmsg-type = 'E'.
zmsg-message = ex->get_text( ).
APPEND zmsg TO it_zmsg.
ENDTRY.
ENDFORM. " INSERTMATERIAL
*&---------------------------------------------------------------------*
*& Form BDC_TRANSACTION
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* -->P_0637 text
*----------------------------------------------------------------------*
FORM bdc_transaction USING tcode.
CALL TRANSACTION tcode USING bdcdata
MODE 'A'.
REFRESH bdcdata.
ENDFORM. " BDC_TRANSACTION
*&---------------------------------------------------------------------*
*& Form BDC_DYNPRO
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* -->PROGRAM text
* -->DYNPRO text
*----------------------------------------------------------------------*
FORM bdc_dynpro USING program dynpro.
CLEAR bdcdata.
bdcdata-program = program.
bdcdata-dynpro = dynpro.
bdcdata-dynbegin = 'X'.
APPEND bdcdata.
ENDFORM. "BDC_DYNPRO
*----------------------------------------------------------------------*
* Insert field *
*----------------------------------------------------------------------*
FORM bdc_field USING fnam fval.
CLEAR bdcdata.
bdcdata-fnam = fnam.
bdcdata-fval = fval.
APPEND bdcdata.
ENDFORM. "BDC_FIELD
*&---------------------------------------------------------------------*
*& Form INSERTMATERIALBYBDC
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
FORM insertmaterialbybdc .
PERFORM data_create_for_plant.
LOOP AT it_material INTO wa_material.
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-MBRSH'
wa_material-mbrsh.
PERFORM bdc_field USING 'RMMG1-MTART'
wa_material-mtart.
PERFORM bdc_dynpro USING 'SAPLMGMM' '0070'.
PERFORM bdc_field USING 'BDC_CURSOR'
'MSICHTAUSW-DYTXT(14)'.
PERFORM bdc_field USING 'BDC_OKCODE'
'=ENTR'.
* perform bdc_field using 'MSICHTAUSW-KZSEL(01)'
* record-KZSEL_01_003.
* perform bdc_field using 'MSICHTAUSW-KZSEL(02)'
* record-KZSEL_02_004.
* perform bdc_field using 'MSICHTAUSW-KZSEL(03)'
* record-KZSEL_03_005.
* perform bdc_field using 'MSICHTAUSW-KZSEL(12)'
* record-KZSEL_12_006.
* perform bdc_field using 'MSICHTAUSW-KZSEL(13)'
* record-KZSEL_13_007.
* perform bdc_field using 'MSICHTAUSW-KZSEL(14)'
* record-KZSEL_14_008.
PERFORM bdc_dynpro USING 'SAPLMGMM' '0080'.
PERFORM bdc_field USING 'BDC_CURSOR'
'RMMG1-WERKS'.
PERFORM bdc_field USING 'BDC_OKCODE'
'=ENTR'.
PERFORM bdc_field USING 'RMMG1-WERKS'
wa_material-werks.
PERFORM bdc_dynpro USING 'SAPLMGMM' '4004'.
PERFORM bdc_field USING 'BDC_OKCODE'
'=SP02'.
PERFORM bdc_field USING 'MAKT-MAKTX'
wa_material-maktx.
PERFORM bdc_field USING 'BDC_CURSOR'
'MARA-BISMT'.
PERFORM bdc_field USING 'MARA-MEINS'
wa_material-meins.
PERFORM bdc_field USING 'MARA-MATKL'
wa_material-matkl.
PERFORM bdc_dynpro USING 'SAPLMGMM' '4004'.
PERFORM bdc_field USING 'BDC_OKCODE'
'=SP09'.
PERFORM bdc_field USING 'MAKT-MAKTX'
wa_material-maktx.
PERFORM bdc_field USING 'BDC_CURSOR'
'MARA-WRKST'.
PERFORM bdc_field USING 'MARA-NORMT'
wa_material-normt.
PERFORM bdc_field USING 'MARA-WRKST'
wa_material-wrkst.
PERFORM bdc_dynpro USING 'SAPLMGMM' '4000'.
PERFORM bdc_field USING 'BDC_OKCODE'
'=SP24'.
PERFORM bdc_field USING 'BDC_CURSOR'
'MAKT-MAKTX'.
PERFORM bdc_field USING 'MAKT-MAKTX'
wa_material-maktx.
PERFORM bdc_field USING 'MARA-MEINS'
wa_material-meins.
PERFORM bdc_field USING 'MARA-MATKL'
wa_material-matkl.
PERFORM bdc_dynpro USING 'SAPLMGMM' '4000'.
PERFORM bdc_field USING 'BDC_OKCODE'
'=BU'.
PERFORM bdc_field USING 'MAKT-MAKTX'
wa_material-maktx.
PERFORM bdc_field USING 'MARA-MEINS'
wa_material-meins.
PERFORM bdc_field USING 'BDC_CURSOR'
'MBEW-BKLAS'.
PERFORM bdc_field USING 'MBEW-BKLAS'
wa_material-bklas.
PERFORM bdc_field USING 'MBEW-VPRSV'
wa_material-vprsv.
* perform bdc_field using 'MBEW-PEINH'
* wa_material-PEINH.
IF wa_material-vprsv = 'V'.
PERFORM bdc_field USING 'MBEW-VERPR'
wa_material-verpr.
ELSEIF wa_material-vprsv = 'S'.
PERFORM bdc_field USING 'MBEW-STPRS'
wa_material-stprs.
ENDIF.
PERFORM bdc_transaction USING 'MM01'.
ENDLOOP.
ENDFORM. " INSERTMATERIALBYBDC
*&---------------------------------------------------------------------*
*& Form INSERTMATERIALBYBAPI
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
FORM insertmaterialbybapi .
LOOP AT it_material INTO wa_material.
DATA: mara TYPE matnr.
CALL FUNCTION 'NUMBER_GET_NEXT'
EXPORTING
nr_range_nr = '01'
object = 'MATERIALNR'
IMPORTING
number = mara.
PERFORM callbapi USING wa_material mara.
IF wa_material-werks = '1000'.
wa_material-werks = '2000'.
ELSEIF wa_material-werks = '2000'.
wa_material-werks = '1000'.
ENDIF.
PERFORM callbapi USING wa_material mara.
ENDLOOP.
ENDFORM. " INSERTMATERIALBYBAPI
*&---------------------------------------------------------------------*
*& Form DATA_CREATE_FOR_PLANT
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
FORM data_create_for_plant .
DATA: wa_m LIKE LINE OF it_material.
LOOP AT it_material INTO wa_material.
IF wa_material-werks = '1000'.
MOVE wa_material TO wa_m.
wa_m-werks = '2000'.
APPEND wa_m TO it_material.
ELSEIF wa_material-werks = '2000'.
MOVE wa_material TO wa_m.
wa_m = '1000'.
APPEND wa_m TO it_material.
ENDIF.
CLEAR wa_material.
ENDLOOP.
ENDFORM. " DATA_CREATE_FOR_PLANT
*&---------------------------------------------------------------------*
*& Form CALLBAPI
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* -->P_WA_MATERIAL text
* -->P_MARA text
*----------------------------------------------------------------------*
FORM callbapi USING wa_material LIKE LINE OF it_material
mara.
bapimathead-material = mara.
bapimathead-ind_sector = wa_material-mbrsh.
bapimathead-matl_type = wa_material-mtart.
bapimathead-basic_view = 'X'.
bapimathead-purchase_view = 'X'.
bapimathead-account_view = 'X'.
bapi_mara-matl_group = wa_material-matkl.
bapi_marax-matl_group = 'X'.
bapi_mara-base_uom = wa_material-meins.
bapi_marax-base_uom = 'X'.
bapi_mara-std_descr = wa_material-normt.
bapi_marax-std_descr = 'X'.
bapi_mara-basic_matl = wa_material-wrkst.
bapi_marax-basic_matl = 'X'.
bapi_marc-pur_group = wa_material-ekgrp.
bapi_marcx-pur_group = 'X'.
bapi_marc-plant = wa_material-werks.
bapi_marcx-plant = wa_material-werks.
bapi_mbew-val_class = wa_material-bklas.
bapi_mbewx-val_class = 'X'.
bapi_mbew-price_ctrl = wa_material-vprsv.
bapi_mbewx-price_ctrl = 'X'.
IF wa_material-vprsv = 'V'.
bapi_mbew-moving_pr = wa_material-verpr.
bapi_mbewx-moving_pr = 'X'.
ELSEIF wa_material-vprsv = 'S'.
bapi_mbew-std_price = wa_material-stprs.
bapi_mbewx-std_price = 'X'.
ENDIF.
bapi_mbew-val_area = wa_material-werks.
bapi_mbewx-val_area = wa_material-werks.
it_bapi-langu = sy-langu.
it_bapi-langu_iso = sy-langu.
it_bapi-matl_desc = wa_material-maktx.
APPEND it_bapi.
CLEAR it_bapi.
CALL FUNCTION 'BAPI_MATERIAL_SAVEDATA'
EXPORTING
headdata = bapimathead
clientdata = bapi_mara
clientdatax = bapi_marax
plantdata = bapi_marc
plantdatax = bapi_marcx
valuationdata = bapi_mbew
valuationdatax = bapi_mbewx
IMPORTING
return = it_return
TABLES
materialdescription = it_bapi.
CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'.
zmsg-type = it_return-type.
zmsg-message = it_return-message.
APPEND zmsg TO it_zmsg.
ENDFORM. " CALLBAPI