Skip to Content

As in CRM projects we come across requirement of changing value of pricing condition(e.g. PB00). below mentioned is the code for doing it in ORDER_SAVE BAdi. I tried searching on SDN and  got  many FMs but  unfortunately none of them worked for me and then i tried  the  solution below and it  worked  for me.

The method used in this BAdi is  “PREPARE” method.

using code below the value of pricing condition of any item can be  changed using Genil BOL ABAP coding.

in the  scenario below  we are  fetching price of material from ECC system using RFC.  this can be replaced by custom logic of your own.

before coding the First check you check the pricing condition configuration for PR00 in IMG CRM –> Basic Functions –> Pricing –> Define Settings for Pricing –> Create Condition Types

in this For PB00 the either “Manual Entry has Priority” or “No Restrictions” should be selected.

Pricing.png

Code in PREPARE method goes as shown below..

****Reading the existing Order****

INSERT ‘APPOINTMENT’ INTO lt_req INDEX 1.

INSERT ‘ORDERADM_H’ INTO lt_req INDEX 2.

INSERT ‘ORDERADM_I’ INTO lt_req INDEX 3.

INSERT ‘PRIDOC’ INTO lt_req INDEX 4.

INSERT iv_guid INTO lt_guid INDEX 1.

CALL FUNCTION ‘CRM_ORDER_READ’

  EXPORTING

    it_header_guid       = lt_guid

    it_requested_objects = lt_req

  IMPORTING

    et_orderadm_h        = lt_order_h

    et_orderadm_i        = lt_order_i

    et_appointment       = lt_appointmnet

    et_pridoc            = lt_pridoc

  EXCEPTIONS

    document_not_found   = 1

    error_occurred       = 2

    document_locked      = 3

    no_change_authority  = 4

    no_display_authority = 5

    no_change_allowed    = 6

    OTHERS               = 7.

IF sy-subrc <> 0.

* Implement suitable error handling here

ENDIF.

****Calling RFC in ECC to get new Price of  material maintained in ECC****

CALL FUNCTION ‘ZGET_MATPRICE_ECC’

  EXPORTING

    it_header_guid = ls_order_h-guid

  IMPORTING

    lt_dn_price    = lt_price

  EXCEPTIONS

    error_occurred = 1

    OTHERS         = 2.

IF sy-subrc <> 0.

* Implement suitable error handling here

ENDIF.

***lt_price table will be  filled with the material number(MATNR), its price in ECC (KBETR) and its currency(KONWA)***

IF lt_price IS NOT INITIAL.

  lo_core = cl_crm_bol_core=>get_instance( ).

  lo_core->start_up( ‘BT’ ).

  CHECK lo_core IS BOUND.

  lo_entity2 ?= lo_core->get_root_entity( iv_object_name = ‘BTOrder’   “Get Root entity

                              iv_object_guid = ls_order_h-guid ).

  CHECK lo_entity2 IS BOUND.

  lo_entity2 = lo_entity2->get_related_entity( ‘BTOrderHeader’ ).      “Get Header entity

  IF lo_entity2->lock( ) = abap_true.

    lo_entity2 = lo_entity2->get_related_entity( ‘BTHeaderItemsExt’ ).  “Get Static Item entity

    LOOP AT lt_order_i INTO ls_order_i.

      READ TABLE lt_cond INTO  ls_cond  WITH KEY  kposn = ls_order_i-guid

                                                  kschl = ‘PB00’.

      IF sy-subrc EQ 0.

        READ TABLE lt_price INTO ls_price WITH KEY matnr = ls_order_i-ordered_prod.

        IF sy-subrc EQ 0.

          CHECK lo_entity2 IS BOUND.

          TRY.

              CALL METHOD lo_entity2->get_related_entities              “Get All Item entities

                EXPORTING

                  iv_relation_name = ‘BTOrderItemAll’

                RECEIVING

                  rv_result        = lr_coll.

            CATCH cx_crm_genil_model_error .

          ENDTRY.

          CHECK lr_coll IS BOUND.

          lo_entity = lr_coll->get_first( ).

          WHILE lo_entity IS BOUND.

            CALL METHOD lo_entity->if_bol_bo_property_access~get_property_as_value

              EXPORTING

                iv_attr_name = ‘ORDERED_PROD’

              IMPORTING

                ev_result    = lv_product.

            IF lv_product EQ ls_price-matnr.

              lo_entity = lo_entity->get_related_entity( ‘BTItemConditionSet’ ).              “Get Static Item Pricing entity

              IF lo_entity IS BOUND.

                TRY.

                    CALL METHOD lo_entity->get_related_entities

                      EXPORTING

                        iv_relation_name = ‘BTCondICondLineAll’             “Get Item Pricing entity which is to be changed

                      RECEIVING

                        rv_result        = lr_coll1.

                  CATCH cx_crm_genil_model_error .

                ENDTRY.

                IF lr_coll IS BOUND.

                  lo_entity1 = lr_coll1->get_first( ).

                  WHILE lo_entity1 IS BOUND.

                    CALL METHOD lo_entity1->if_bol_bo_property_access~get_property_as_value

                      EXPORTING

                        iv_attr_name = ‘KSCHL’

                      IMPORTING

                        ev_result    = lv_cond.

                    CALL METHOD lo_entity1->if_bol_bo_property_access~get_property_as_value

                      EXPORTING

                        iv_attr_name = ‘KPOSN’

                      IMPORTING

                        ev_result    = lv_item.

                    IF lv_cond EQ ‘PB00’ AND lv_item EQ ls_order_i-guid.

***Below is the Logic of Updating PB00 Pricing value of Item entity

                      lo_entity->switch_to_change_mode( ).

                      CALL METHOD lo_entity1->if_bol_bo_property_access~set_property

                        EXPORTING

                          iv_attr_name = ‘KBETR’

                          iv_value     = ls_price-kbetr.

                      CALL METHOD lo_core->get_transaction

                        RECEIVING

                          rv_result = lr_trnx.

                      lo_core->modify( ).

                      TRY.

                          CALL METHOD lr_test->check_save_possible

                            RECEIVING

                              rv_result = lr_result.

                        CATCH cx_crm_genil_general_error .

                      ENDTRY.

                    ENDIF.

                    lo_entity1 = lr_coll1->get_next( ).

                  ENDWHILE.

                ENDIF.

              ENDIF.

            ENDIF.

            lo_entity = lr_coll->get_next( ).

          ENDWHILE.

        ENDIF.

      ENDIF.

    ENDLOOP.

  ENDIF.

ENDIF.

The Code above might need some Optimization. Suggestions are  welcome to make this  code more optimized.

This is just to give an  idea  of  solving this pricing condition value change requirement…

To report this post you need to login first.

1 Comment

You must be Logged on to comment or reply to a post.

Leave a Reply