Search code examples
abapbapi

Update SD condition records using FM/BAPI


I am trying to update DATBI field of A* pricing condition tables using the data coming from excel file.I have tried below approaches but unable to update the entry as DATBI is key field. It creates new record instead of updating the existing one. Approaches:

  1. FM: RV_CONDITION_COPY(with mai tain_ mode as B)
  2. FM: RV_CONDITION_MAINTENANCE(using maintain_mode as B)
  3. BAPI_PRICES_CONDITIONS(It changes KONH table but not A* table.Also, as per note#1135984,we shouldn't be using it)
  4. FM:RV_KONDITION_SICHERN_V13A in update mode(This gives sy-subrc 4 in this FM post update A* from table__ syntax as the datbi is new coming from my excel file)
  5. Idoc COND_A04(This also creates new entry instead of updating existing one)

BDC is the only approach that I can think of but looking at VK12 screen, it has been observed that based on key combination and its underlying A* tables, it should be dynamic.

Can you please help in this regard?Is there any dynamic BDC for VK12?


Solution

  • Undoubtedly we shouldn't use BAPI_PRICES_CONDITIONS, and note 94443 describes the problems it can cause when you push incorrect conditions into system, but sometime there is no way to avoid it. I tried this FM and it definitely worked on my system.

    Here is the code for updating A009 table:

    *  header table
    APPEND INITIAL LINE TO lt_head ASSIGNING <fs_head>.
    <fs_head>-operation   = '009'.               
    <fs_head>-cond_usage  = 'A'.                
    <fs_head>-table_no    = '007'.                
    <fs_head>-applicatio  = 'V'. 
    <fs_head>-cond_type   = 'ZP15'.           
    <fs_head>-valid_from  = '20210103'. 
    <fs_head>-valid_to    = '99991231'.  
    <fs_head>-cond_no     = '$000000001'. 
    
    <fs_head>-varkey = 'BE110101001000635199993112'.
    
    **     items
    APPEND INITIAL LINE TO lt_konh ASSIGNING <fs_konh>.
    <fs_konh>-operation  = '009'.                   
    <fs_konh>-cond_no    = '$000000001'. 
    <fs_konh>-cond_usage = 'A'.                    
    <fs_konh>-table_no   = '007'.                   
    <fs_konh>-applicatio = 'V'.                     
    <fs_konh>-cond_type  = 'ZP15'.               
    <fs_konh>-valid_from = '20210103'.        
    <fs_konh>-valid_to   = '99991231'.           
    <fs_konh>-created_by = sy-uname.
    <fs_konh>-creat_date = sy-datum.
    
    APPEND INITIAL LINE TO lt_konp ASSIGNING <fs_konp>.
    <fs_konp>-operation  = '009'.                 
    <fs_konp>-cond_no    = '$000000001'.
    <fs_konp>-cond_count = '01'.                 
    <fs_konp>-applicatio = 'V'.                   
    <fs_konp>-cond_type  = 'ZP15'.             
    <fs_konp>-scaletype  = 'A'.                   
    <fs_konp>-scalebasin = space.                
    <fs_konp>-scale_qty  = '0'.                   
    <fs_konp>-cond_p_unt = '1'.                   
    <fs_konp>-cond_unit  = 'Т'.                  
    <fs_konp>-calctypcon = 'C'.                  
    <fs_konp>-cond_value = '160'.              
    <fs_konp>-condcurr   = 'EUR'.             
    
    CALL FUNCTION 'BAPI_PRICES_CONDITIONS'
      EXPORTING
        pi_initialmode = abap_true
      TABLES
        ti_bapicondct  = lt_head
        ti_bapicondhd  = lt_konh
        ti_bapicondit  = lt_konp
        ti_bapicondqs  = lt_konm
        ti_bapicondvs  = lt_konw
        to_bapiret2    = lt_return
        to_bapiknumhs  = lt_knumh
        to_mem_initial = lt_buffer
      EXCEPTIONS
        update_error   = 1
        OTHERS         = 2.
    
    CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'
     EXPORTING
       WAIT          = abap_true.
    

    I tried the above code and record with the specified key was properly updated with new valid to field (DATAB). Updating DATBI is a bit more complicated since it is included in the primary key and cannot be updated in one step. You must delete the record with the old DATBI and the create the new one. In this blog you can read more about how to use this BAPI:

    https://blogs.sap.com/2019/07/22/how-to-use-bapi_prices_conditions-to-mass-upload-price-conditions/