Aug 8, 2012

Pricing Conditions Upload with BAPI

Below is sample code to simulate Pricing Conditions upload using BAPI


The structure of condition table A073
MANDT MANDT CLNT 3 0 Client
KAPPL KAPPL CHAR 2 0 Application
KSCHL KSCHA CHAR 4 0 Condition type
VKORG VKORG CHAR 4 0 Sales Organization
VTWEG VTWEG CHAR 2 0 Distribution Channel
MATNR MATNR CHAR 18 0 Article Number
VRKME VRKME UNIT 3 0 Sales unit
DATBI KODATBI DATS 8 0 Validity end date of the condition record
DATAB KODATAB DATS 8 0 Validity start date of the condition record
KNUMH KNUMH CHAR 10 0 Condition record number


*&---------------------------------------------------------------------*
*& Report  ZTES
*&
*&---------------------------------------------------------------------*
*&
*&
*&---------------------------------------------------------------------*

REPORT  ztes.

CONSTANTS: lc_kschl_vkp0 TYPE kscha VALUE 'VKP0'.

DATA: lv_datum       TYPE sydatum,
lv_count       TYPE kopos,
lv_unit        TYPE kpein,
ls_bapicondct  TYPE bapicondct,
ls_bapicondhd  TYPE bapicondhd,
ls_bapicondit  TYPE bapicondit,
lt_bapicondct  TYPE STANDARD TABLE OF bapicondct,
lt_bapicondhd  TYPE STANDARD TABLE OF bapicondhd,
lt_bapicondit  TYPE STANDARD TABLE OF bapicondit,
lt_bapicondqs  TYPE STANDARD TABLE OF bapicondqs,
lt_bapicondvs  TYPE STANDARD TABLE OF bapicondvs,
lt_bapiret2    TYPE STANDARD TABLE OF bapiret2,
ls_bapiret2    TYPE bapiret2,
lt_bapiknumhs  TYPE STANDARD TABLE OF bapiknumhs,
lt_mem_initial TYPE STANDARD TABLE OF cnd_mem_initial.

break dc_idenny.
lv_datum = '20120812'"sy-datum.
lv_count = 1.

*LOOP AT gt_vbap INTO wa_vbap.

* BAPI Structure for Condition Tables
ls_bapicondct-operation  = '009'.
ls_bapicondct-table_no   = '073'.
ls_bapicondct-applicatio = 'V'.
ls_bapicondct-cond_usage = 'A'.
ls_bapicondct-cond_type  = lc_kschl_vkp0.

"Set the varkey from A700 table key fields
DATA lw_a073 LIKE a073.
lw_a073-VKORG = '1001'.
lw_a073-VTWEG = '03'.
lw_a073-MATNR = '000000100000072002'.
lw_a073-VRKME = 'AA1'.
CONCATENATE lw_a073-vkorg lw_a073-vtweg lw_a073-matnr lw_a073-vrkme
       INTO ls_bapicondct-varkey.
*  ls_bapicondct-varkey = space.

ls_bapicondct-valid_to   = '99991231'.
ls_bapicondct-valid_from = lv_datum.
ls_bapicondct-cond_no    = '$000000001'.

**** BAPI Structure of KONH with English Field Names
ls_bapicondhd-operation  = '009'.
ls_bapicondhd-cond_no    = '$000000001'.
ls_bapicondhd-created_by = sy-uname.
ls_bapicondhd-creat_date = sy-datum.
ls_bapicondhd-cond_usage = 'A'.
ls_bapicondhd-table_no   = '073'.
ls_bapicondhd-applicatio = 'V'.
ls_bapicondhd-cond_type  = lc_kschl_vkp0.
ls_bapicondhd-varkey     = ls_bapicondct-varkey.
ls_bapicondhd-valid_to   = '99991231'.
ls_bapicondhd-valid_from = lv_datum.

*** BAPI Structure of KONP with English Field Names
CLEAR ls_bapicondit.
ls_bapicondit-operation  = '009'.
ls_bapicondit-cond_no    = '$000000001'.
ls_bapicondit-cond_count = lv_count.
ls_bapicondit-applicatio = 'V'.
ls_bapicondit-cond_type  = lc_kschl_vkp0.
ls_bapicondit-scaletype  = 'A'.
ls_bapicondit-scalebasin = 'B'.
ADD 1 TO ls_bapicondit-scale_qty.
ls_bapicondit-calctypcon = 'B'.
lv_unit = '1'"wa_vbap-kwmeng.
ls_bapicondit-cond_p_unt = lv_unit.
ls_bapicondit-cond_value = '1.14'"wa_vbap-value.
ls_bapicondit-condcurr   = 'RM'.   "wa_vbap-waerk.

APPEND: ls_bapicondct TO lt_bapicondct,
ls_bapicondhd TO lt_bapicondhd,
ls_bapicondit TO lt_bapicondit.

*ENDLOOP.

*** BAPI for pricing Condition Records
CALL FUNCTION 'BAPI_PRICES_CONDITIONS'
  TABLES
    ti_bapicondct  = lt_bapicondct
    ti_bapicondhd  = lt_bapicondhd
    ti_bapicondit  = lt_bapicondit
    ti_bapicondqs  = lt_bapicondqs
    ti_bapicondvs  = lt_bapicondvs
    to_bapiret2    = lt_bapiret2
    to_bapiknumhs  = lt_bapiknumhs
    to_mem_initial = lt_mem_initial
  EXCEPTIONS
    update_error   = 1
    OTHERS         = 2.

IF sy-subrc EQ 0.

  WRITE: /1 'Return Messages for Condition create'(t03).
  LOOP AT lt_bapiret2 INTO ls_bapiret2.
    WRITE: /1 ls_bapiret2-message.
  ENDLOOP.
  ULINE.

  CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'
    EXPORTING
      wait   = 'X'
    IMPORTING
      return = ls_bapiret2.
ENDIF.