Skip to Content

Archived discussions are read-only. Learn more about SAP Q&A

BAPI contract change is not working to update the scale price

CALL FUNCTION 'BAPI_CONTRACT_CHANGE'

     EXPORTING

       purchasingdocument   = p_ebeln

     TABLES

       item                              = lt_item_upd

       itemx                            = lt_itemx_upd

       item_cond_validity       = lt_item_cond_validity_upd

       item_cond_validityx     = lt_item_cond_validityx_upd

       item_condition             = lt_item_condition_upd

       item_conditionx           = lt_item_condition_updx

       item_cond_scale_quan  = lt_item_cond_scale_quan_upd

       return                             = lt_bapi_return.


we passing the quantity update scale , which is not update in MM contract.

Tags:
Former Member replied

REPORT  ZXXXXXXXXX.

DATA lt_item_cond_validity        TYPE TABLE OF bapimeoutvalidity.

DATA ls_item_cond_validity        TYPE          bapimeoutvalidity.

DATA lt_item_condition            TYPE TABLE OF bapimeoutcondition.

DATA ls_item_condition            TYPE          bapimeoutcondition.

DATA lt_item_conditionx           TYPE TABLE OF bapimeoutconditionx.

DATA ls_item_conditionx           TYPE          bapimeoutconditionx.

DATA lt_item                      TYPE TABLE OF bapimeoutitem.

DATA ls_item                      TYPE          bapimeoutitem.

DATA lt_item_cond_scale_value     TYPE TABLE OF bapimeoutitemscaleval.

DATA ls_item_cond_scale_value     TYPE          bapimeoutitemscaleval.

DATA lt_item_cond_scale_quan      TYPE TABLE OF bapimeoutitemscalequan.

DATA ls_item_cond_scale_quan      TYPE          bapimeoutitemscalequan.

*

*DATA ITEM_TEXT

*DATA HEADER_TEXT

DATA lt_head_cond_validity        TYPE TABLE OF bapimeoutheadvalidity.

DATA ls_head_cond_validity        TYPE          bapimeoutheadvalidity.

DATA lt_head_condition            TYPE TABLE OF bapimeoutheadercond.

DATA ls_head_condition            TYPE          bapimeoutheadercond.

DATA lt_head_cond_scale_val       TYPE TABLE OF bapimeoutscalevalue.

DATA ls_head_cond_scale_val       TYPE          bapimeoutscalevalue.

DATA lt_head_cond_scale_quan      TYPE TABLE OF bapimeoutscale.

DATA ls_head_cond_scale_quan      TYPE          bapimeoutscale.

DATA ls_header                    TYPE          bapimeoutheader.

DATA lt_bapi_return               TYPE   bapiret2_t.

DATA ls_return                    TYPE   bapiret2.

DATA lt_item_upd                  TYPE TABLE OF bapimeoutitem.

DATA ls_item_upd                  TYPE          bapimeoutitem.

DATA ls_itemx_upd                 TYPE          bapimeoutitemx.

DATA lt_itemx_upd                 TYPE TABLE OF bapimeoutitemx.

DATA lt_item_cond_validityx_upd   TYPE TABLE OF bapimeoutvalidityx.

DATA ls_item_cond_validityx_upd   TYPE          bapimeoutvalidityx.

DATA lt_item_cond_validity_upd    TYPE TABLE OF bapimeoutvalidity.

DATA ls_item_cond_validity_upd    TYPE          bapimeoutvalidity.

DATA lt_item_condition_upd        TYPE TABLE OF bapimeoutcondition.

DATA ls_item_condition_upd        TYPE          bapimeoutcondition.

DATA lt_item_condition_updx       TYPE TABLE OF bapimeoutconditionx.

DATA ls_item_condition_updx       TYPE          bapimeoutconditionx.

DATA lt_item_cond_scale_quan_upd  TYPE TABLE OF bapimeoutitemscalequan.

DATA ls_item_cond_scale_quan_upd  TYPE          bapimeoutitemscalequan.

PARAMETERS p_ebeln TYPE ebeln.

PARAMETERS p_ebelp TYPE ebelp.

CONSTANTS cv_currency_unit TYPE waers VALUE 'EUR'.

*----------------------------------------------------------------------*

*       CLASS lcl_bapi_set_falg DEFINITION

*----------------------------------------------------------------------*

*

*----------------------------------------------------------------------*

CLASS lcl_bapi_set_falg DEFINITION.

   PUBLIC SECTION.

     CLASS-METHODS set_update_flag

       IMPORTING

          is_data TYPE any

       EXPORTING

          is_data_update TYPE any .

ENDCLASS.                    "lcl_bapi_set_falg DEFINITION

*----------------------------------------------------------------------*

*       CLASS lcl_bapi_contract_change DEFINITION

*----------------------------------------------------------------------*

*

*----------------------------------------------------------------------*

CLASS lcl_bapi_contract_change DEFINITION.

   PUBLIC SECTION.

     DATA lv_condition_number TYPE kopos.

     METHODS constructor.

     METHODS set_item_data

        IMPORTING

          iv_matnr TYPE matnr

          iv_index TYPE i

        EXPORTING

           es_item TYPE bapimeoutitem.

     METHODS set_condition

       IMPORTING

         is_item TYPE bapimeoutitem

       EXPORTING

         es_condition TYPE bapimeoutcondition.

     METHODS set_condition_validity

       IMPORTING

         is_condition TYPE bapimeoutcondition

       EXPORTING

         es_condition_validity TYPE bapimeoutvalidity.

     METHODS set_scale_quantity

        IMPORTING

          is_condition    TYPE bapimeoutcondition

          iv_line_no      TYPE klfn1

          iv_scale_qty    TYPE kstbm

          iv_cond_value   TYPE bapicurext

        EXPORTING

          es_scale_quantity TYPE bapimeoutitemscalequan.

ENDCLASS.                    "lcl_bapi_contract_change DEFINITION

*----------------------------------------------------------------------*

*       CLASS lcl_bapi_set_falg DEFINITION

*----------------------------------------------------------------------*

*

*----------------------------------------------------------------------*

CLASS lcl_bapi_set_falg IMPLEMENTATION.

   METHOD set_update_flag.

     DATA lo_item_stru      TYPE REF TO  cl_abap_structdescr.

     DATA lt_components_imp TYPE         cl_abap_structdescr=>component_table.

     DATA ls_components_imp TYPE LINE OF cl_abap_structdescr=>component_table.

     DATA lt_components_exp TYPE         cl_abap_structdescr=>component_table.

     DATA ls_components_exp TYPE LINE OF cl_abap_structdescr=>component_table.

     FIELD-SYMBOLS <fv_value> TYPE any.

     FIELD-SYMBOLS <fv_value_upd> TYPE any.

     CLEAR is_data_update.

     lo_item_stru ?= cl_abap_typedescr=>describe_by_data( p_data = is_data ).

     lt_components_imp = lo_item_stru->get_components( ).

     lo_item_stru ?= cl_abap_typedescr=>describe_by_data( p_data = is_data_update ).

     lt_components_exp = lo_item_stru->get_components( ).

*    DELETE lt_components_imp WHERE name EQ 'DELETE_IND'.

*    DELETE lt_components_imp WHERE name EQ 'ITEM_NO'.

*    DELETE lt_components_imp WHERE name EQ 'DELETION_IND'.

*    DELETE lt_components_imp WHERE name EQ 'ITEM_NOX'.

     LOOP AT lt_components_imp INTO ls_components_imp.

       ASSIGN COMPONENT ls_components_imp-name OF STRUCTURE is_data TO <fv_value>.

       CHECK <fv_value> IS ASSIGNED AND <fv_value> IS NOT INITIAL.

       READ TABLE lt_components_exp INTO ls_components_exp WITH KEY name = ls_components_imp-name.

       CHECK sy-subrc EQ 0.

       ASSIGN COMPONENT ls_components_exp-name OF STRUCTURE is_data_update TO <fv_value_upd>.

       " check the component type is equal to BAPIUPDATE then update the value as true.

       IF ls_components_exp-type->absolute_name EQ '\TYPE=BAPIUPDATE'.

         <fv_value_upd> = abap_true.

       ELSE.

         <fv_value_upd> = <fv_value>.

       ENDIF.

     ENDLOOP.

   ENDMETHOD.                    "set_update_flag

ENDCLASS.                    "lcl_bapi_set_falg DEFINITION

*----------------------------------------------------------------------*

*       CLASS lcl_bapi_contract_change IMPLEMENTATION

*----------------------------------------------------------------------*

*

*----------------------------------------------------------------------*

CLASS lcl_bapi_contract_change IMPLEMENTATION.

   METHOD set_item_data.

     CLEAR ls_material_number.

     CALL FUNCTION 'CONVERSION_EXIT_ALPHA_INPUT'

       EXPORTING

         input  = iv_matnr

       IMPORTING

         output = ls_material_number.

*    ls_material_number = iv_matnr.

     APPEND ls_material_number TO lt_material_number.

     " build the material master table with material master data " lt_material_master "

     CLEAR es_item.

     " get the material base unit of measure.

     READ TABLE lt_material_master INTO ls_material_master WITH KEY matnr COMPONENTS matnr = iv_matnr.

     es_item-item_no    = p_ebelp + iv_index * 10.

     es_item-acctasscat = 'U'.

     es_item-material   = ls_material_number.

     es_item-short_text = ls_material_master-maktx.  " pass material description

     es_item-matl_group = ls_material_master-mat_group.

     es_item-net_price  = '49.525'.

     es_item-price_unit = 1.

     es_item-prnt_price  = abap_true.

*    ls_item_upd-n = 'EUR'.

     IF ls_material_master-meins IS INITIAL.

       es_item-po_unit = 'LE'.

     ELSE.

       es_item-po_unit = ls_material_master-meins.

     ENDIF.

     es_item-price_date = sy-datum.

*    " after the error of scale update

*    es_item-relord_qty = 1.

*    es_item-conv_den1 = 1.

*    es_item-conv_num1 = 1.

*    es_item-period_ind_expiration_date = 'D'.

   ENDMETHOD.                    "SET_ITEM_DATA

   METHOD set_condition.

     " member variable holds the ne value

*    CLEAR lv_condition_number.

     lv_condition_number = lv_condition_number + 1.

     es_condition-item_no = is_item-item_no.

     es_condition-serial_id = lv_condition_number.

     es_condition-cond_count = 01" lv_condition_number.

     es_condition-cond_type = 'PB00'.

*    es_condition-scale_type = 'A'.

     es_condition-scale_type = 'B'.

     es_condition-scale_base_ty = 'C'.

     es_condition-scale_unit = 'LE'.

     es_condition-calctypcon = 'C'.

     es_condition-cond_value = is_item-net_price.

*    es_condition-scale_curr = cv_currency_unit. " not update in get values

     es_condition-currency   = cv_currency_unit.

     es_condition-cond_p_unt = 1.

     es_condition-cond_unit   = is_item-po_unit.

     es_condition-base_uom   = is_item-po_unit.

     es_condition-numerator = 1.

     es_condition-denominator = 1.

   ENDMETHOD.                    "SET_CONDITION

   METHOD set_condition_validity.

     es_condition_validity-valid_from = sy-datum.

     es_condition_validity-valid_to = '99991230'.

     es_condition_validity-item_no = is_condition-item_no.

     es_condition_validity-serial_id = is_condition-serial_id.

   ENDMETHOD.                    "SET_CONDITION_VALIDITY

   METHOD constructor.

     CLEAR lv_condition_number.

   ENDMETHOD.                    "set_condition_validity

   METHOD set_scale_quantity.

     es_scale_quantity-item_no = is_condition-item_no.

     es_scale_quantity-serial_no = is_condition-serial_id.

     es_scale_quantity-cond_count = is_condition-cond_count.

*    es_scale_quantity-line_no = iv_line_no.

     es_scale_quantity-scale_base_qty = iv_scale_qty.

     es_scale_quantity-cond_value = iv_cond_value.

   ENDMETHOD.                    "SET_SCALE_QUANTITY

ENDCLASS.                    "lcl_bapi_contract_change IMPLEMENTATION

DATA lo_contract_upd TYPE REF TO lcl_bapi_contract_change.

DATA lv_index        TYPE syindex.

DATA lv_lineno       TYPE klfn1.

DATA lv_scale_qty    TYPE kstbm.

DATA lv_cond_value   TYPE bapicurext.

START-OF-SELECTION.

   CLEAR lt_bapi_return.

   CLEAR lt_item_upd.

   CLEAR lt_itemx_upd.

   CLEAR lt_item_condition_upd.

   CLEAR lt_item_condition_updx.

   CREATE OBJECT lo_contract_upd.

   CLEAR lv_index.

   DO 2 TIMES.

     DATA lv_scale_type LIKE ls_item_condition_upd-scale_type.

     CASE sy-index.

       WHEN 1.

         lv_scale_type = 'A'.

       WHEN 2.

         lv_scale_type = 'B'.

     ENDCASE.

     CLEAR ls_item_upd.

     lv_index = lv_index + 1.

     lo_contract_upd->set_item_data(

       EXPORTING

         iv_matnr = '967678'

         iv_index = lv_index

       IMPORTING

         es_item  = ls_item_upd

     ).

     CLEAR ls_itemx_upd.

     lcl_bapi_set_falg=>set_update_flag(

      EXPORTING

        is_data        = ls_item_upd

      IMPORTING

        is_data_update = ls_itemx_upd

    ).

     APPEND ls_item_upd TO lt_item_upd.

     APPEND ls_itemx_upd TO lt_itemx_upd.

     CLEAR ls_item_condition_upd.

     lo_contract_upd->set_condition(

       EXPORTING

         is_item      = ls_item_upd

       IMPORTING

         es_condition = ls_item_condition_upd

     ).

     CLEAR ls_item_condition_updx.

     lcl_bapi_set_falg=>set_update_flag(

       EXPORTING

         is_data        = ls_item_condition_upd

       IMPORTING

         is_data_update = ls_item_condition_updx

     ).

     ls_item_condition_upd-scale_type = lv_scale_type.

     APPEND ls_item_condition_upd TO lt_item_condition_upd.

     APPEND ls_item_condition_updx TO lt_item_condition_updx.

     CLEAR ls_item_cond_validity_upd.

     lo_contract_upd->set_condition_validity(

       EXPORTING

         is_condition          = ls_item_condition_upd

       IMPORTING

         es_condition_validity = ls_item_cond_validity_upd

     ).

     CLEAR ls_item_cond_validityx_upd.

     lcl_bapi_set_falg=>set_update_flag(

       EXPORTING

         is_data        = ls_item_cond_validity_upd

       IMPORTING

         is_data_update = ls_item_cond_validityx_upd

     ).

     APPEND ls_item_cond_validity_upd TO lt_item_cond_validity_upd.

     APPEND ls_item_cond_validityx_upd TO lt_item_cond_validityx_upd.

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

*** scale quantity update

     " if I execut the progrom with out these line the To scale is also

     " updated in the system

     CLEAR lv_lineno.

     CLEAR lv_scale_qty.

     CLEAR lv_cond_value.

     lv_cond_value = ls_item_condition_upd-cond_value.

     CLEAR ls_item_cond_scale_quan_upd.

     DATA lt_item_cond_scale_per_item LIKE lt_item_cond_scale_quan_upd.

     CLEAR lt_item_cond_scale_per_item.

     DO 10 TIMES.

       lv_lineno = lv_lineno + 1.

       lv_cond_value = lv_cond_value - 1.

       lv_scale_qty = 10 *  lv_lineno.

       lo_contract_upd->set_scale_quantity(

         EXPORTING

           is_condition      = ls_item_condition_upd

           iv_line_no        = lv_lineno

           iv_scale_qty      = lv_scale_qty

           iv_cond_value     = lv_cond_value

       IMPORTING

         es_scale_quantity = ls_item_cond_scale_quan_upd

       ).

       APPEND ls_item_cond_scale_quan_upd TO lt_item_cond_scale_per_item.

     ENDDO.

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

" here is the major area, the sorting needs to done base on the scale

" if the scale type is To we need to deceding order and the scale type

" is A we need the acceding order.

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

     CASE ls_item_condition_upd-scale_type.

       WHEN 'B'.

         SORT lt_item_cond_scale_per_item BY item_no scale_base_qty DESCENDING.

       WHEN 'A'.

         SORT lt_item_cond_scale_per_item BY item_no scale_base_qty ASCENDING.

       WHEN OTHERS.

         ASSERT 1 = 0.

     ENDCASE.

     FIELD-SYMBOLS <ls_item_cond_scale_quan> LIKE LINE OF lt_item_cond_scale_quan_upd.

     CLEAR lv_lineno.

     LOOP AT lt_item_cond_scale_per_item ASSIGNING <ls_item_cond_scale_quan>.

       ADD 1 TO lv_lineno.

       <ls_item_cond_scale_quan>-line_no = lv_lineno.

     ENDLOOP.

     APPEND LINES OF lt_item_cond_scale_per_item TO lt_item_cond_scale_quan_upd.

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

   ENDDO.

*  IF lt_item_condition_upd IS INITIAL.

*    WRITE:/ 'No record passed for update'.

*    RETURN.

*  ENDIF.

   BREAK-POINT.

   CALL FUNCTION 'BAPI_CONTRACT_CHANGE'

     EXPORTING

       purchasingdocument   = p_ebeln

     TABLES

       item                 = lt_item_upd

       itemx                = lt_itemx_upd

       item_cond_validity   = lt_item_cond_validity_upd

       item_cond_validityx  = lt_item_cond_validityx_upd

       item_condition       = lt_item_condition_upd

       item_conditionx      = lt_item_condition_updx

       item_cond_scale_quan = lt_item_cond_scale_quan_upd

       return               = lt_bapi_return.

   READ TABLE lt_bapi_return TRANSPORTING NO FIELDS WITH KEY type = 'E'.

   IF sy-subrc NE 0.

     CALL FUNCTION 'BAPI_TRANSACTION_COMMIT'

       EXPORTING

         wait   = abap_true

       IMPORTING

         return = ls_return.

     IF ls_return IS INITIAL.

       WRITE:/ 'Recored udated successfully'.

     ENDIF.

   ELSE.

     WRITE:/ 'Update failed'.

   ENDIF.


The above sample will update the contact with new item , item condition and condition scale values.


if you want to update the scale values with quantity scale use the above code

the SORTing of the scale table is more import also the line number update on the item scale value update table is more important.

0 View this answer in context
Not what you were looking for? View more on this topic or Ask a question