Application Development Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 

Problems while using BAPI_SALESORDER_CREATEFROMDAT2

shreyank_joshi
Discoverer
0 Kudos

I am using the below code to create sales order for multiple line items.

*Create sales order for given data.
LOOP AT ch_sales INTO DATA(lwa_sales).

*For every new customer ref order define header data for sales order.
AT NEW bstnk.

CLEAR: lwa_items,
lwa_itemsx.

CLEAR lwa_schedules.

*Clear items, partners and schedules for previous reference.
CLEAR: lt_items,
lt_itemsx,
lt_schedules_in,
lt_schedulesx,
lt_partners,
lwa_order_header_in,
lwa_order_header_inx.

WRITE lwa_sales-auart TO lwa_order_header_in-doc_type.
WRITE lwa_sales-vkorg TO lwa_order_header_in-sales_org.
WRITE lwa_sales-vtweg TO lwa_order_header_in-distr_chan.
WRITE lwa_sales-spart TO lwa_order_header_in-division.
WRITE lwa_sales-bstnk TO lwa_order_header_in-purch_no_c.

lwa_order_header_inx-updateflag = 'I'.
lwa_order_header_inx-doc_type = 'X'.
lwa_order_header_inx-sales_org = 'X'.
lwa_order_header_inx-distr_chan = 'X'.
lwa_order_header_inx-division = 'X'.
lwa_order_header_inx-purch_no_c = 'X'.
lwa_order_header_inx-purch_date = 'X'.
lwa_order_header_inx-req_date_h = 'X'.


ENDAT.

*Convert the date into the required format.
CONCATENATE lwa_sales-bstdk+6(4) lwa_sales-bstdk+3(2)
lwa_sales-bstdk+0(2) INTO lwa_order_header_in-purch_date.

CONCATENATE lwa_sales-vdatu+6(4) lwa_sales-vdatu+3(2)
lwa_sales-vdatu+0(2) INTO lwa_order_header_in-req_date_h.

*Define sold to party of the sales order.
lwa_partners-partn_role = lc_sold_to.
CALL FUNCTION 'CONVERSION_EXIT_ALPHA_INPUT'
EXPORTING
input = lwa_sales-vkunnr
IMPORTING
output = lwa_sales-vkunnr.
.
lwa_partners-partn_numb = lwa_sales-vkunnr.
APPEND lwa_partners TO lt_partners.
CLEAR lwa_partners.

*Define ship to party of the sales order.
lwa_partners-partn_role = lc_ship_to.
CALL FUNCTION 'CONVERSION_EXIT_ALPHA_INPUT'
EXPORTING
input = lwa_sales-kkunnr
IMPORTING
output = lwa_sales-kkunnr.
.

lwa_partners-partn_numb = lwa_sales-kkunnr.
APPEND lwa_partners TO lt_partners.
CLEAR lwa_partners.

*Convert the Material No. to the format.
CALL FUNCTION 'CONVERSION_EXIT_MATN1_INPUT'
EXPORTING
input = lwa_sales-matnr
IMPORTING
output = lwa_items-material_long.


ADD 10 TO lwa_items-itm_number.

*Define the target quantity for the given material.
MOVE lwa_sales-kwmeng TO lwa_items-target_qty.
APPEND lwa_items TO lt_items.

lwa_itemsx-updateflag = 'I'.
lwa_itemsx-itm_number = lwa_items-itm_number.
lwa_itemsx-material_long = 'X'.
lwa_itemsx-target_qty = 'X'.
APPEND lwa_itemsx TO lt_itemsx.

*Define the required quantity for the given material.
* ADD 1 TO lwa_schedules-sched_line.
lwa_schedules-itm_number = lwa_items-itm_number.
MOVE lwa_sales-kwmeng TO lwa_schedules-req_qty.
APPEND lwa_schedules TO lt_schedules_in.

lwa_schedulesx-sched_line = lwa_schedules-sched_line.
lwa_schedulesx-updateflag = 'I'.
lwa_schedulesx-itm_number = lwa_items-itm_number.
lwa_schedulesx-req_qty = 'X'.
APPEND lwa_schedulesx TO lt_schedulesx.
CLEAR lwa_schedulesx.

*Call the BAPI to create sales order.
AT END OF bstnk.

CALL FUNCTION 'BAPI_SALESORDER_CREATEFROMDAT2'
EXPORTING
order_header_in = lwa_order_header_in
order_header_inx = lwa_order_header_inx
IMPORTING
salesdocument = lv_sales_doc_no
TABLES
return = lt_return
order_items_in = lt_items
order_items_inx = lt_itemsx
order_partners = lt_partners
order_schedules_in = lt_schedules_in
order_schedules_inx = lt_schedulesx.

*Read table to check for errors and messages.
READ TABLE lt_return INTO DATA(lwa_return)
WITH KEY type = 'E'.
IF sy-subrc = 0.
MOVE lwa_return-message TO lwa_sales-msg.
ADD 1 TO ch_failure.
ELSE.
CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'
EXPORTING
wait = 'X'.

CONCATENATE TEXT-006 lv_sales_doc_no
INTO lwa_sales-msg SEPARATED BY space.
ADD 1 TO ch_success.
ENDIF.

*Update the messages to the corresponding field in internal table.
MODIFY ch_sales FROM lwa_sales TRANSPORTING msg
WHERE bstnk = lwa_sales-bstnk.

CLEAR lt_return.

ENDAT.

ENDLOOP.

If i dont pass ITM_NUMBER externally the sales order gets created with only one schedule line.

If i externally generate ITM_NUMBER i get the following error in LT_RETURN:

Invalid Schedule Lines while Schedule line processing:

2 REPLIES 2

Jelena
Active Contributor
0 Kudos

How many schedule lines did you expect? Schedule lines are actually generated by ATP. It's just in this BAPI for some reason SAP is forcing us to fill in the schedule line table just to enter quantity. (In VA01 it's unnecessary to visit the scheduling screen.) There could be one or many schedule lines generated in the order. It depends.

The error text doesn't ring a bell. Post the message ID and number.

There are many examples of this BAPI available on SCN. Use search and compare your code to other examples.

shreyank_joshi
Discoverer
0 Kudos

Thanks!

However, i solved the problem. I used 'I' as insert rather than 'U' for update flag and rest everything was taken care of itself. The sales order were successfully created.