How to use ABAP code to create Sales Order in S/4HANA
This article mainly introduces how to use ABAP code to create Sales Order in S/4HANA. The introduction in this article is very detailed and has certain reference value. Interested friends must read it!
Below is a screenshot of S/4HANA's Sales Order created using ABAP code:
The values in red are hardcoded in my code, and blue are created by the SD_SALESDEVICE_CREATE function itself.
Take a look at the code:
DATA: ls_header TYPE bapisdhd1,ls_headerx TYPE bapisdhd1x,lt_bapiret2 LIKE bapiret2 OCCURS 0 WITH HEADER LINE,po_order_number TYPE bapivbeln-vbeln,lt_partners TYPE TABLE OF bapiparnr,ls_partners LIKE LINE OF lt_partners.ls_header-serv_date = '20200101'.ls_header-doc_type = 'TA'. "Type of sales order ls_header-comp_cde_b = '0001'. "Company Code ls_headerx-comp_cde_b ='X '.ls_headerx-doc_type ='X'.ls_headerx-updateflag ='I '. "Specify modification mode as I-create ls_partners-part_role ='WE '. "ship to partyls_partners-partn_numb = '0000000001'. "This customer master must exist in database table KNA1 APPEND ls_partners TO lt_partners.ls_partners-partn_role ='AG '. " sold to partyls_partners-partn_numb = '0000000001'.APPEND ls_partners TO lt_partners. "Call BAPI: CALL FUNCTION 'SD_SALESDEVICE_CREATE'EXPORTINGsales_header_in = ls_headersales_header_inx = ls_headerxint_number_assignment = 'X'IMPORTINGsalesdocument_ex = po_order_numberTABLESreturn = lt_bapiret2sales_partners = lt_partners.LOOP AT lt_bapiret2 ASSIGNING FIELD-SYMBOL() WHERE type = 'E'.WRITE:/ 'Error:', -message COLOR COL_NEGATIVE.RETURN.ENDLOOP.IF po_order_number IS INITIAL.WRITE:/ 'PO number initial'.RETURN.ENDIF.CLEAR: lt_bapiret2.CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'EXPORTINGwait = 'X'IMPORTINGreturn = lt_bapiret2.
That's all for the article "How to create Sales Order in S/4HANA using ABAP code." Thank you for reading! Hope to share the content to help everyone, more relevant knowledge, welcome to pay attention to the industry information channel!