Application Development Blog Posts
Learn and share on deeper, cross technology development topics such as integration and connectivity, automation, cloud extensibility, developing at scale, and security.
cancel
Showing results for 
Search instead for 
Did you mean: 
Former Member

Sometimes the task is not to create order through Bapi, because in this case it will be saved,
if not error occured. If you need to go into a standard transaction, passing
with some header information and items. Here are fragments of programs that
make it easy to do this:

1) Purchase Order.

  Imagine, you have
an ALV report with output table gt_outtab and columns banfn and bnfpo -
purchase requisition no and item. no.

  DATA : header                        LIKE mepoheader,
       mepo_doc       
TYPE mepo_document,
       requisitions   
TYPE  mereq_t_eban_mem,
       wa_requisitions
LIKE LINE OF requisitions,
       items          
LIKE  mepoitem OCCURS 0 WITH HEADER LINE .
DATAlr_selections   TYPE REF TO cl_salv_selections,
       lt_rows        
TYPE salv_t_row,
       ls_row         
LIKE LINE OF  lt_rows.


  lr_selections
= gr_table->get_selections( ).
  lt_rows
= lr_selections->get_selected_rows( ).
 
IF lt_rows IS INITIAL .
   
MESSAGE s001 WITH 'select any line' .
   
EXIT .
 
ENDIF.
 
CLEAR : items , items[]  .
 
LOOP AT lt_rows INTO l_row.
   
READ TABLE gt_outtab INDEX l_row .
   
CHECK gt_outtab-ebeln IS INITIAL .
   
ADD 10 TO items-ebelp .
    items
-banfn = gt_outtab-banfn .
    items
-bnfpo = gt_outtab-bnfpo .
   
APPEND items.
 
ENDLOOP .

* call me21n
 
CALL FUNCTION 'WB2_PO_PROCESS_START'
   
EXPORTING
      im_ekko              
= header
      im_aktyp             
= 'H'
   
IMPORTING
      ch_requisitions      
= requisitions
   
TABLES
      cht_items            
= items
   
CHANGING
      ch_document          
= mepo_doc
   
EXCEPTIONS
      invalid_call         
= 1
      invalid_activity_type
= 2
      done                 
= 3
      error                
= 4
     
OTHERS                = 5.

 
IF sy-subrc <> 0.
* Implement suitable error handling here
 
ENDIF.

2)Sales order basis on previous sales document. Vbeln - number of previous sales document.

  SELECT SINGLE * FROM vbak INTO wa_vbak
        
WHERE vbeln = vbeln.

CALL FUNCTION 'Z_MAP_VBAKKOM_2_HDRIN' " code of FM below
   
EXPORTING
      i_vbakkom      
= vbakkom
   
IMPORTING
      order_header_in
= sales_header_in.

  CLEAR : sales_partners , sales_partners[].

 
DATA t_vbpa     LIKE vbpa OCCURS 0 WITH HEADER LINE  .
 
SELECT * FROM vbpa INTO TABLE t_vbpa WHERE vbeln = wa_vbak-vbeln .
 
LOOP AT t_vbpa .
    sales_partners
-partn_role = t_vbpa-parvw.
   
IF NOT t_vbpa-kunnr IS INITIAL   .
      sales_partners
-partn_numb = t_vbpa-kunnr.
   
ELSEIF NOT t_vbpa-pernr IS INITIAL   .
      sales_partners
-partn_numb = t_vbpa-pernr .
   
ENDIF .
    sales_partners
-itm_number = t_vbpa-posnr .
   
APPEND sales_partners.
 
ENDLOOP.

* items
 
CLEAR : sales_items_in[] ,      sales_items_in,
          sales_schedules_in[]
sales_schedules_in,
          sales_conditions_in[]
, sales_conditions_in,
          tab_root_instances[]
root_instances.

   
SELECT * FROM vbap INTO wa_vbap WHERE vbeln = vbeln.

     sales_items_in-itm_number     = wa_vbap-posnr .
    sales_items_in
-material       = wa_vbap-matnr .
    sales_items_in
-reason_rej     = wa_vbap-abgru .
    sales_items_in
-ref_doc        = wa_vbap-vbeln .
    sales_items_in
-ref_doc_it     = wa_vbap-posnr .
    sales_items_in
-ref_doc_ca     = wa_vbak-vbtyp .
    sales_items_in
-plant          = wa_vbap-werks .
    sales_items_in
-store_loc      = wa_vbap-lgort .
    sales_items_in
-po_itm_no      = sales_items_in-itm_number .
   
IF vbtyp_v = 'B'. " next doc is contract
      sales_items_in
-target_qty     wa_vbap-orfmng  .
   
ELSE .
      sales_schedules_in
-itm_number     = sales_items_in-itm_number .
      sales_schedules_in
-sched_line     = 1 .
      sales_schedules_in
-req_qty        = wa_vbap-orfmng   .
     
APPEND sales_schedules_in .
   
ENDIF .
   
APPEND sales_items_in.

* conditions
   
DATA amount_external LIKE  bapicurr-bapicurr .
   
SELECT        * FROM  konv INTO wa_konv
                   
WHERE  knumv  = wa_vbak-knumv
                     
AND  kposn  = wa_vbap-posnr
                     
AND  kherk  = 'C'.    " manual conditions
     
IF wa_konv-krech = 'A' .  
        wa_konv
-kbetr = wa_konv-kbetr / 10 .
     
ENDIF .
     
MOVE-CORRESPONDING wa_konv TO komv.
     
CALL FUNCTION 'MAP2E_KOMV_TO_BAPISDCOND'
       
EXPORTING
          komv      
= komv
       
CHANGING
          bapisdcond
= bapisdcond.

     
MOVE-CORRESPONDING bapisdcond TO sales_conditions_in .
     
IF bapisdcond-currency IS NOT INITIAL .
       
CALL FUNCTION 'BAPI_CURRENCY_CONV_TO_EXTERNAL'
         
EXPORTING
           
currency        = bapisdcond-currency
            amount_internal
= bapisdcond-cond_value
         
IMPORTING
            amount_external
= amount_external.
        sales_conditions_in
-cond_value = amount_external.
     
ENDIF .

      sales_conditions_in
-itm_number = sales_items_in-itm_number .
     
APPEND sales_conditions_in .
   
ENDSELECT.

   
IF tc = 'X'  .   " configuration if vehicle, variable tc is flag - material is vehicle

       root_instances-instance_no = wa_vbap-cuobj .
      root_instances
-posex  = sales_items_in-po_itm_no  .   
     
APPEND  root_instances TO tab_root_instances .
   
ENDIF .

 
ENDSELECT .

   IF tc = 'X'  .   " read configuration of previous doc. if it is vehicle
   
CALL FUNCTION 'CUXI_GET_MULTI_CONFIGURATION'
     
EXPORTING
        i_tab_root_instances        
= tab_root_instances
     
TABLES
        e_tab_cfg_headers           
= e_tab_cfg_headers
        e_tab_instances             
= e_tab_instances
        e_tab_part_of               
= e_tab_part_of
        e_tab_values                
= e_tab_values
        e_tab_var_keys              
= e_tab_var_keys
     
EXCEPTIONS
        invalid_instance            
= 1
        instance_is_a_classification
= 2
        no_root_instance            
= 3
        internal_error              
= 4
        invalid_data                
= 5
       
OTHERS                       = 6.


** clear some fields
   
LOOP AT e_tab_cfg_headers INTO  cfg_headers .
     
CLEAR :   cfg_headers-sce ,
                cfg_headers
-kbname,
                cfg_headers
-kbversion,
                cfg_headers
-complete,
                cfg_headers
-consistent,
                cfg_headers
-cfginfo,
                cfg_headers
-kbprofile,
                cfg_headers
-kblanguage,
                cfg_headers
-cbase_id,
                cfg_headers
-cbase_id_type .
     
MODIFY e_tab_cfg_headers  FROM cfg_headers.
   
ENDLOOP .

   
LOOP AT e_tab_instances INTO  instances .
     
CLEAR : instances-obj_txt ,
              instances
-quantity ,
              instances
-author ,
              instances
-quantity_unit ,
              instances
-complete ,
              instances
-consistent ,
              instances
-object_guid ,
              instances
-persist_id ,
              instances
-persist_id_type .
     
MODIFY e_tab_instances FROM instances .
   
ENDLOOP .

   
LOOP AT e_tab_values INTO values.
     
CLEAR  values-valcode .
     
MODIFY e_tab_values FROM  values .
   
ENDLOOP.
 
ENDIF .

* header conditions
 
SELECT        * FROM  konv INTO wa_konv
                 
WHERE  knumv  = wa_vbak-knumv
                   
AND  kposn  = '000000'
                   
AND  kherk  = 'C'.    " manual
   
IF wa_konv-krech = 'A' .        

      wa_konv-kbetr = wa_konv-kbetr / 10 .
   
ENDIF .
   
MOVE-CORRESPONDING wa_konv TO komv.
   
CALL FUNCTION 'MAP2E_KOMV_TO_BAPISDCOND'
     
EXPORTING
        komv      
= komv
     
CHANGING
        bapisdcond
= bapisdcond.

   
MOVE-CORRESPONDING bapisdcond TO sales_conditions_in .
    sales_conditions_in
-itm_number = '000000' .
   
APPEND sales_conditions_in .
 
ENDSELECT.

CALL FUNCTION 'BAPI_SALESDOCU_CREATEWITHDIA'
 
EXPORTING
    sales_header_in              
= sales_header_in

   synchronous                   = 'X'

   IMPORTING
   salesdocument_ex             
= vbeln_so
TABLES
  
return                        = return
   sales_items_in               
= sales_items_in

    sales_partners                = sales_partners
   sales_schedules_in           
= sales_schedules_in
   sales_conditions_in          
= sales_conditions_in
   sales_cfgs_ref               
= e_tab_cfg_headers
   sales_cfgs_inst              
= e_tab_instances
   sales_cfgs_value             
= e_tab_values .

  FUNCTION Z_MAP_VBAKKOM_2_HDRIN.
*"----------------------------------------------------------------------
*"*"Локальный интерфейс:
*"  IMPORTING
*"     REFERENCE(I_VBAKKOM) TYPE  VBAKKOM
*"  EXPORTING
*"     REFERENCE(ORDER_HEADER_IN) TYPE  BAPISDHEAD1
*"----------------------------------------------------------------------
 
MOVE i_vbakkom-auart TO order_header_in-doc_type        .
 
MOVE i_vbakkom-submi TO order_header_in-collect_no      .
 
MOVE i_vbakkom-vkorg TO order_header_in-sales_org       .
 
MOVE i_vbakkom-vtweg TO order_header_in-distr_chan      .
 
MOVE i_vbakkom-spart TO order_header_in-division        .
 
MOVE i_vbakkom-vkgrp TO order_header_in-sales_grp       .
 
MOVE i_vbakkom-vkbur TO order_header_in-sales_off       .
 
MOVE i_vbakkom-vdatu TO order_header_in-req_date_h      .
 
MOVE i_vbakkom-vprgr TO order_header_in-date_type       .
 
MOVE i_vbakkom-bsark TO order_header_in-po_method       .
 
MOVE i_vbakkom-bstdk TO order_header_in-purch_date      .
 
MOVE i_vbakkom-bstzd TO order_header_in-po_supplem      .
 
MOVE i_vbakkom-ihrez TO order_header_in-ref_1           .
 
MOVE i_vbakkom-bname TO order_header_in-name            .
 
MOVE i_vbakkom-telf1 TO order_header_in-telephone       .
 
MOVE i_vbakkom-konda TO order_header_in-price_grp       .
 
MOVE i_vbakkom-kdgrp TO order_header_in-cust_group      .
 
MOVE i_vbakkom-bzirk TO order_header_in-sales_dist      .
 
MOVE i_vbakkom-pltyp TO order_header_in-price_list      .
 
MOVE i_vbakkom-inco1 TO order_header_in-incoterms1      .
 
MOVE i_vbakkom-inco2 TO order_header_in-incoterms2      .
 
MOVE i_vbakkom-zterm TO order_header_in-pmnttrms        .
 
MOVE i_vbakkom-lifsk TO order_header_in-dlv_block       .
 
MOVE i_vbakkom-faksk TO order_header_in-bill_block      .
 
MOVE i_vbakkom-augru TO order_header_in-ord_reason      .
 
MOVE i_vbakkom-autlf TO order_header_in-compl_dlv       .
 
MOVE i_vbakkom-prsdt TO order_header_in-price_date      .
 
MOVE i_vbakkom-angdt TO order_header_in-qt_valid_f      .
 
MOVE i_vbakkom-bnddt TO order_header_in-qt_valid_t      .
 
MOVE i_vbakkom-guebg TO order_header_in-ct_valid_f      .
 
MOVE i_vbakkom-gueen TO order_header_in-ct_valid_t      .
 
MOVE i_vbakkom-kvgr1 TO order_header_in-cust_grp1       .
 
MOVE i_vbakkom-kvgr2 TO order_header_in-cust_grp2       .
 
MOVE i_vbakkom-kvgr3 TO order_header_in-cust_grp3       .
 
MOVE i_vbakkom-kvgr4 TO order_header_in-cust_grp4       .
 
MOVE i_vbakkom-kvgr5 TO order_header_in-cust_grp5       .
 
MOVE i_vbakkom-bstkd TO order_header_in-purch_no_c      .
 
MOVE i_vbakkom-bstkd_e TO order_header_in-purch_no_s      .
 
MOVE i_vbakkom-bstdk_e TO order_header_in-po_dat_s        .
 
MOVE i_vbakkom-bsark_e TO order_header_in-po_meth_s       .
 
MOVE i_vbakkom-ihrez_e TO order_header_in-ref_1_s         .
 
MOVE i_vbakkom-audat TO order_header_in-doc_date        .
 
MOVE i_vbakkom-gwldt TO order_header_in-war_date        .
 
MOVE i_vbakkom-vsbed TO order_header_in-ship_cond       .
 
MOVE i_vbakkom-ktext TO order_header_in-pp_search       .
 
MOVE i_vbakkom-mahza TO order_header_in-dun_count       .
 
MOVE i_vbakkom-mahdt TO order_header_in-dun_date        .
 
MOVE i_vbakkom-abrvw TO order_header_in-dlvscheduse     .
 
MOVE i_vbakkom-abdis TO order_header_in-plan_dlv_schtype.
 
MOVE i_vbakkom-vgbel TO order_header_in-ref_doc         .
 
MOVE i_vbakkom-bukrs_vf TO order_header_in-comp_code_b     .
 
MOVE i_vbakkom-taxk1 TO order_header_in-alt_tax_class   .
 
MOVE i_vbakkom-taxk2 TO order_header_in-tax_class2      .
 
MOVE i_vbakkom-taxk3 TO order_header_in-tax_class3      .
 
MOVE i_vbakkom-taxk4 TO order_header_in-tax_class4      .
 
MOVE i_vbakkom-taxk5 TO order_header_in-tax_class5      .
 
MOVE i_vbakkom-taxk6 TO order_header_in-tax_class6      .
 
MOVE i_vbakkom-taxk7 TO order_header_in-tax_class7      .
 
MOVE i_vbakkom-taxk8 TO order_header_in-tax_class8      .
 
MOVE i_vbakkom-taxk9 TO order_header_in-tax_class9      .
 
MOVE i_vbakkom-xblnr TO order_header_in-ref_doc_l       .
 
MOVE i_vbakkom-zuonr TO order_header_in-***_number      .
 
MOVE i_vbakkom-vgtyp TO order_header_in-ref_doc_cat     .
 
MOVE i_vbakkom-kzazu TO order_header_in-ord_comb_in     .
 
MOVE i_vbakkom-perfk TO order_header_in-bill_sched      .
 
MOVE i_vbakkom-perrl TO order_header_in-invo_sched      .
 
MOVE i_vbakkom-mrnkz TO order_header_in-mn_invoice      .
 
MOVE i_vbakkom-kurrf TO order_header_in-exch_rate_fi    .
 
MOVE i_vbakkom-valtg TO order_header_in-add_val_dy      .
 
MOVE i_vbakkom-valdt TO order_header_in-fix_val_dy      .
 
MOVE i_vbakkom-zlsch TO order_header_in-pymt_meth       .
 
MOVE i_vbakkom-ktgrd TO order_header_in-accnt_asgn      .
 
MOVE i_vbakkom-kursk TO order_header_in-exchg_rate      .
 
MOVE i_vbakkom-fkdat TO order_header_in-bill_date       .
 
MOVE i_vbakkom-fbuda TO order_header_in-serv_date       .
 
MOVE i_vbakkom-mschl TO order_header_in-dunn_key        .
 
MOVE i_vbakkom-mansp TO order_header_in-dunn_block      .
 
MOVE i_vbakkom-abssc TO order_header_in-pymt_gar_proc   .
 
MOVE i_vbakkom-abtnr TO order_header_in-department_no   .
 
MOVE i_vbakkom-empst TO order_header_in-rec_point       .
 
MOVE i_vbakkom-lcnum TO order_header_in-doc_num_fi      .
 
MOVE i_vbakkom-kdkg1 TO order_header_in-cust_cond_grp1  .
 
MOVE i_vbakkom-kdkg2 TO order_header_in-cust_cond_grp2  .
 
MOVE i_vbakkom-kdkg3 TO order_header_in-cust_cond_grp3  .
 
MOVE i_vbakkom-kdkg4 TO order_header_in-cust_cond_grp4  .
 
MOVE i_vbakkom-kdkg5 TO order_header_in-cust_cond_grp5  .
 
MOVE i_vbakkom-delco TO order_header_in-dlv_time        .
 
MOVE i_vbakkom-waerk TO order_header_in-CURRENCY        .
 
MOVE i_vbakkom-ernam TO order_header_in-created_by      .
 
MOVE i_vbakkom-landtx TO order_header_in-tax_depart_cty  .
 
MOVE i_vbakkom-stceg_l TO order_header_in-tax_dest_cty    .
 
MOVE i_vbakkom-xegdr TO order_header_in-eu_triang_deal  .
 
MOVE i_vbakkom-vbeln_grp TO order_header_in-master_contr    .
 
MOVE i_vbakkom-scheme_grp TO order_header_in-ref_proc        .
 
MOVE i_vbakkom-abruf_part TO order_header_in-check_partn_auth.
 
MOVE i_vbakkom-dat_fzau TO order_header_in-cml_qty_date    .
 
MOVE i_vbakkom-vsnmr_v TO order_header_in-version         .
 
MOVE i_vbakkom-qmnum TO order_header_in-notif_no        .
 
MOVE i_vbakkom-vkont TO order_header_in-fkk_conacct     .

ENDFUNCTION.

Labels in this area