BAPI_SALESORDER_CHANGE Sample program

BAPI_SALESORDER_CHANGE Sample program. Here are the details:

Below is a sample program using BAPI_SALESORDER_CHANGE in SAP to modify sales orders. Please note that the code provided is a simplified example and may require modification based on your specific requirements.

REPORT Z_SALESORDER_CHANGE.

* Include necessary function modules

INCLUDE <BAPI_INCLUDE>.

DATA: lv_order_number TYPE VBELN,

      lt_order_data TYPE TABLE OF BAPISDHD1,

      lt_order_items TYPE TABLE OF BAPISDITM,

      lt_order_schedule TYPE TABLE OF BAPISCHDL,

      lt_return TYPE TABLE OF BAPIRET2.

DATA: ls_order_header TYPE BAPISDHD1,

      ls_order_item TYPE BAPISDITM,

      ls_order_schedule TYPE BAPISCHDL.

START-OF-SELECTION.

  PERFORM get_order_data.

  PERFORM modify_order_data.

  PERFORM modify_order_items.

  PERFORM modify_order_schedule.

  PERFORM update_sales_order.

*&———————————————————————*

*&      Form  GET_ORDER_DATA

*&———————————————————————*

FORM get_order_data.

  lv_order_number = ‘123456’. ” Specify the sales order number to be changed

  CALL FUNCTION ‘BAPI_SALESORDER_GETDETAIL’

    EXPORTING

      salesdocument = lv_order_number

    TABLES

      return = lt_return

      salesheader = lt_order_data

      salesitems = lt_order_items

      salesschedule = lt_order_schedule.

  IF lt_return[] IS NOT INITIAL.

    LOOP AT lt_return INTO DATA(ls_return).

      WRITE: / ‘Error: ‘, ls_return-type, ls_return-message.

    ENDLOOP.

    EXIT.

  ENDIF.

  READ TABLE lt_order_data INTO ls_order_header INDEX 1.

ENDFORM.

*&———————————————————————*

*&      Form  MODIFY_ORDER_DATA

*&———————————————————————*

FORM modify_order_data.

  IF ls_order_header IS INITIAL.

    EXIT.

  ENDIF.

  ” Modify relevant fields in ls_order_header structure

  ls_order_header-ref_1 = ‘Modified Ref 1’.

  ls_order_header-ref_2 = ‘Modified Ref 2’.

  MODIFY lt_order_data FROM ls_order_header INDEX 1 TRANSPORTING ref_1 ref_2.

ENDFORM.

*&———————————————————————*

*&      Form  MODIFY_ORDER_ITEMS

*&———————————————————————*

FORM modify_order_items.

  IF lt_order_items[] IS INITIAL.

    EXIT.

  ENDIF.

  LOOP AT lt_order_items INTO ls_order_item.

    ” Modify relevant fields in ls_order_item structure

    ls_order_item-material = ‘MOD000001’.

    ls_order_item-target_qty = 10.

    MODIFY lt_order_items FROM ls_order_item INDEX sy-tabix TRANSPORTING material target_qty.

  ENDLOOP.

ENDFORM.

*&———————————————————————*

*&      Form  MODIFY_ORDER_SCHEDULE

*&———————————————————————*

FORM modify_order_schedule.

  IF lt_order_schedule[] IS INITIAL.

    EXIT.

  ENDIF.

  LOOP AT lt_order_schedule INTO ls_order_schedule.

    ” Modify relevant fields in ls_order_schedule structure

    ls_order_schedule-sched_line = ’10’.

    ls_order_schedule-req_qty = 5.

    MODIFY lt_order_schedule FROM ls_order_schedule INDEX sy-tabix TRANSPORTING sched_line req_qty.

  ENDLOOP.

ENDFORM.

*&———————————————————————*

*&      Form  UPDATE_SALES_ORDER

*&———————————————————————*

FORM update_sales_order.

  CALL FUNCTION ‘BAPI_SALESORDER_CHANGE’

    EXPORTING

      salesdocument = lv_order_number

    TABLES

      return = lt_return

      salesheader = lt_order_data

      salesitems = lt_order_items

      salesschedule = lt_order_schedule.

  IF lt_return[] IS NOT INITIAL.

    LOOP AT lt_return INTO DATA(ls_return).

      WRITE: / ‘Error: ‘, ls_return-type, ls_return-message.

    ENDLOOP.

  ELSE.

    WRITE: ‘Sales order’, lv_order_number, ‘successfully modified.’.

  ENDIF.

ENDFORM.

In this program, we start by including the necessary function module using the <BAPI_INCLUDE> statement. Then we define various data structures to hold the sales order data, such as order header, items, schedule, and return messages.

Inside the START-OF-SELECTION block, we call several forms to perform different tasks. The get_order_data form retrieves the sales order data using the BAPI_SALESORDER_GETDETAIL function module. It populates the relevant internal tables (lt_order_data, lt_order_items, lt_order_schedule) and checks for any return messages.

The modify order_data, modify_order_items, and modify_order_schedule forms are used to modify specific fields in the sales order header, items, and schedule, respectively. You can modify these forms to adjust the fields according to your requirements.

Finally, the update_sales_order form calls the BAPI_SALESORDER_CHANGE function module to update the modified sales order data. It checks for any return messages and displays success or error messages accordingly.

Please note that you need to replace the sample sales order number ‘123456’ with the actual sales order number you want to modify, and make sure to adjust the field modifications in the relevant forms based on your specific requirements.

Remember to test the program thoroughly in a development or test environment before running it in a production system.

Here’s what we did: This is a two step process because the BAPI to change
sales orders, BAPI_SALESORDER_CHANGE, doesn’t have the functionality
to update conditions. So, what we have to do is call the change BAPI first
with the bare minimum fields populated and the logic switch set to ‘B’ carry
out new pricing). This will reset the conditions we needed to reset, ZR00
and ZN02 prices. Of course, after this call we need to commit the changes
to the database. Secondly, we make another call to the change BAPI, this
time we include the new ZR00 and ZN02 prices. And then commit the
changes to the DB. This ain’t the prettiest thing in the world, but it works.

Code:
REPORT Y_SD_ORDER_REPRICE_BAPI.

TABLES: VBAP.

DATA: IRETURN TYPE STANDARD TABLE OF BAPIRET2 WITH HEADER LINE,
IORDER_KEYS TYPE STANDARD TABLE OF BAPISDKEY WITH HEADER LINE,
IORDER_HDR_IN TYPE STANDARD TABLE OF BAPISDHD1 WITH HEADER LINE,
IORDER_HDR_INX TYPE STANDARD TABLE OF BAPISDHD1X WITH HEADER LINE,
IORDER_ITEM_IN TYPE STANDARD TABLE OF BAPISDITM WITH HEADER LINE,
IORDER_ITEM_INX TYPE STANDARD TABLE OF BAPISDITMX WITH HEADER LINE,
IORDER_COND_IN TYPE STANDARD TABLE OF BAPICOND WITH HEADER LINE,
IORDER_COND_INX TYPE STANDARD TABLE OF BAPICONDX WITH HEADER LINE,
IORDER_TEXT TYPE STANDARD TABLE OF BAPISDTEXT WITH HEADER LINE,
COMMIT_RETURN TYPE STANDARD TABLE OF BAPIRET2 WITH HEADER LINE,
IORDER_NUMBER LIKE BAPIVBELN-VBELN,
V_MSG_TXT(220),
MSG_ID LIKE SY-MSGID,
MSG_NO LIKE SY-MSGNO,
MSG_V1 LIKE SY-MSGV1,
CHANGE_ITEM_NUMBER LIKE VBAP-POSNR.

DATA: ILOGIC_SWITCH LIKE BAPISDLS.

PARAMETER: ORDER LIKE VBAK-VBELN MATCHCODE OBJECT VMVA,
ITEM LIKE VBAP-POSNR.

START-OF-SELECTION.

PERFORM NEW_PRICING.
PERFORM COMMIT_ORDER.
PERFORM ADD_NEW_CONDITIONS.
PERFORM COMMIT_ORDER.

*&———————————————————————*
*& Form NEW_PRICING
*&———————————————————————*
* text
*———————————————————————-*
* –> p1 text
* <– p2 text
*———————————————————————-*
FORM NEW_PRICING.

SKIP.
WRITE:/ ‘* * * CARRY OUT NEW PRICING * * *’.
SKIP.

SELECT SINGLE * FROM VBAP WHERE VBELN = ORDER
AND POSNR = ITEM.

IORDER_NUMBER = VBAP-VBELN.

*……..Insert New Item………………………………………………
*……..Load Sales Order Item Table………………………………

CLEAR IORDER_ITEM_IN.
IORDER_ITEM_IN-ITM_NUMBER = VBAP-POSNR.
IORDER_ITEM_IN-MATERIAL = VBAP-MATNR.
IORDER_ITEM_IN-ITEM_CATEG = VBAP-PSTYV.
APPEND IORDER_ITEM_IN.

CLEAR IORDER_ITEM_INX.
IORDER_ITEM_INX-ITM_NUMBER = VBAP-POSNR.
IORDER_ITEM_INX-UPDATEFLAG = ‘U’.
APPEND IORDER_ITEM_INX.

ILOGIC_SWITCH-PRICING = ‘B’. “CARRY OUT NEW PRICING

*……..Call Sales Order Create RFC………………………………
CALL FUNCTION ‘BAPI_SALESORDER_CHANGE’
EXPORTING
SALESDOCUMENT = IORDER_NUMBER
ORDER_HEADER_INX = ‘U’
LOGIC_SWITCH = ILOGIC_SWITCH
TABLES
RETURN = IRETURN
ORDER_KEYS = IORDER_KEYS
ORDER_ITEM_IN = IORDER_ITEM_IN
ORDER_ITEM_INX = IORDER_ITEM_INX
CONDITIONS_IN = IORDER_COND_IN
CONDITIONS_INX = IORDER_COND_INX.

ENDFORM. “new pricing
*&———————————————————————*
*& Form commit_order
*&———————————————————————*
* text
*———————————————————————-*
* –> p1 text
* <– p2 text
*———————————————————————-*
FORM COMMIT_ORDER.

WRITE:/5 ‘ORDER NUMBER –>’, IORDER_NUMBER.
SKIP.

LOOP AT IRETURN.

CALL FUNCTION ‘MESSAGE_TEXT_BUILD’
EXPORTING
MSGID = IRETURN-ID
MSGNR = IRETURN-NUMBER
MSGV1 = IRETURN-MESSAGE
IMPORTING
MESSAGE_TEXT_OUTPUT = V_MSG_TXT
EXCEPTIONS
OTHERS = 1.

WRITE: / V_MSG_TXT.
ENDLOOP.

*……..Order has to be commited to database………………………

CALL FUNCTION ‘BAPI_TRANSACTION_COMMIT’
IMPORTING
RETURN = COMMIT_RETURN.

REFRESH: IORDER_HDR_IN, IORDER_ITEM_IN, IRETURN.

ENDFORM. ” commit_order
*&———————————————————————*
*& Form ADD_NEW_CONDITIONS
*&———————————————————————*
* text
*———————————————————————-*
* –> p1 text
* <– p2 text
*———————————————————————-*
FORM ADD_NEW_CONDITIONS.

SKIP.
WRITE:/ ‘* * * ADD NEW PRICES * * *’.
SKIP.

REFRESH: IRETURN, IORDER_ITEM_IN, IORDER_ITEM_INX, IORDER_COND_IN,
IORDER_COND_INX.

IORDER_NUMBER = VBAP-VBELN.

*……..Load Sales Order Item Table………………………………
CLEAR IORDER_ITEM_IN.
IORDER_ITEM_IN-ITM_NUMBER = VBAP-POSNR.
IORDER_ITEM_IN-MATERIAL = VBAP-MATNR.
IORDER_ITEM_IN-ITEM_CATEG = VBAP-PSTYV.
APPEND IORDER_ITEM_IN.

CLEAR IORDER_ITEM_INX.
IORDER_ITEM_INX-ITM_NUMBER = VBAP-POSNR.
IORDER_ITEM_INX-UPDATEFLAG = ‘U’.
APPEND IORDER_ITEM_INX.

*……..Load Sales Order Pricing Conditions……………………….
CLEAR IORDER_COND_IN.
IORDER_COND_IN-ITM_NUMBER = VBAP-POSNR.
IORDER_COND_IN-COND_TYPE = ‘ZR00’.
IORDER_COND_IN-COND_VALUE = ‘175’.
IORDER_COND_IN-CURRENCY = ‘USD’.
APPEND IORDER_COND_IN.

*……..Indicate Change to Condition……………………………..
CLEAR IORDER_COND_INX.
IORDER_COND_INX-ITM_NUMBER = VBAP-POSNR.
IORDER_COND_INX-COND_TYPE = ‘ZR00’.
IORDER_COND_INX-UPDATEFLAG = ‘I’.
IORDER_COND_INX-COND_VALUE = ‘X’.
IORDER_COND_INX-CURRENCY = ‘X’.
APPEND IORDER_COND_INX.

*……..Load Sales Order Pricing Conditions……………………….
CLEAR IORDER_COND_IN.
IORDER_COND_IN-ITM_NUMBER = VBAP-POSNR.
IORDER_COND_IN-COND_TYPE = ‘ZN02’.
IORDER_COND_IN-COND_VALUE = ‘175’.
IORDER_COND_IN-CURRENCY = ‘USD’.
APPEND IORDER_COND_IN.

*……..Indicate Change to Condition……………………………..
CLEAR IORDER_COND_INX.
IORDER_COND_INX-ITM_NUMBER = VBAP-POSNR.
IORDER_COND_INX-COND_TYPE = ‘ZN02’.
IORDER_COND_INX-UPDATEFLAG = ‘I’.
IORDER_COND_INX-COND_VALUE = ‘X’.
IORDER_COND_INX-CURRENCY = ‘X’.
APPEND IORDER_COND_INX.

*……..Call Sales Order Create RFC………………………………
CALL FUNCTION ‘BAPI_SALESORDER_CHANGE’
EXPORTING
SALESDOCUMENT = IORDER_NUMBER
ORDER_HEADER_INX = ‘U’
TABLES
RETURN = IRETURN
ORDER_ITEM_IN = IORDER_ITEM_IN
ORDER_ITEM_INX = IORDER_ITEM_INX
CONDITIONS_IN = IORDER_COND_IN
CONDITIONS_INX = IORDER_COND_INX.

ENDFORM. ” ADD_NEW_CONDITIONSGood Luck Mike – I hope this helps!
__________________

BAPI_SALESORDER_CHANGE Sample program

Leave a comment