BAPI To Update Sales Order Reason For Rejection

Please find below a sample program using the BAPI \’BAPI_SALESORDER_CHANGE\’ to demonstrate how to update the reason for rejection (VBAP-ABGRU) for all items in a sales order.

 

REPORT ztestbapiso1.
*&--------------------------------------------------------------------&*
*& Program Description:                                               &*
*& -----------------------                                            &*
*& This demo program will update the reason for rejection for all     &*
*& items in a selected sales order.                                   &*
*&                                                                    &*
*& The program demonstrate the use of the \'BAPI_SALESORDER_CHANGE\'.   &*
*&                                                                    &*
*& Author:  ABAPCOOKBOOK                                              &*
*& Website: www.abapcookbook.com                                      &*
************************************************************************

************************************************************************
* DATA DECLARATIONS                                                    *
************************************************************************
*Tables:
TABLES:
  vbap.

*Internal tables:
DATA:
  gt_vbap           TYPE STANDARD TABLE OF vbap,
  gt_item_in        TYPE STANDARD TABLE OF bapisditm,
  gt_item_inx       TYPE STANDARD TABLE OF bapisditmx,
  gt_return         TYPE STANDARD TABLE OF bapiret2.

*Field Symbols:
FIELD-SYMBOLS:
  <fs_vbap>         TYPE vbap.

*Structures:
DATA:
  gst_item_hedx     TYPE bapisdh1x,
  gst_item_in       TYPE bapisditm,
  gst_item_inx      TYPE bapisditmx.

*Variables:
DATA:
  gv_msg            TYPE string.

*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.


************************************************************************
* 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.

  LOOP AT gt_vbap ASSIGNING <fs_vbap>.

*   (Order Header Level)
*   Setting the update flag at order header level to update mode.
    gst_item_hedx-updateflag = \'U\'.

*   (Order Item Level)
*   Setting of the material number(MATNR) at order item level.
    gst_item_in-material = <fs_vbap>-matnr.

*   Setting of the item number(POSNR) at order item level.
    gst_item_in-itm_number  = <fs_vbap>-posnr.
    gst_item_inx-itm_number = <fs_vbap>-posnr.

*   Setting of the reason for rejection(ABGRU) at order item level.
    gst_item_in-reason_rej  = p_abgru.
    gst_item_inx-reason_rej = \'X\'.

*   Setting the update flag at order item level to update mode.
    gst_item_inx-updateflag = \'U\'.

*   BAPI items level tables:
    APPEND:
      gst_item_in  TO gt_item_in,
      gst_item_inx TO gt_item_inx.

*   Calling BAPI to update reason for rejection in the selected sales order.
    CALL FUNCTION \'BAPI_SALESORDER_CHANGE\'
      EXPORTING
        salesdocument    = <fs_vbap>-vbeln
        order_header_inx = gst_item_hedx
      TABLES
        return           = gt_return
        order_item_in    = gt_item_in
        order_item_inx   = gt_item_inx.

*   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 BAPI. 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.

*     Rollback changes.
      CALL FUNCTION \'BAPI_TRANSACTION_ROLLBACK\'.

*     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.

*     Commit changes.
      CALL FUNCTION \'BAPI_TRANSACTION_COMMIT\'
        EXPORTING
          wait = abap_true.

*     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,
*     Structures:
      gst_item_hedx,
      gst_item_in,
      gst_item_inx.

*   Refreshing internal tables:
    REFRESH:
      gt_item_in,
      gt_item_inx,
      gt_return.

  ENDLOOP.

ENDIF.

In case of error, please check the BAPI return messages available in the internal table \’GT_RETURN\’.

One response to “BAPI To Update Sales Order Reason For Rejection”