Sample code on BAPI SALESORDER CREATEFROMDAT2
REPORT zbapi_salesorder_create.
TABLES: vbak, vbap, likp, vbkd.
DATA: order_header_inx LIKE bapisdhd1x.
DATA: salesdocument LIKE bapivbeln-vbeln,
order_header_in LIKE bapisdhd1,
salesdocument_ex LIKE bapivbeln-vbeln,
return LIKE bapiret2 OCCURS 0 WITH HEADER LINE,
order_items_in LIKE bapisditm OCCURS 0 WITH HEADER LINE,
order_items_inx LIKE bapisditmx OCCURS 0 WITH HEADER LINE,
torder_partners LIKE bapiparnr OCCURS 0 WITH HEADER LINE,
order_schedules_in LIKE bapischdl OCCURS 0 WITH HEADER LINE,
order_schedules_inx LIKE bapischdlx OCCURS 0 WITH HEADER LINE,
order_conditions_in LIKE bapicond OCCURS 0 WITH HEADER LINE,
order_conditions_inx LIKE bapicondx OCCURS 0 WITH HEADER LINE.
PARAMETER: p_vbeln LIKE vbak-vbeln OBLIGATORY DEFAULT '1516000901',
"'1500000840' ,
p_kunnr LIKE kna1-kunnr DEFAULT '70000' .
SELECT SINGLE vkorg vtweg spart augru FROM vbak INTO
(vbak-vkorg, vbak-vtweg, vbak-spart ,vbak-augru )
WHERE vbeln = p_vbeln.
CLEAR order_header_in.
order_header_in-refobjtype = 'BUS2032'.
order_header_in-refobjkey = 'RAMIRO1'.
order_header_in-doc_type = 'ZESF'.
order_header_in-sales_org = vbak-vkorg.
order_header_in-distr_chan = vbak-vtweg.
order_header_in-division = vbak-spart.
order_header_in-ord_reason = vbak-augru.
order_header_in-purch_no_c = '.'.
REFRESH torder_partners.
torder_partners-partn_role = 'AG'.
torder_partners-partn_numb = p_kunnr.
APPEND torder_partners.
torder_partners-partn_role = 'WE'.
torder_partners-partn_numb = p_kunnr.
APPEND torder_partners.
REFRESH order_items_in.
REFRESH order_schedules_in.
SELECT posnr matnr kwmeng FROM vbap
INTO (vbap-posnr, vbap-matnr, vbap-kwmeng )
WHERE vbeln = p_vbeln.
CLEAR order_items_in.
order_items_in-itm_number = vbap-posnr.
order_items_in-refobjtype = 'BUS2032'.
order_items_in-refobjkey = 'RAMIRO1'.
order_items_in-material = vbap-matnr.
APPEND order_items_in.
CLEAR order_schedules_in.
order_schedules_in-itm_number = vbap-posnr.
order_schedules_in-refobjtype = 'BUS2032'.
order_schedules_in-refobjkey = 'RAMIRO1'.
order_schedules_in-req_qty = vbap-kwmeng.
APPEND order_schedules_in.
ENDSELECT.
CALL FUNCTION 'BAPI_SALESORDER_CREATEFROMDAT2'
EXPORTING
order_header_in = order_header_in
order_header_inx = order_header_inx
binary_relationshiptype = 'VORA' "Object references at item level "vorl hddr lev
int_number_assignment = 'X'
IMPORTING
salesdocument = salesdocument_ex
TABLES
return = return
order_items_in = order_items_in
order_items_inx = order_items_inx
order_partners = torder_partners
order_schedules_in = order_schedules_in
order_schedules_inx = order_schedules_inx
order_conditions_in = order_conditions_in.
IF sy-subrc EQ 0.
CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'
EXPORTING
wait = 'X'
* IMPORTING
* RETURN =
ENDIF.
p_vbeln = salesdocument_ex.
WRITE :/ 'sales document ' , p_vbeln.