Code to Upload Data Using BAPI
types : begin of st_final,
pdate type BAPI2017_GM_HEAD_01-PSTNG_DATE,
matnr(18),
plant(4),
lgort(4),
batch(10),
qnty(13),
eou(3),
pcentr(10),
end of st_final.
types : begin of st_head,
pdate type BAPI2017_GM_HEAD_01-PSTNG_DATE,
end of st_head.
types : begin of st_item,
pdate type BAPI2017_GM_HEAD_01-PSTNG_DATE,
matnr type BAPI2017_GM_ITEM_CREATE-MATERIAL,
plant type BAPI2017_GM_ITEM_CREATE-PLANT,
lgort type BAPI2017_GM_ITEM_CREATE-STGE_LOC,
batch type BAPI2017_GM_ITEM_CREATE-BATCH,
qnty type BAPI2017_GM_ITEM_CREATE-ENTRY_QNT,
eou type BAPI2017_GM_ITEM_CREATE-ENTRY_UOM,
pcentr type BAPI2017_GM_ITEM_CREATE-COSTCENTER,
end of st_item.
types : begin of st_error,
docment(10),
year(4),
type(1),
message(50),
end of st_error.
data : it_final type table of st_final,
wa_final type st_final,
it_head type table of st_head,
wa_head type st_head,
it_item type table of st_item,
wa_item type st_item.
data : bapi_HEADER like BAPI2017_GM_HEAD_01,
bapi_CODE like BAPI2017_GM_CODE,
bapi_ITEM like table of BAPI2017_GM_ITEM_CREATE,
wa_goods like BAPI2017_GM_ITEM_CREATE,
it_error like table of BAPIRET2,
wa_error like BAPIRET2,
it_error1 type table of st_error,
wa_error1 type st_error.
data : flag.
data : v_document type bAPI2017_GM_HEAD_RET-MAT_DOC,
v_year type BAPI2017_GM_HEAD_RET-DOC_YEAR.
*Selection
selection-screen : begin of block b1 with frame title text-001.
PARAMETERS : INFILE LIKE RLGRAP-FILENAME.
selection-screen : end of block b1.
*Providing input help to select the file
AT SELECTION-SCREEN ON VALUE-REQUEST FOR INFILE.
CALL FUNCTION 'WS_FILENAME_GET'
EXPORTING
DEF_FILENAME = SPACE
DEF_PATH = INFILE
* MASK = MASK
MODE = 'O'
TITLE = 'INPUT FILE'
IMPORTING
FILENAME = INFILE
EXCEPTIONS
INV_WINSYS = 1
NO_BATCH = 2
SELECTION_CANCEL = 3
SELECTION_ERROR = 4
OTHERS = 5.
START-OF-SELECTION.
*Checking whether file is in text format otherwise providing message to the user
if infile CS '.TXT'.
else.
MESSAGE w005(ymess) WITH 'Invalid File Type !' 'Please provide a TEXT file'.
endif.
*uploading the file
CALL FUNCTION 'WS_UPLOAD'
EXPORTING
FILENAME = INFILE
FILETYPE = 'DAT'
TABLES
DATA_TAB = IT_final
.
IF SY-SUBRC <> 0.
* MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
* WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
ENDIF.
*segregating into header and item table
sort it_final by pdate.
loop at it_final into wa_final.
clear : flag.
at new pdate.
flag = 'X'.
endat.
if flag = 'X'.
wa_head-pdate = wa_final-pdate.
append wa_head to it_head.
clear wa_head.
endif.
wa_item-pdate = wa_final-pdate.
wa_item-matnr = wa_final-matnr.
wa_item-plant = wa_final-plant.
wa_item-lgort = wa_final-lgort.
wa_item-batch = wa_final-batch.
wa_item-qnty = wa_final-qnty.
wa_item-eou = wa_final-eou.
wa_item-pcentr = wa_final-pcentr.
append wa_item to it_item.
clear wa_item.
endloop.
loop at it_head into wa_head .
refresh : bapi_item,it_error.
clear : wa_goods,v_document,v_year,bapi_header.
bapi_header-PSTNG_DATE = wa_head-pdate.
bapi_header-DOC_DATE = sy-datum.
bapi_code = '03'. "code 03 for goods issue
loop at it_item into wa_item where pdate = wa_head-pdate.
*coversion carried out for the cost center
CALL FUNCTION 'CONVERSION_EXIT_ALPHA_INPUT'
EXPORTING
input = wa_item-pcentr
IMPORTING
OUTPUT = wa_item-pcentr
.
wa_goods-MATERIAL = wa_item-matnr.
wa_goods-PLANT = wa_item-plant.
wa_goods-STGE_LOC = wa_item-lgort.
wa_goods-BATCH = wa_item-batch.
wa_goods-MOVE_TYPE = '551'.
wa_goods-ENTRY_QNT = wa_item-qnty.
wa_goods-ENTRY_UOM = wa_item-eou.
wa_goods-COSTCENTER = wa_item-pcentr.
append wa_goods to bapi_item.
clear wa_goods.
endloop.
* calling bapi to upload the data
perform bapi.
endloop.
loop at it_error1 into wa_error1.
write : / wa_error1-docment , wa_error1-year,wa_error1-type ,wa_error1-message.
endloop.
*&---------------------------------------------------------------------*
*& Form BAPI
*&---------------------------------------------------------------------*
* text
*----------------------------------------------------------------------*
* --> p1 text
* <-- p2 text
*----------------------------------------------------------------------*
form BAPI .
CALL FUNCTION 'BAPI_GOODSMVT_CREATE'
EXPORTING
goodsmvt_header = bapi_header
goodsmvt_code = bapi_code
* TESTRUN = ' '
* GOODSMVT_REF_EWM =
IMPORTING
* GOODSMVT_HEADRET =
MATERIALDOCUMENT = v_document
MATDOCUMENTYEAR = v_year
tables
goodsmvt_item = bapi_item
* GOODSMVT_SERIALNUMBER =
return = it_error
* GOODSMVT_SERV_PART_DATA =
* EXTENSIONIN =
.
CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'
* IMPORTING
* WAIT = 2
* IMPORTING
* RETURN =
.
*dummy loop is carried out for database update
do 10000000 times.
enddo.
*error table
if it_error is initial.
wa_error1-docment = v_document.
wa_error1-year = v_year.
wa_error1-message = 'Document Created'.
append wa_error1 to it_error1.
clear wa_error1.
endif.
loop at it_error into wa_error where type = 'E' or type = 'S'.
wa_error1-type = wa_error-type.
wa_error1-message = wa_error-message.
append wa_error1 to it_error1.
clear wa_error1.
endloop.
endform. " BAPI