Search on this Website

Saturday, July 23, 2011

BAPI_SALESORDER_CHANGE Sample program

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!
__________________

No comments: