Sunday, 6 October 2024

Bapi for Add, Change and Delete Material in Tcode VA02

 





Tcode SE37 : Function Builder
  - Create Bapi Name  BAPI_CHANGE_SVO
  In tab  

   Import :






 Tables :





Source :

    Table Definition :

*
  

types:
    begin of ty_data,
          col_01 type resb-potx1,"(025), "(025),
          col_02(055),
          col_03(018),
          col_04(025),
          col_05(025),
          col_06(050),
          col_07(025),
          col_08(025),
          col_09(010),
          col_10(025),
          col_11(025),
          col_12(4type n,
          col_13(018),
          col_14(025),
          col_15(025),
          col_16(050),
          col_17(025),
          col_18(025),
          col_19(040),
          col_20(010),
          col_21 type aufnr,
          col_22 type lifnr,
          col_23 type eban-bnfpo,
          col_24(1),  "Indicator Update or Delete,
          col_25(1),  "Status
          col_26(25),  "Sold to Party
          rsnum(6type n"resb-rsnum,
          rspos(4type n,"resb-rspos,
          banfn type ebkn-banfn,
          bnfpo type ebkn-bnfpo,
          loekz type ebkn-loekz,
          ktext type aufk-ktext,
     end of ty_data,

     begin of ty_resb,
       aufnr type aufnr,
       rsnum(6type n,
       rspos(4type n,
       status(1),
       lnitm(4type n,
     end of ty_resb,

      begin of ty_eban,
       banfn type eban-banfn,
       matnr type eban-matnr,
       bnfpo type ebkn-bnfpo,
       loekz type ebkn-loekz,
       aufnr type ebkn-aufnr,
     end of ty_eban.

data it_dat1 type table of ty_data with header line,
       wa_dat1 type ty_data,

       it_resb type table of ty_resb with header line,
       wa_resb type ty_resb,

       it_eban type table of ty_eban with header line,
       wa_eban type ty_eban,

       g_refnum(6type n,
       g_rsnum type rsnum,
       g_kunum type kunum,
       g_kunum_old type kunum.

       field-symbols <it_dat1> type ty_data,
                       <it_eban> type ty_eban,
                       <it_resb> type ty_resb.


Source

   
*Bapi's
  data lt_return type standard table of bapiret2.
  data ls_return like line of  lt_return .

  data lt_methods  type standard table of bapi_alm_order_method.
  data ls_methods like line of lt_methods.

  data lt_component type standard table of bapi_alm_order_component.
  data ls_component like line of lt_component.

  data lt_component_up type standard table of bapi_alm_order_component_up .
  data ls_component_up like line of lt_component_up.

  data lt_numbers type standard table of bapi_alm_numbers.

  data lt_d_components  type table of bapi_alm_order_component_e.
  data ls_d_components  type bapi_alm_order_component_e.

  data lt_componentx  type table of bapi_alm_order_component_up.
  data ls_componentx    type bapi_alm_order_component_up.

  data lt_d_partner  type table of bapi_alm_order_partner.
  data ls_d_partner  type bapi_alm_order_partner.

  data lt_d_partner_n  type table of bapi_alm_order_partn_mul.
  data ls_d_partner_n  type bapi_alm_order_partn_mul.

  data lt_d_partner_up  type table of bapi_alm_order_partn_mul_up.
  data ls_d_partner_up  type bapi_alm_order_partn_mul_up.

  data lt_d_header type table of bapi_alm_order_headers_i.
  data ls_d_header type bapi_alm_order_headers_i.

  clear wa_dat1.
  refresh it_dat1.
  loop at i_detail.

    wa_dat1-col_01 i_detail-external_key.
    wa_dat1-col_02 i_detail-operation_code.
    wa_dat1-col_03 i_detail-control_key.
    wa_dat1-col_04 i_detail-operation_short_text.
    wa_dat1-col_05 i_detail-qty_open.
    wa_dat1-col_06 i_detail-price.
    wa_dat1-col_07 i_detail-material_group.
    wa_dat1-col_08 i_detail-purchasing_group.
    wa_dat1-col_09 i_detail-sales_org.
    wa_dat1-col_10 i_detail-vendor_code.
    wa_dat1-col_11 i_detail-tracking_no.
    wa_dat1-col_12 i_detail-component_item_no.
    wa_dat1-col_13 i_detail-material_code.
    wa_dat1-col_14 i_detail-qty.
    wa_dat1-col_15 i_detail-storage_loc.
    wa_dat1-col_16 i_detail-plant.
    wa_dat1-col_17 i_detail-uom.
    wa_dat1-col_18 i_detail-activity_start_time.
    wa_dat1-col_19 i_detail-activity_start_time.
    wa_dat1-col_21 salesorder.
    wa_dat1-col_20 i_detail-cost_center.
    wa_dat1-col_23 i_detail-line_item_pr.
    wa_dat1-col_24 i_detail-change_id.
    wa_dat1-col_25 i_detail-status.
    wa_dat1-col_26 i_detail-kode_stp.
    g_kunum wa_dat1-col_26.

    select single lifnr into wa_dat1-col_22
      from resb
    where aufnr salesorder.
*      and rspos = wa_dat1-col_12.

    append wa_dat1 to it_dat1.
  endloop.

  refresh lt_methods,lt_component,lt_component_up.
  clear ls_methods.
  clear ls_component.
  clear ls_component_up.

*Get Reservation No.
  loop at it_dat1 assigning <it_dat1>.
    select single rsnum rspos meins
           into (<it_dat1>-rsnum,<it_dat1>-rspos,<it_dat1>-col_20)
           from resb
           where potx1 eq <it_dat1>-col_01
             and aufnr eq salesorder.
    g_rsnum <it_dat1>-rsnum.
    modify it_dat1  from <it_dat1> .
  endloop.
**

  if g_rsnum is not initial.
* Get SVO
*  Clear salesorder.
    loop at it_dat1 assigning  <it_dat1>.
      if <it_dat1>-col_20 is initial.
        <it_dat1>-col_20 <it_dat1>-col_10.
      endif.
    endloop.

    if salesorder is not initial.
* Get data for Delete
      select from resb into corresponding fields of table it_resb
          where aufnr eq salesorder.

* Compare exit and Delete
      sort it_resb by  rsnum rspos.
      sort it_dat1 by  rsnum rspos.
      loop at it_resb assigning <it_resb>.
        read table it_dat1 assigning <it_dat1> with key rsnum <it_resb>-rsnum
                                                        rspos <it_resb>-rspos
                                               binary search.
        if sy-subrc eq 0.
          <it_resb>-lnitm <it_dat1>-col_12.
          if  <it_dat1>-col_24 'D'.
            <it_resb>-status <it_dat1>-col_24.
          else.
            <it_resb>-status <it_dat1>-col_24.
          endif.
        endif.
      endloop.

*  Delete Component
      sort it_resb by rsnum rspos.
      delete it_resb where status ne 'D'.
    endif.

**************  Add Component  ***************************
    g_refnum 1.
    clear ls_component,ls_methods,lt_return.
    refresh lt_component,lt_methods,lt_return.
    loop at it_dat1 into wa_dat1 where rsnum eq '0000000000'
                                  and  col_24 'U'.
*-- Component Mapping
      add to ls_methods-refnumber.

      ls_methods-refnumber =  g_refnum"wa_dat1-col_22 .
      ls_methods-objecttype 'COMPONENT'.
      ls_methods-method     'CREATE'.
      ls_methods-objectkey  salesorder.
      append ls_methods to lt_methods.

      clear ls_methods.
      ls_methods-refnumber =  g_refnum.
      ls_methods-method 'SAVE'.
      append ls_methods to lt_methods.

      ls_component-activity '0010'.
      ls_component-material wa_dat1-col_13.
*    ls_component-price    = 0 / 100.
      ls_component-price_unit '1'.
      ls_component-requirement_quantity wa_dat1-col_14.

*Mrp
      if wa_dat1-col_04(3'EXT'.
        concatenate '000' wa_dat1-col_10 into wa_dat1-col_10.
        ls_component-vendor_no  wa_dat1-col_10.
      else.  "Internal
        if  wa_dat1-col_20 is not initial.
          ls_component-mrp_relevant '1'.
        endif.
      endif.

      ls_component-preq_name sy-uname.
      ls_component-plant     '2000'.
      ls_component-pur_group wa_dat1-col_08.
      ls_component-matl_group wa_dat1-col_07.
      ls_component-purch_org  '1000'.
      ls_component-item_number wa_dat1-col_12.
      ls_component-trackingno =  wa_dat1-col_11.
      ls_component-stge_loc wa_dat1-col_15.
      ls_component-item_text1 wa_dat1-col_01.
      append ls_component to lt_component.

      g_refnum g_refnum + 1.
    endloop.

    clear ls_component_up.
    ls_component_up-activity 'X'.
    ls_component_up-material 'X'.
    ls_component_up-price    'X'.
    ls_component_up-price_unit 'X'.
    ls_component_up-requirement_quantity =  'X'.
    ls_component_up-requirement_quantity_unit =  'X'.
    ls_component_up-vendor_no 'X'.
    ls_component_up-preq_name =  'X'.

    ls_component_up-mrp_relevant =  'X'.
    ls_component_up-plant =  'X'.
    ls_component_up-matl_group =  'X'.
    ls_component_up-purch_org =  'X'.
    ls_component_up-item_number 'X'.
    ls_component_up-stge_loc 'X'.
    ls_component_up-trackingno 'X'.
    ls_component_up-item_text1 'X'.
    append ls_component_up to lt_component_up.

    call function 'BAPI_ALM_ORDER_MAINTAIN'
      tables
        it_methods      lt_methods
        it_component    lt_component
        it_component_up lt_component_up
        return          lt_return
        et_numbers      lt_numbers.

    call function 'BAPI_TRANSACTION_COMMIT'
      exporting
        wait 'X'.

  endif.

* **************  Change  ***************************
* Get data
  call function 'BAPI_ALM_ORDER_GET_DETAIL'
    destination 'NONE'
    exporting
      number        salesorder
      it_header     lt_d_header
    tables
      et_components lt_d_components[]
      et_partner    lt_d_partner[]
      return        lt_return[].


  if g_kunum is initial.
* **************  Change Component  ***************************
    clear ls_d_components.
    sort it_dat1 by col_12.
    loop at lt_d_components into ls_d_components.
      read table it_dat1 assigning <it_dat1>
                         with key col_12 ls_d_components-item_number
                         binary search.
      if sy-subrc eq 0.
        ls_d_components-requirement_quantity <it_dat1>-col_14.
        if <it_dat1>-col_15 is initial.
          ls_d_components-stge_loc <it_dat1>-col_15.
        endif.
        modify lt_d_components from ls_d_components.
      endif.
    endloop.

    g_refnum 1.
    clear ls_component,ls_methods,lt_return.
    refresh lt_component,lt_methods,lt_return.
    sort lt_d_components  by item_number.
    loop at lt_d_components into ls_d_components.

*-- Component Mapping
      ls_methods-refnumber =  g_refnum .
      ls_methods-objecttype 'COMPONENT'.
      ls_methods-method     'CHANGE'.
      concatenate salesorder '0010'
                 into ls_methods-objectkey.
      append ls_methods to lt_methods.
*
      clear ls_methods.
      ls_methods-method 'SAVE'.
      append ls_methods to lt_methods.

      ls_component-reserv_no ls_d_components-reserv_no.
      ls_component-res_item  ls_d_components-res_item.
      ls_component-material  ls_d_components-material.
      ls_component-item_number =  ls_d_components-item_number .
      ls_component-activity  '0010'.
      ls_component-plant     '2000'.
      ls_component-pur_group ls_d_components-pur_group.

      ls_component-requirement_quantity      ls_d_components-requirement_quantity .
      ls_component-requirement_quantity_unit ls_d_components-requirement_quantity_unit.
      ls_component-item_text1 ls_d_components-item_text1.
      ls_component-vendor_no  ls_d_components-vendor_no.
      ls_component-trackingno ls_d_components-trackingno.
      ls_component-stge_loc   ls_d_components-stge_loc.

      ls_componentx-material  'X'.
      ls_componentx-item_number 'X'.
      ls_componentx-activity  ='X'.
      ls_componentx-plant     'X'.
      ls_componentx-pur_group 'X'.

      ls_componentx-requirement_quantity      'X'.
      ls_componentx-requirement_quantity_unit 'X'.
      ls_componentx-item_text1 'X'.
      ls_componentx-vendor_no  'X'.
      ls_componentx-trackingno 'X'.

      ls_componentx-stge_loc   'X'.

      append ls_componentx to lt_componentx.
      append ls_component to lt_component.

      add to g_refnum.
    endloop.

    call function 'BAPI_ALM_ORDER_MAINTAIN'
      tables
        it_methods      lt_methods
        it_component    lt_component
        it_component_up lt_componentx "lt_component_up
        return          lt_return
        et_numbers      lt_numbers.

    call function 'BAPI_TRANSACTION_COMMIT'
      exporting
        wait 'X'.
***************  Change Sold to Party  ***************************
  else.
    .
    clear ls_methods,lt_return.
    refresh lt_methods,lt_return.
*Old Partner
    loop at lt_d_partner into ls_d_partner.
      g_kunum_old ls_d_partner-partner_old.
    endloop.


*Table methods
    ls_methods-refnumber '1'.
    ls_methods-objecttype 'PARTNER'.
    ls_methods-method 'CHANGE'.
    ls_methods-objectkey salesorder.
    append ls_methods to lt_methods.

    ls_methods-refnumber '2'.
    ls_methods-objecttype ''.
    ls_methods-method 'SAVE'.
    ls_methods-objectkey salesorder.
    append ls_methods to lt_methods.

    ls_d_partner_n-orderid salesorder.
    ls_d_partner_n-partn_role 'AG'.
    ls_d_partner_n-partn_role_old 'AG'.
    ls_d_partner_n-partner g_kunum.
    ls_d_partner_n-partner_old =  g_kunum_old.
    append ls_d_partner_n to lt_d_partner_n.

    ls_d_partner_up-orderid salesorder.
    ls_d_partner_up-partn_role 'X'.
    ls_d_partner_up-partner 'X'.
    ls_d_partner_up-partn_role_old 'AG'.
    ls_d_partner-partner_old =   g_kunum_old.
    append ls_d_partner_up to lt_d_partner_up.

    call function 'BAPI_ALM_ORDER_MAINTAIN'
      tables
        it_methods    lt_methods
        it_partner    lt_d_partner_n
        it_partner_up lt_d_partner_up
        return        lt_return.

    call function 'BAPI_TRANSACTION_COMMIT'
      exporting
        wait 'X'.
  endif.
************  Delete Component  ***************************

  g_refnum 1.
  clear ls_component,ls_methods,lt_return.
  refresh lt_component,lt_methods.
  if it_resb[] is not initial.
    clear ls_component,ls_methods,lt_return.
    refresh lt_component,lt_methods.
    loop at it_resb into wa_resb .
*-- Component Mapping
      ls_methods-refnumber =  g_refnum"1.
      ls_methods-objecttype 'COMPONENT'.
      ls_methods-method     'DELETE'.

      concatenate salesorder '0010'
             into ls_methods-objectkey.

      append ls_methods to lt_methods.

      ls_component-reserv_no =   wa_resb-rsnum.
      ls_component-res_item  =   wa_resb-rspos.
      ls_component-item_number wa_resb-lnitm.
      ls_component-activity '0010'.

      ls_component_up-item_number 'X'.

      append ls_component to lt_component.
      append ls_component_up to lt_component_up.

      clear ls_methods.
      ls_methods-method 'SAVE'.
      append ls_methods to lt_methods.

      add to g_refnum.
    endloop.

    call function 'BAPI_ALM_ORDER_MAINTAIN'
      tables
        it_methods      lt_methods
        it_component    lt_component
        it_component_up lt_component_up
        return          lt_return
        et_numbers      lt_numbers.


    call function 'BAPI_TRANSACTION_COMMIT'
      exporting
        wait 'X'.
  endif.

* Get PR Doc
  select eban~banfn eban~matnr
         eban~bnfpo eban~loekz ebkn~aufnr
        into corresponding fields of table it_eban
             from eban
            inner join ebkn on ebkn~banfn eq eban~banfn
                           and ebkn~bnfpo eq eban~bnfpo
            where ebkn~aufnr eq salesorder.

* Update Data
  loop at it_dat1 assigning <it_dat1>.
    read table it_eban assigning <it_eban> with key
                                               aufnr salesorder
                                               bnfpo <it_dat1>-col_23
                                               binary search.
    if sy-subrc eq 0.
      <it_dat1>-banfn <it_eban>-banfn.
      <it_dat1>-bnfpo <it_eban>-bnfpo.
      <it_dat1>-loekz <it_eban>-loekz.
    endif.

* SVO Des
    select single ktext
       into <it_dat1>-ktext
       from aufk
      where aufnr salesorder.
  endloop.




        
 






Monday, 30 September 2024

Using Tcode DP95 for Create Sales Order from Service Order - subtitute tcode DP90 Using SM35 - Recording

   Tcode DP90 can not use for Batch Input and Background, Try Other tcode with some trick, Using variant Tcode DP95 for Uncheck Test Run because if this Checkbox is Tic, we can not create Sales Order



The Step are :
 - Createa Variant di DP95, Any Variant.
    Using SM35 to Recording Tcode DP95,

   Then get variant Program :

  Salesorder are SVO Number.


 constants c_space  type value cl_abap_char_utilities=>horizontal_tab.

******

  perform bdc_dynpro      using 'RVPKMASS' '1000'.
  perform bdc_field       using 'BDC_OKCODE'
                                '/EGET'.
  perform bdc_field       using 'BDC_CURSOR'
                                'SO_VBELN-LOW'.
  perform bdc_dynpro      using 'SAPMSSY0' '0120'.
  perform bdc_field       using 'BDC_CURSOR'
                                '04/11'.
  perform bdc_field       using 'BDC_OKCODE'
                                '=PICK'.
  perform bdc_dynpro      using 'RVPKMASS' '1000'.
  perform bdc_field       using 'BDC_CURSOR'
                                'SO_VBELN-LOW'.
  perform bdc_field       using 'BDC_OKCODE'
                                '=ONLI'.
  perform bdc_field       using 'SO_VBELN-LOW' ''.
  perform bdc_field       using 'P_FSPOS'
                                'X'.
  perform bdc_field       using 'SO_AUFNR-LOW'    salesorder.
  perform bdc_field       using 'P_DATETO'        g_date.
  perform bdc_field       using 'P_RRBFLT'        'X'.
  perform bdc_field       using 'P_SBELN'         'X'.
  perform bdc_field       using 'P_DETAIL'        'X'.
  perform bdc_dynpro      using 'RVPKMASS' '1000'.
  perform bdc_field       using 'BDC_CURSOR'
                                'SO_VBELN-LOW'.
  perform bdc_field       using 'BDC_OKCODE'
                                '=ONLI'.
  perform bdc_field       using 'SO_VBELN-LOW'  c_space.
*                                ''.
  perform bdc_field       using 'P_FSPOS'
                                'X'.
  perform bdc_field       using 'SO_AUFNR-LOW'   salesorder.
  perform bdc_field       using 'P_DATETO'       g_date.
  perform bdc_field       using 'P_RRBFLT'
                                'X'.
  perform bdc_field       using 'P_SBELN'
                                'X'.
  perform bdc_field       using 'P_DETAIL'
                                'X'.
  perform bdc_dynpro      using 'SAPMSSY0' '0120'.
  perform bdc_field       using 'BDC_OKCODE'
                                '=EF03'.
  perform bdc_dynpro      using 'RVPKMASS' '1000'.
  perform bdc_field       using 'BDC_OKCODE'
                                '/EE'.
  perform bdc_field       using 'BDC_CURSOR'
                                'SO_VBELN-LOW'.
  perform bdc_transaction using 'DP95'.

***********************************************



















Thursday, 15 February 2024

Change Info record using BAPI_ALM_ORDER_MAINTAIN

 









 Data g_refnum(6TYPE N.

  REFRESH lt_methods,lt_component,lt_component_up..
  CLEAR ls_methods.
  CLEAR ls_component.
  CLEAR ls_component_up.

*Get Reservation N
  LOOP AT IT_DAT4 INTO WA_DAT4.
    Select single rsnum rspos
           into (wa_dat4-rsnum,wa_dat4-rspos)
           from RESB
           Where Aufnr EQ G_AUFNR
             And Matnr EQ wa_dat4-col_10

    Modify it_dat4 from wa_dat4.
  Endloop.

  g_refnum 1.
  LOOP AT IT_DAT4 INTO WA_DAT4 WHERE col_12 EQ SPACE.
*-- Component Mapping
    ls_methods-refnumber =  g_refnum .
    ls_methods-objecttype 'COMPONENT'.
    ls_methods-method     'CHANGE'.
    ls_methods-objectkey  =  G_AUFNR.
    Append ls_methods to lt_methods.

    ls_component-reserv_no =  wa_dat4-rsnum.
    ls_component-res_item  =  wa_dat4-rspos.
    ls_component-material  =  wa_dat4-col_10.
    ls_component-item_number =  wa_dat4-col_21.
    ls_component-activity '0010'.
    ls_component-info_rec ''.
    ls_component-pur_group wa_dat4-col_06.

    ls_component_up-material 'X'.
    ls_component_up-item_number 'X'.
    ls_component_up-info_rec 'X'.
    ls_component_up-pur_group 'X'.
    APPEND ls_component TO lt_component.
    APPEND ls_component_up TO lt_component_up.
    CLEAR ls_methods.

    ls_methods-method 'SAVE'.
    APPEND ls_methods TO lt_methods.
    g_refnum g_refnum + 1.
  ENDLOOP.

  call function 'BAPI_ALM_ORDER_MAINTAIN'
    TABLES
      it_methods      lt_methods
      it_component    lt_component
      it_component_up lt_component_up
      return          lt_return
      et_numbers      lt_numbers.

  Call function 'BAPI_TRANSACTION_COMMIT'
    EXPORTING
      wait 'X'.

Visitor

Code Syntax Try...Endtry

  Code : Report Try. Parameters  NUMBER  type  I . data  RESULT  type  P  decimals  2 . data  OREF  type  ref  to  CX_ROOT . data  TEXT  typ...