Please find below a sample code which demonstrate the use of the
function module ‘SD_BACKORDER_UPDATE’ to change or cancel the confirmed
quantity (VBEP-BMENG) in sales order schedule lines.
The nice part of this function module is that whenever the confirmed
quantity is change or cancel, ATP check is carried out automatically.
REPORT ztestfmconfirmedquantity.
************************************************************************
* DATA DECLARATIONS *
************************************************************************
*Tables:
TABLES:
vbap,
vbep.
*Internal tables:
DATA:
gt_vbap TYPE STANDARD TABLE OF vbap,
gt_vbep TYPE STANDARD TABLE OF vbep,
gt_kortab TYPE STANDARD TABLE OF mdvu,
gt_return TYPE STANDARD TABLE OF bapiret2.
*Field Symbols:
FIELD-SYMBOLS:
<fs_vbap> TYPE vbap,
<fs_vbep> TYPE vbep.
*Structures:
DATA:
gst_kortab TYPE mdvu.
*Variables:
DATA:
gv_msg TYPE string,
gv_tabix TYPE sy-tabix.
*Constants:
CONSTANTS:
gc_error TYPE string VALUE ': An error occured, no change done to the sales order.',
gc_success TYPE string VALUE ': Sales order changed successfully.'.
************************************************************************
* SELECTION SCREEN *
************************************************************************
SELECT-OPTIONS:
* Sales Order Number.
s_vbeln FOR vbap-vbeln OBLIGATORY.
PARAMETERS:
* Reason for Rejection.
p_abgru TYPE vbap-abgru OBLIGATORY,
* Confirm Quantity.
p_bmeng TYPE vbep-bmeng OBLIGATORY.
************************************************************************
* CODE LOGIC *
************************************************************************
*Select sales order data from table VBAP.
SELECT *
FROM vbap
INTO TABLE gt_vbap
WHERE vbeln IN s_vbeln.
IF sy-subrc EQ 0.
* Rules 'For All Entries'.
* Not necessary as 'VBELN' and 'POSNR' is already primary key, this logic
* just to demonstrate 'For All Entries' rule.
SORT gt_vbap BY vbeln posnr.
DELETE ADJACENT DUPLICATES FROM gt_vbap COMPARING vbeln posnr.
* Retrieving schedule lines entries.
SELECT *
FROM vbep
INTO TABLE gt_vbep
FOR ALL ENTRIES IN gt_vbap
WHERE vbeln EQ gt_vbap-vbeln
AND posnr EQ gt_vbap-posnr.
IF sy-subrc EQ 0.
* Sorting for binary search.
SORT gt_vbep BY vbeln posnr.
LOOP AT gt_vbap ASSIGNING <fs_vbap>.
* ........................
* FM Data for updating the confirm quantity.
* ........................
* Adding the schedule lines items.
READ TABLE gt_vbep TRANSPORTING NO FIELDS
WITH KEY vbeln = <fs_vbap>-vbeln
posnr = <fs_vbap>-posnr
BINARY SEARCH.
IF sy-subrc EQ 0.
gv_tabix = sy-tabix.
* Index looping for better performance.
LOOP AT gt_vbep ASSIGNING <fs_vbep> FROM gv_tabix.
IF <fs_vbep>-vbeln EQ <fs_vbap>-vbeln
AND <fs_vbep>-posnr EQ <fs_vbap>-posnr.
* Current item of the sales order for the current
* availability date.
gst_kortab-vbeln = <fs_vbep>-vbeln.
gst_kortab-posnr = <fs_vbep>-posnr.
gst_kortab-mbdat = <fs_vbep>-mbdat.
* Setting the confirm quantity. Please note that
* setting this value to '0' will cancel the confirm quantity
* in the sales order on all schedule lines.
gst_kortab-vmeng = p_bmeng.
APPEND gst_kortab TO gt_kortab.
ELSE.
* Clear index
CLEAR gv_tabix.
* Move out of the loop.
EXIT.
ENDIF.
* Clearing of work areas.
CLEAR:
gst_kortab.
ENDLOOP.
ENDIF.
* Calling function module to update confirm quantity. The ATP check is
* also generated using this function module.
* Please Note: The drawback of this FM is that it automatically commit
* changes to database even if a sales order is in edit mode and
* it don't look that much perfomant.
CALL FUNCTION 'SD_BACKORDER_UPDATE'
TABLES
kortab = gt_kortab
et_return = gt_return.
* Preparing the result message.
CONCATENATE <fs_vbap>-vbeln " Sales Order Number
<fs_vbap>-posnr " Item Number
INTO gv_msg " Message
SEPARATED BY space. " Space
* Check if at least one error was raised by the FM. Loop inside
* loop is not advise, however, the return table will contains small
* amount of entries. We can use that for our demo.
LOOP AT gt_return TRANSPORTING NO FIELDS
WHERE type EQ 'E'
OR type EQ 'A'.
* Exit and rollback changes.
EXIT.
ENDLOOP.
* If error found, rollback database changes.
IF sy-subrc EQ 0.
* Preparing error message.
CONCATENATE gv_msg "Sales Order and Item Number
gc_error "Error Message
INTO gv_msg
SEPARATED BY space.
* Output message.
WRITE / gv_msg.
* Else, no error found, commit database changes.
ELSE.
* Preparing success message.
CONCATENATE gv_msg "Sales Order and Item Number
gc_success "Success Message
INTO gv_msg
SEPARATED BY space.
* Output message.
WRITE / gv_msg.
ENDIF.
* Write a line after each sales order.
AT END OF vbeln.
WRITE: sy-uline.
ENDAT.
* Clearing of variables and structures:
CLEAR:
* Variables:
gv_msg,
gv_tabix.
* Refreshing internal tables:
REFRESH:
gt_kortab,
gt_return.
ENDLOOP.
ENDIF.
ENDIF.
Serkan AKKAVAK
Computer Engineer
ABAP Developer & SAP S/4 HANA Logistics Consultant
Contact : serkurumsal@yandex.com
Social Plugin