You are on page 1of 11

Creating SAP Pricing conditions

using BAPI_PRICES_CONDITIONS

Category: SD
Published on Thursday, 17 September 2009 18:16
Written by Kevin Wilson
Hits: 11194
This code snippet inserts a condition record for Pricing condition type Z123 with key sales order and line
number.
CONSTANTS: lc_kschl_z123 TYPE kscha VALUE 'Z123'.
DATA: lv_datum
lv_count
lv_unit

TYPE sydatum,

TYPE kopos,
TYPE kpein,

is_bapicondct TYPE bapicondct,


is_bapicondhd TYPE bapicondhd,
is_bapicondit TYPE bapicondit,
it_bapicondct TYPE STANDARD TABLE OF bapicondct,
it_bapicondhd TYPE STANDARD TABLE OF bapicondhd,
it_bapicondit TYPE STANDARD TABLE OF bapicondit,
it_bapicondqs TYPE STANDARD TABLE OF bapicondqs,
it_bapicondvs TYPE STANDARD TABLE OF bapicondvs,
it_bapiret2

TYPE STANDARD TABLE OF bapiret2,

is_bapiret2

TYPE bapiret2,

it_bapiknumhs TYPE STANDARD TABLE OF bapiknumhs,


it_mem_initial TYPE STANDARD TABLE OF cnd_mem_initial.
lv_datum = sy-datum.
lv_count = 1.
LOOP AT gt_vbap INTO wa_vbap.

* BAPI Structure for Condition Tables


is_bapicondct-operation = '009'.
is_bapicondct-table_no = '700'.
is_bapicondct-applicatio = 'V'.
is_bapicondct-cond_usage = 'A'.
is_bapicondct-cond_type = lc_kschl_z123.
"Set the varkey from A700 table key fields
CONCATENATE wa_vbap-vbeln wa_vbap-posnr
INTO is_bapicondct-varkey.
is_bapicondct-valid_to = '12/31/2009'.
is_bapicondct-valid_from = lv_datum.
is_bapicondct-cond_no

= '$000000001'.

**** BAPI Structure of KONH with English Field Names


is_bapicondhd-operation = '009'.
is_bapicondhd-cond_no

= '$000000001'.

is_bapicondhd-created_by = sy-uname.
is_bapicondhd-creat_date = sy-datum.
is_bapicondhd-cond_usage = 'A'.
is_bapicondhd-table_no = '700'.
is_bapicondhd-applicatio = 'V'.
is_bapicondhd-cond_type = lc_kschl_z123.
is_bapicondhd-varkey

= is_bapicondct-varkey.

is_bapicondhd-valid_to = '12/31/2009'.
is_bapicondhd-valid_from = lv_datum.
*** BAPI Structure of KONP with English Field Names
CLEAR is_bapicondit.
is_bapicondit-operation = '009'.
is_bapicondit-cond_no

= '$000000001'.

is_bapicondit-cond_count = lv_count.
is_bapicondit-applicatio = 'V'.
is_bapicondit-cond_type = lc_kschl_z123.
is_bapicondit-scaletype = 'A'.
is_bapicondit-scalebasin = 'B'.
ADD 1 TO is_bapicondit-scale_qty.

is_bapicondit-calctypcon = 'B'.
lv_unit = wa_vbap-kwmeng.
is_bapicondit-cond_p_unt = lv_unit.
is_bapicondit-cond_value = wa_vbap-value.
is_bapicondit-condcurr = wa_vbap-waerk.
APPEND: is_bapicondct TO it_bapicondct,
is_bapicondhd TO it_bapicondhd,
is_bapicondit TO it_bapicondit.
ENDLOOP.
*** BAPI for pricing Condition Records
CALL FUNCTION 'BAPI_PRICES_CONDITIONS'
TABLES
ti_bapicondct = it_bapicondct
ti_bapicondhd = it_bapicondhd
ti_bapicondit = it_bapicondit
ti_bapicondqs = it_bapicondqs
ti_bapicondvs = it_bapicondvs
to_bapiret2

= it_bapiret2

to_bapiknumhs = it_bapiknumhs
to_mem_initial = it_mem_initial
EXCEPTIONS
update_error = 1
OTHERS

= 2.

IF sy-subrc EQ 0.
WRITE: /1 'Return Messages for Condition create'(t03).
LOOP AT it_bapiret2 INTO is_bapiret2.
WRITE: /1 is_bapiret2-message.
ENDLOOP.
ULINE.
CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'
EXPORTING
wait = 'X'

IMPORTING
return = is_bapiret2.
ENDIF.
Correct AnswerRe: Bapi to create pricing condition\VK11

Niranjan Krishnamurthi Jul 9, 2012 9:02 AM (in response to Gil Hadida)


Hi,
Please try this.
DATA: BEGIN OF ls_komk OCCURS 10.
INCLUDE STRUCTURE komk.
DATA: END OF ls_komk.
DATA:ls_komg TYPE komg.
DATA: BEGIN OF ls_komv OCCURS 10.
INCLUDE STRUCTURE komv.
DATA: END OF ls_komv.
DATA: BEGIN OF lt_komv OCCURS 0.
INCLUDE STRUCTURE komv.
DATA: END OF lt_komv.
DATA:
komk TYPE komk ,
komp TYPE komp .
ls_komg-matnr = <material>.
ls_komg-vkorg = <sales org>.
ls_komg-vtweg = <dist chn>.
CLEAR: lt_komv,ls_komv.
ls_komv-kappl = 'V'.
" Application V = Sales
ls_komv-kschl = <>. " Condition type
ls_komv-knumh = <condition record number>.
ls_komv-krech = <>.
" calculation type;
ls_komv-kbetr = <>.
ls_komv-kmein = <>.
ls_komv-kpein = <>.
ls_komv-waers = <>.
ls_komv-updkz = 'I'.
APPEND ls_komv TO lt_komv.
CLEAR lt_komv.
DATA wa_pispr TYPE pispr.
wa_pispr-matnr = <>.
wa_pispr-vkorg = <>.
wa_pispr-vtweg = <>.
CALL FUNCTION 'SPR_KOMK_KOMP_FILL'
EXPORTING
pi_i_spr = wa_pispr
IMPORTING
pe_i_komk = komk
pe_i_komp = komp.
CALL FUNCTION 'RV_CONDITION_COPY'
EXPORTING
application
= 'V'

condition_table
=' '
" 3 character cond. table
condition_type
='
' " cond. type
date_from
= '20120101' " valid on
date_to
= '99991231' " valid to
enqueue
= 'X'
" lock entry
i_komk
= komk
i_komp
= komp
key_fields
= ls_komg " key fields
maintain_mode
= 'B'
" A= create " B= change, " C= display " D= create
no_authority_check
= 'X'
selection_date
= '20120101' " valid on
keep_old_records
=''
overlap_confirmed
= 'X'
no_db_update
= space
IMPORTING
e_komk
= komk
e_komp
= komp
new_record
= lv_new_record
TABLES
copy_records
= lt_komv
EXCEPTIONS
enqueue_on_record
=1
invalid_application
=2
invalid_condition_number = 3
invalid_condition_type = 4
no_selection
=5
table_not_valid
=6
no_authority_ekorg
=7
no_authority_kschl
= 8.
IF sy-subrc = 0.
CALL FUNCTION 'RV_CONDITION_SAVE'.
COMMIT WORK.
CALL FUNCTION 'RV_CONDITION_RESET'.
"necessary to write data
COMMIT WORK.
WRITE:/ 'Success'.
ELSE.
WRITE: / 'Error'.
ENDIF.
Cheers
~Niranjan

Re: Sample code for BAPI_PRICES_CONDITIONS

Ferry Lianto Apr 13, 2006 9:01 PM (in response to Martn Becker)
Hi Martin,

Please check this sample code from other thread.


wtable1-table_no = '306'.
wtable1-applicatio = 'V'.
wtable1-cond_type = 'ZPR0'.

wtable1-operation = '009'.
wtable1-varkey = '13001001USD 000000000050068946'.
wtable1-valid_to = '99991231'.
wtable1-valid_from = '20051101'.
wtable1-cond_no = 'wtable1-table_no = '306'.
wtable1-applicatio = 'V'.
wtable1-cond_type = 'ZPR0'.
wtable1-operation = '009'.
wtable1-varkey = '13001001USD 000000000050068946'.
wtable1-valid_to = '99991231'.
wtable1-valid_from = '20051101'.
wtable1-cond_no = '$000000001'.
APPEND wtable1 TO table1.
wtable2-operation = '009'.
wtable2-cond_no = '$000000001'.
wtable2-created_by = sy-uname.
wtable2-creat_date = '20051022'.
wtable2-cond_usage = 'A'.
wtable2-table_no = '110'.
wtable2-applicatio = 'V'.
wtable2-cond_type = 'ZPR0'.
wtable2-varkey = '13001001USD 000000000050068946'.
wtable2-valid_from = '20051101'.
wtable2-valid_to = '99991231'.
APPEND wtable2 TO table2.

wtable3-operation = '009'.
wtable3-cond_no = '$000000001'.
wtable3-cond_count = '01'.
wtable3-applicatio = 'V'.
wtable3-cond_type = 'ZPR0'.
wtable3-scaletype = 'A'.
wtable3-scalebasin = 'C'.
wtable3-scale_qty = '1'.
wtable3-cond_p_unt = '1'.
wtable3-cond_unit = 'EA'.
wtable3-calctypcon = 'C'.
wtable3-cond_value = '454'.
wtable3-condcurr = 'USD'.
APPEND wtable3 TO table3.
CALL FUNCTION 'BAPI_PRICES_CONDITIONS'
* EXPORTING
* PI_initialmode = 'X'
* PI_BLOCKNUMBER =
TABLES
ti_bapicondct = table1
ti_bapicondhd = table2
ti_bapicondit = table3
ti_bapicondqs = table4
ti_bapicondvs = table5
to_bapiret2 = table6
to_bapiknumhs = table7
to_mem_initial = table8

EXCEPTIONS
update_error = 1
OTHERS = 2
.
IF sy-subrc <> 0.
MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
ENDIF.
READ TABLE table6 WITH KEY type = 'E' TRANSPORTING NO FIELDS.
IF sy-subrc = 0.
loop at table6 into ret.
write: / ret-type, ret-message, ret-id, RET-LOG_NO, RET-LOG_MSG_NO,
RET-MESSAGE_V1, RET-MESSAGE_V2, RET-MESSAGE_V3, RET-MESSAGE_V4,
RET-PARAMETER,RET-ROW,RET-FIELD.
endloop.
ELSE.
CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'
*EXPORTING
* WAIT =
IMPORTING
return = ret
.
ENDIF.'.
APPEND wtable1 TO table1.
wtable2-operation = '009'.
wtable2-cond_no = 'wtable1-table_no = '306'.
wtable1-applicatio = 'V'.
wtable1-cond_type = 'ZPR0'.
wtable1-operation = '009'.
wtable1-varkey = '13001001USD 000000000050068946'.
wtable1-valid_to = '99991231'.
wtable1-valid_from = '20051101'.
wtable1-cond_no = '$000000001'.
APPEND wtable1 TO table1.
wtable2-operation = '009'.
wtable2-cond_no = '$000000001'.
wtable2-created_by = sy-uname.
wtable2-creat_date = '20051022'.
wtable2-cond_usage = 'A'.
wtable2-table_no = '110'.
wtable2-applicatio = 'V'.
wtable2-cond_type = 'ZPR0'.
wtable2-varkey = '13001001USD 000000000050068946'.
wtable2-valid_from = '20051101'.
wtable2-valid_to = '99991231'.
APPEND wtable2 TO table2.

wtable3-operation = '009'.
wtable3-cond_no = '$000000001'.
wtable3-cond_count = '01'.
wtable3-applicatio = 'V'.
wtable3-cond_type = 'ZPR0'.
wtable3-scaletype = 'A'.
wtable3-scalebasin = 'C'.
wtable3-scale_qty = '1'.
wtable3-cond_p_unt = '1'.
wtable3-cond_unit = 'EA'.
wtable3-calctypcon = 'C'.
wtable3-cond_value = '454'.
wtable3-condcurr = 'USD'.
APPEND wtable3 TO table3.
CALL FUNCTION 'BAPI_PRICES_CONDITIONS'
* EXPORTING
* PI_initialmode = 'X'
* PI_BLOCKNUMBER =
TABLES
ti_bapicondct = table1
ti_bapicondhd = table2
ti_bapicondit = table3
ti_bapicondqs = table4
ti_bapicondvs = table5
to_bapiret2 = table6
to_bapiknumhs = table7
to_mem_initial = table8
EXCEPTIONS
update_error = 1
OTHERS = 2
.
IF sy-subrc <> 0.
MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
ENDIF.
READ TABLE table6 WITH KEY type = 'E' TRANSPORTING NO FIELDS.
IF sy-subrc = 0.
loop at table6 into ret.
write: / ret-type, ret-message, ret-id, RET-LOG_NO, RET-LOG_MSG_NO,
RET-MESSAGE_V1, RET-MESSAGE_V2, RET-MESSAGE_V3, RET-MESSAGE_V4,
RET-PARAMETER,RET-ROW,RET-FIELD.
endloop.
ELSE.
CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'
*EXPORTING
* WAIT =
IMPORTING
return = ret
.
ENDIF.'.
wtable2-created_by = sy-uname.
wtable2-creat_date = '20051022'.

wtable2-cond_usage = 'A'.
wtable2-table_no = '110'.
wtable2-applicatio = 'V'.
wtable2-cond_type = 'ZPR0'.
wtable2-varkey = '13001001USD 000000000050068946'.
wtable2-valid_from = '20051101'.
wtable2-valid_to = '99991231'.
APPEND wtable2 TO table2.

wtable3-operation = '009'.
wtable3-cond_no = 'wtable1-table_no = '306'.
wtable1-applicatio = 'V'.
wtable1-cond_type = 'ZPR0'.
wtable1-operation = '009'.
wtable1-varkey = '13001001USD 000000000050068946'.
wtable1-valid_to = '99991231'.
wtable1-valid_from = '20051101'.
wtable1-cond_no = '$000000001'.
APPEND wtable1 TO table1.
wtable2-operation = '009'.
wtable2-cond_no = '$000000001'.
wtable2-created_by = sy-uname.
wtable2-creat_date = '20051022'.
wtable2-cond_usage = 'A'.
wtable2-table_no = '110'.
wtable2-applicatio = 'V'.
wtable2-cond_type = 'ZPR0'.
wtable2-varkey = '13001001USD 000000000050068946'.
wtable2-valid_from = '20051101'.
wtable2-valid_to = '99991231'.
APPEND wtable2 TO table2.

wtable3-operation = '009'.
wtable3-cond_no = '$000000001'.
wtable3-cond_count = '01'.
wtable3-applicatio = 'V'.
wtable3-cond_type = 'ZPR0'.
wtable3-scaletype = 'A'.
wtable3-scalebasin = 'C'.
wtable3-scale_qty = '1'.
wtable3-cond_p_unt = '1'.
wtable3-cond_unit = 'EA'.
wtable3-calctypcon = 'C'.
wtable3-cond_value = '454'.
wtable3-condcurr = 'USD'.
APPEND wtable3 TO table3.
CALL FUNCTION 'BAPI_PRICES_CONDITIONS'
* EXPORTING
* PI_initialmode = 'X'

* PI_BLOCKNUMBER =
TABLES
ti_bapicondct = table1
ti_bapicondhd = table2
ti_bapicondit = table3
ti_bapicondqs = table4
ti_bapicondvs = table5
to_bapiret2 = table6
to_bapiknumhs = table7
to_mem_initial = table8
EXCEPTIONS
update_error = 1
OTHERS = 2
.
IF sy-subrc <> 0.
MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
ENDIF.
READ TABLE table6 WITH KEY type = 'E' TRANSPORTING NO FIELDS.
IF sy-subrc = 0.
loop at table6 into ret.
write: / ret-type, ret-message, ret-id, RET-LOG_NO, RET-LOG_MSG_NO,
RET-MESSAGE_V1, RET-MESSAGE_V2, RET-MESSAGE_V3, RET-MESSAGE_V4,
RET-PARAMETER,RET-ROW,RET-FIELD.
endloop.
ELSE.
CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'
*EXPORTING
* WAIT =
IMPORTING
return = ret
.
ENDIF.'.
wtable3-cond_count = '01'.
wtable3-applicatio = 'V'.
wtable3-cond_type = 'ZPR0'.
wtable3-scaletype = 'A'.
wtable3-scalebasin = 'C'.
wtable3-scale_qty = '1'.
wtable3-cond_p_unt = '1'.
wtable3-cond_unit = 'EA'.
wtable3-calctypcon = 'C'.
wtable3-cond_value = '454'.
wtable3-condcurr = 'USD'.
APPEND wtable3 TO table3.
CALL FUNCTION 'BAPI_PRICES_CONDITIONS'
* EXPORTING
* PI_initialmode = 'X'
* PI_BLOCKNUMBER =
TABLES
ti_bapicondct = table1
ti_bapicondhd = table2
ti_bapicondit = table3

ti_bapicondqs = table4
ti_bapicondvs = table5
to_bapiret2 = table6
to_bapiknumhs = table7
to_mem_initial = table8
EXCEPTIONS
update_error = 1
OTHERS = 2
.
IF sy-subrc <> 0.
MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
ENDIF.
READ TABLE table6 WITH KEY type = 'E' TRANSPORTING NO FIELDS.
IF sy-subrc = 0.
loop at table6 into ret.
write: / ret-type, ret-message, ret-id, RET-LOG_NO, RET-LOG_MSG_NO,
RET-MESSAGE_V1, RET-MESSAGE_V2, RET-MESSAGE_V3, RET-MESSAGE_V4,
RET-PARAMETER,RET-ROW,RET-FIELD.
endloop.
ELSE.
CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'
*EXPORTING
* WAIT =
IMPORTING
return = ret
.
ENDIF.
Also there are other options:
- from LSMW the report RV14BTCI
- batch input to transaction XK15
- using FM modules RV_CONDITION* (e.g. _copy and _save)

You might also like