tables: bapimepoheader,
        bapimepoheaderx
.

data: it_bapimepoitem type bapimepoitem occurs 0 with header line,
      it_bapimepoitemx 
type bapimepoitemx occurs 0 with header line,
      it_poaccount 
type bapimepoaccount occurs 0 with header line,
      it_poaccountx 
type bapimepoaccountx occurs 0 with header line.
data: it_return2 like bapiret2 occurs 0 with header line.
data: flines like tline occurs 0 with header line,
      string
(132) type c, fobject like thead-tdobject, fname  like thead-tdname.

data: line_no type i.
data: vendor_no type lifnr.

data: ntgew type ekpo-ntgew.
data: brgew type ekpo-brgew.

data: str type string.
data: str1 type string.

data: v_plant(4) type c,
      v_whno
(3) type c,
      v_sorg
(4) type c,
      v_dplnt
(4) type c,
      v_ccode
(4) type c,
      v_curr
(3) type c,
      v_shippt
(4) type c,
      v_country
(2) type c.

data: temp_char(200) type c.

data: banfn type vbep-banfn. " for purchase requisition number.

  
select single banfn into banfn from vbep where vbeln = zsalesdocument. " fetching purchse requisition number

  
" for PO Header data fill

   bapimepoheader
-comp_code = t001-bukrs.
   bapimepoheader
-doc_type = 'ZCOM'.
   bapimepoheader
-item_intvl = '00010'.

   bapimepoheader
-vendor = VENDOR.
   bapimepoheader
-pmnttrms = '0001'.
   bapimepoheader
-purch_org = ‘ABC’.
   bapimepoheader
-pur_group = 'E15'.
   bapimepoheader
-currency = 'USD'.
   
"bapimepoheader-ref_1 = wa_asntab-asnnumber.
   bapimepoheader
-incoterms2 = 'inco2'.

   bapimepoheaderx
-comp_code = 'X'.
   bapimepoheaderx
-doc_type = 'X'.
   bapimepoheaderx
-item_intvl = 'X'.
   bapimepoheaderx
-vendor = 'X'.
   bapimepoheaderx
-pmnttrms = 'X'.
   bapimepoheaderx
-purch_org = 'X'.
   bapimepoheaderx
-pur_group = 'X'.
   bapimepoheaderx
-currency = 'X'.
   
"bapimepoheaderx-ref_1 = 'X'.
   bapimepoheaderx
-incoterms2 = 'X'.

  
" for PO Line item Data FIll

    line_no 
= line_no + 10.
    it_bapimepoitem
-po_item = '00010'.
    it_bapimepoitem
-material = material.
    it_bapimepoitem
-plant = PLANT.
    it_bapimepoitem
-quantity = '1'.
    it_bapimepoitem
-free_item = 'X'.
    it_bapimepoitem
-conf_ctrl = '0004'.
    it_bapimepoitem
-incoterms2 = 'inco2'.

    it_bapimepoitem
-gross_wt = brgew.  “ gross weight

    it_bapimepoitem
-preq_no = banfn. “ purchase requisition number

    it_bapimepoitem
-net_weight = ntgew. “ net weight

    
append it_bapimepoitem to it_bapimepoitem.
    
clear it_bapimepoitem.

    it_bapimepoitemx
-po_item = '00010'.
    it_bapimepoitemx
-material = 'X'.
    it_bapimepoitemx
-plant = 'X'.
    it_bapimepoitemx
-quantity = 'X'.
    it_bapimepoitemx
-free_item = 'X'.
    it_bapimepoitemx
-conf_ctrl = 'X'.
    it_bapimepoitemx
-incoterms2 = 'X'.
    it_bapimepoitemx
-gross_wt = 'X'.
    it_bapimepoitemx
-preq_no  = 'X'.
    it_bapimepoitemx
-net_weight = 'X'.
    
append it_bapimepoitemx to it_bapimepoitemx.
    
clear it_bapimepoitemx.
    
clear: temp_char.

    it_poaccount
-po_item = '00010'.
    it_poaccount
-serial_no = '01'.
    it_poaccount
-quantity = '1'.

    it_poaccount
-costcenter = cost_center.
    
append it_poaccount to it_poaccount.

    it_poaccountx
-po_item = '00010'.
    it_poaccountx
-serial_no = '01'.
    it_poaccountx
-quantity = 'X'.
    it_poaccountx
-costcenter = 'X'.
    
append it_poaccountx to it_poaccountx.


    
clear po_number.

    
call function 'BAPI_PO_CREATE1'
    
exporting
      poheader         
= bapimepoheader
      poheaderx        
= bapimepoheaderx
    
importing
      exppurchaseorder 
= po_number
    
tables
      
return           = it_return2
      poitem           
= it_bapimepoitem
      poitemx          
= it_bapimepoitemx
      poaccount        
= it_poaccount
      poaccountx        
= it_poaccountx.

   
call function 'BAPI_TRANSACTION_COMMIT'
    
exporting
      
wait   = 'X'
    
importing
      
return = return.


Serkan AKKAVAK
Computer Engineer
ABAP Developer & SAP S/4 HANA Logistics Consultant
Contact : serkurumsal@yandex.com