Hello everyone,
I have been working on a way to batch entry data for va01, stumbling across the bapi BAPI_SALESORDER_CREATEFROMDAT2, I have created a program for it, however there’s a slight issue,
I made loops inside the code so that it could accept multiple lines after reading an excel file, and it reads them properly. However when it comes to the bapi call, weird behaviour happens as the bapi enters records that are different from what it takes in the structure in the matnr field.
Here’s a snippet for the subroutine that calls the bapi.
The problem occurs in order_items_in table, you’ll also find a snapshot of what the data that is being entered looks like
FORM create_order .
CLEAR : gsdata_itab.
DATA: GTDATA2_ITAB LIKE GTDATA_ITAB.
gtdata2_itab = gtdata_itab.testheader.txttestdata.txt
LOOP AT gtdata_itab INTO gsdata_itab.
READ TABLE gtdata2_itab into gsdata_itab index sy-tabix.
at new vbeln.
clear gshd_itab.
READ TABLE gthd_itab WITH KEY vbeln = gsdata_itab-vbeln INTO gshd_itab BINARY SEARCH.
CLEAR: order_header_in, order_header_inx, order_partners[], order_partners.
order_header_in-doc_type = gshd_itab-order_type.
order_header_in-sales_org = gshd_itab-sorg.
order_header_in-distr_chan = gshd_itab-dch.
order_header_in-division = gshd_itab-div.
order_header_in-purch_no_c = gshd_itab-bstkd.
order_header_inx-updateflag = 'I'.
order_header_inx-doc_type = 'X'.
order_header_inx-sales_org = 'X'.
order_header_inx-distr_chan = 'X'.
order_header_inx-division = 'X'.
order_header_inx-purch_no_c = 'X'.
CALL FUNCTION 'CONVERSION_EXIT_ALPHA_INPUT'
EXPORTING
input = gshd_itab-kunnr
IMPORTING
OUTPUT = gshd_itab-kunnr.
order_partners-partn_role = 'AG'.
order_partners-partn_numb = gshd_itab-kunnr.
APPEND order_partners.
CLEAR: order_partners.
endat.
gv_item = gv_item + 10.
order_items_in-itm_number = gv_item.
CALL FUNCTION 'CONVERSION_EXIT_ALPHA_OUTPUT'
EXPORTING
input = gsdata_itab-matnr
IMPORTING
OUTPUT = gsdata_itab-matnr.
unpack gsdata_itab-matnr to matnr.
order_items_in-material = matnr.
CONDENSE gsdata_itab-qty.
order_items_in-target_qty = gsdata_itab-qty.
order_items_in-plant = gsdata_itab-plant.
order_items_in-store_loc = gsdata_itab-stloc.
APPEND order_items_in.
CLEAR: order_items_in.
order_items_inx-itm_number = gv_item.
order_items_inx-material = 'X'.
order_items_inx-target_qty = 'X'.
order_items_inx-plant = 'X'.
order_items_inx-store_loc = 'X'.
APPEND order_items_inx.
CLEAR: order_items_inx.
order_schedules_in-itm_number = gv_item.
order_schedules_in-req_qty = gsdata_itab-qty.
APPEND order_schedules_in.
CLEAR order_schedules_in.
order_schedules_inx-itm_number = gv_item.
order_schedules_inx-req_qty = 'X'.
APPEND order_schedules_inx.
CLEAR order_schedules_inx.
order_conditions_in-itm_number = gv_item.
order_conditions_in-cond_type = gsdata_itab-kschl.
CONDENSE gsdata_itab-price.
order_conditions_in-cond_value = ( gsdata_itab-price / 10 ). "dividing by 10 here cause bapi multiplies by 10 for some reason
APPEND order_conditions_in.
clear order_conditions_in.
order_conditions_in-itm_number = gv_item.
order_conditions_inx-cond_type = gsdata_itab-kschl.
order_conditions_inx-cond_value = 'X'.
APPEND order_conditions_inx.
clear order_conditions_inx.
CLEAR gsdata_itab.
at end of vbeln.
CALL FUNCTION 'BAPI_SALESORDER_CREATEFROMDAT2'
EXPORTING
order_header_in = order_header_in
ORDER_HEADER_INX = order_header_inx
IMPORTING
SALESDOCUMENT = SALESDOCUMENT
TABLES
RETURN = RETURN
ORDER_ITEMS_IN = ORDER_ITEMS_IN
ORDER_ITEMS_INX = ORDER_ITEMS_INX
order_partners = order_partners
ORDER_SCHEDULES_IN = ORDER_SCHEDULES_IN
ORDER_SCHEDULES_INX = ORDER_SCHEDULES_INX
ORDER_CONDITIONS_IN = ORDER_CONDITIONS_IN
ORDER_CONDITIONS_INX = ORDER_CONDITIONS_INX
.
if SALESDOCUMENT IS NOT INITIAL.
CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'
EXPORTING
WAIT = 'X'.
write: SALESDOCUMENT.
else.
write: / 'Issues in order creation.'.
endif.
CLEAR: ORDER_ITEMS_IN[], order_header_in, order_header_inx, ORDER_ITEMS_INX[], ORDER_SCHEDULES_IN[], ORDER_SCHEDULES_INX[], order_partners[], ORDER_CONDITIONS_IN[],ORDER_CONDITIONS_INX[], RETURN[].
endat.
ENDLOOP.