• How To Use Function Module READ_TEXT

    The function module \’READ_TEXT\’ allows us to read standard text.

    For example, let say you want to use the FM \’READ_TEXT\’ to retrieve a specific text from a sales order.

    Step 1:

    The first thing to do is to find the appropriate parameters to pass to the FM to retrieve that specific text.

    Thus, modify the sales order in VA02 and enter some text in the field you want to read the text.

    Step 2:

    Activate debugging mode (/h) and save the sales order. In debug, create a break point on the function module \’SAVE_TEXT\’.

    \"\"

    Step 3:

    All parameters which we will needing to pass to the function module \’READ_TEXT\’ will be available in:

    • <HEADER>-TDOBJECT
    • <HEADER>-TDNAME
    • <HEADER>-TDID
    • <HEADER>-TDSPRAS

    \"\"

    Step 4:

    Once the appropriate parameters have been found, pass them to the FM \’READ_TEXT\’ and read the required returning text.

    Please find below a sample code.

    REPORT ZREADTEXT.
    
    *&--------------------------------------------------------------------&*
    *& Program Description:                                               &*
    *& -----------------------                                            &*
    *& This demo program demonstrate how to use the function module       &*
    *& \'READ_TEXT\' to find standard text.                                 &*
    *&                                                                    &*
    *& Author:  ABAPCOOKBOOK                                              &*
    *& Website: www.abapcookbook.com                                      &*
    ************************************************************************
    
    DATA:
      lv_output_text TYPE char50.
    
    *Subroutine using the FM \'READ_TEXT\' to return
    *the appropriate standard text.
    
    *Please Note: To find the required parameters (for example a text in
    *a Sales Order), in VA02, enter some text in the field in question,
    *enable debugger(/h) and put a break point on the function module \'SAVE_TEXT\'.
    *All appropriate parameters will be available in the field symbol \'<HEADER>\'.
    PERFORM f_read_text USING \'ABCD\'            \" Text ID
                              \'E\'               \" Text Language
                              \'12345678\'        \" Text Name
                              \'VBBK\'            \" Text Object 
                     CHANGING lv_output_text.   \" Return Text
    
    *Display the standard text.
    WRITE lv_output_text.
    
    *----------------------------------------------------------------------*
    * Sub routine to read text from standard transactions using the        *
    * function module \'READ_TEXT\'.                                         *
    *----------------------------------------------------------------------*
    FORM f_read_text USING pi_textid      TYPE thead-tdid
                           pi_language    TYPE thead-tdspras
                           pi_name        TYPE thead-tdname
                           pi_object      TYPE thead-tdobject
                  CHANGING pc_output_text TYPE char50.
    *----------------------------------------------------------------------*
    * Local Data Declarations.
    * Internal Tables.
      DATA:
        lt_lines TYPE STANDARD TABLE OF tline.
    
    * Field Symbols.
      FIELD-SYMBOLS:
        <fs_lines> TYPE tline.
    
    * Clearing field to be on the safer side.
      CLEAR pc_output_text.
    
      CALL FUNCTION \'READ_TEXT\'
        EXPORTING
          id                      = pi_textid
          language                = pi_language
          name                    = pi_name
          object                  = pi_object
        TABLES
          lines                   = lt_lines
        EXCEPTIONS
          id                      = 1
          language                = 2
          name                    = 3
          not_found               = 4
          object                  = 5
          reference_check         = 6
          wrong_access_to_archive = 7
          OTHERS                  = 8.
    
      IF sy-subrc EQ 0.
    
    *   Getting the text from the FM table.
        READ TABLE lt_lines
        ASSIGNING <fs_lines>
        INDEX 1.
    
        IF sy-subrc EQ 0.
    
    *     Standard text.
          pc_output_text = <fs_lines>-tdline.
    
        ENDIF.
    
      ENDIF.
    
    ENDFORM.

    Program Output:

    \"\"

    Easy! 🙂 Hope it helps.

  • Output Type Not Process During Post Goods Issue

    If you are having an issue where the output type is not turning green (processed):

    • during post goods issue in the delivery
    • upon receiving an iDoc which will do the post goods issue (PGI)
    • any other process in background where the output type is supposed to be processed

    Then one possible reason may be due to an incorrect SAP configuration.

     

    Explanation:

    Normally, the program RSNAST00 is responsible for processing of output types. The program RSNAST00 will process entry found in table NAST where the field NAST-VSZTP equals to 1 or 2. Thus, if the output type has not been processed means that maybe no line was added in table NAST or a line was added but the field NAST-VSZTP does not equals to 1 or 2. You may refer to the post for more information on program RSNAST00.

     

    Possible Solution:

    According to OSS note 509475, one possible solution is to enable the output determination in transaction VL10CUC (\”Parameter per Profile in Delivery Process\”).

    Configuration Steps:

    Step 1: In transaction code \’VL10CUC\’, double click on the delivery creation \’2000\’

    Step 2: Untick the checkbox \’No OutputDet.\’ (as shown in the picture below) to enable output type determination

    \"\"

    Please note that output type can be configured in transaction VV22, VV23, etc…

  • Solve Error \’File Has Already Been Loaded\’ in FEBC

    If you received the error message \’File has already been loaded\’ in transaction FEBC (program RFEBIT00), please find below an explanation of this issue and how to solve it.

    \"\"

    Please note that the program RFEBIT00 is used to create multicash files from a bank file.

     

    Issue Description:

    Normally when a bank file is uploaded in FEBC (program RFEBIT00), an entry is created in table \’FIPYXXD_INSTMT\’.

    When the same file is uploaded a second time, the program RFEBIT00 will check if the entry exists in table \’FIPYXXD_INSTMT\’, if yes, means that the bank file has already been processed.

    If the entry is already in table \’FIPYXXD_INSTMT\’, the error message \’File has already been loaded\’ will be displayed. This check ensures that the same file is not uploaded twice.

    This check is related to the application of the OSS note 1705402 on the system.

    Sometimes during uploading of the bank file (due to some wrong steps), it may happen that the file has not been uploaded successfully and the entry created in table \’FIPYXXD_INSTMT\’. This will prevent the user from uploading the file a second due to the check that will display the error message \’File has already been loaded\’.

     

    Solution:

    The solution is to delete the entry from table \’FIPYXXD_INSTMT\’ before trying to upload the bank file again.

    According to the OSS notes 2043763, please follow below steps in transaction FEBC (program RFEBIT00):

     

    Step 1: Tick the checkbox ‘Delete’, enter the ‘Statement ID’ or ‘Bank Number’ to delete in the field ‘Bank Number’ and execute the program. This will delete the appropriate entry from table \’FIPYXXD_INSTMT\’.

    \"\"

    Step 2: Once the file has been deleted, please upload the bank file again.

     

    Please Note: When deleting entries from table FEBKO by using program \’RFEBKA96\’  it won\’t delete related entries from table ‘FIPYXXD_INSTMT’.

  • How To Quickly Save ABAP Code To Your Computer

    This is a quick tip on how to easily save your ABAP code to your computer.

    When the code is being displayed in the ABAP Editor, just press the shortcut keys \’Ctrl + Shift + S\’. A popup will appear which will allows you to save the ABAP code on your computer with the extension \’.ABAP\’.

    To view the code in the \’.ABAP\’ file, just open it with Notepad.

    Hope this helps. 🙂

  • SAP ABAP Message Types

    This is an easy which demonstrate the different types of messages that can be displayed in an ABAP program.

    Types of ABAP messages:

    • Success message (S)
      • A success message appears along the SAP Logon footer (in green) and processing is not stop.

    \"\"

    • Error message (E)
      • Error message appears along the SAP Logon footer (in red) and processing is stopped.

    \"\"

    • Warning message (W)
      • Warning message is similar to error message, however depending on the context, error message may appear or program terminated.

    \"\"

    • Information message (I)
      • An information message appears in a popup window, processing halt. When the user close the information message, processing resume.

    \"\"

    • Abend /Termination Message message (A)
      • A termination message will stop processing and an exit button will appears which will exit the session.

    \"\"

    • Exit message (X)
      • An exit message will normally terminated the program. Usually, a stack dump for the state of the system is raised (can be viewed in ST22).

    \"\"

     

    Please find below a sample program which demonstrate the different types of messages available for SAP ABAP programs.

    REPORT zdemomessages.
    
    *&--------------------------------------------------------------------&*
    *& Program Description:                                               &*
    *& -----------------------                                            &*
    *& This demo program will demonstrate the different types of SAP ABAP &*
    *& messages.                                                          &*
    *&                                                                    &*
    *& Author:  ABAPCOOKBOOK                                              &*
    *& Website: www.abapcookbook.com                                      &*
    ************************************************************************
    
    
    ************************************************************************
    * SELECTION SCREEN                                                     *
    ************************************************************************
    PARAMETERS:
      rb_msg01 RADIOBUTTON GROUP msg DEFAULT \'X\',   \" Success Message
      rb_msg02 RADIOBUTTON GROUP msg,               \" Error Message
      rb_msg03 RADIOBUTTON GROUP msg,               \" Warning Message
      rb_msg04 RADIOBUTTON GROUP msg,               \" Information Message
      rb_msg05 RADIOBUTTON GROUP msg,               \" Abend/Termination Message
      rb_msg06 RADIOBUTTON GROUP msg.               \" Exit Message
    
    
    ************************************************************************
    * SAMPLE CODE LOGIC FOR DEMONSTRACTION PURPOSES                        *
    ************************************************************************
    START-OF-SELECTION.
    
      CASE \'X\'.
    
    *   Success Message.
    *   A success message appears along the SAP Logon footer (in green)
    *   and processing is not stop.
        WHEN rb_msg01.
    
            MESSAGE \'This is an example of a SUCCESS message.\' TYPE \'S\'.
    
    *   Error Message.
    *   Error message appears along the SAP Logon footer (in red) and
    *   processing is stopped.
        WHEN rb_msg02.
    
          MESSAGE \'This is an example of an ERROR message.\' TYPE \'E\'.
    
    *   Warning Message.
    *   Warning message is similar to error message, however depending
    *   on the context, error message may appear or program terminated.
        WHEN rb_msg03.
    
          MESSAGE \'This is an example of a WARNING message.\' TYPE \'W\'.
    
    *   Information Message.
    *   An information message appears in a popup window, processing halt.
    *   When the user close the information message, processing resume.
        WHEN rb_msg04.
    
          MESSAGE \'This is an example of an INFORMATION message.\' TYPE \'I\'.
    
    *   Abend/Termination Message.
    *   A termination message will stop processing and an exit button will
    *   appears which will exit the session.
        WHEN rb_msg05.
    
          MESSAGE \'This is an example of a TERMINATION message.\' TYPE \'A\'.
    
    *   Exit Message.
    *   An exit message will normally terminated the program. Usually, a
    *   stack dump for the state of the system is raised (can be viewed in ST22).
        WHEN rb_msg06.
    
          MESSAGE \'This is an example of an EXIT message.\' TYPE \'X\'.
    
      ENDCASE.

     

    Please Note: The syntax for the implementation of ABAP messages can also be in the following format:

    *Hardcoded success message.
     MESSAGE \'This is an example of a SUCCESS message.\' TYPE \'S\'.
            
    *Success message with message number \'001\' defined in the message class \'V1\' (via SE91). 
     MESSAGE S001(V1).
            
    *Success message with text symbols \'001\' defined in menu \'GoTO > Text Elements > Text Symbols\'.
     MESSAGE TEXT-001 TYPE \'S\'.

     

  • Back Button Not Working in GUI-STATUS/PF-STATUS

    Sometimes when copying GUI status form standard program, buttons (like back button) stops working.

    This issue can be resolved as follows:

    Step 1 : Button:

    Double click on the button which is not working.

    \"\"

    Step 2 – Function Code & Functional Type:

    Make sure that the function code and the functional type has been assigned to the button.

    \"\"

    Activate and enjoy. 🙂

  • Change Variant Content Dynamically

    The below program demonstrate how to change the content of variant at run time.

    REPORT ztestautoselectoption.
    *&--------------------------------------------------------------------&*
    *& Program Description:                                               &*
    *& -----------------------                                            &*
    *& This demo program shows how to dynamically populate the data in a  &*
    *& variant.                                                           &*
    *&                                                                    &*
    *& Author:  ABAPCOOKBOOK                                              &*
    *& Website: www.abapcookbook.com                                      &*
    ************************************************************************
    
    ************************************************************************
    * DATA DECLARATIONS                                                    *
    ************************************************************************
    *Tables:
    TABLES:
      vbap.
    
    *Internal tables:
    DATA:
      gt_vbap               TYPE STANDARD TABLE OF vbap,
      gt_vari               TYPE STANDARD TABLE OF rsparams.
    
    *Field Symbols:
    FIELD-SYMBOLS:
      <fs_vbap>             TYPE vbap.
    
    *Structures:
    DATA:
      gst_vari              TYPE rsparams,
      gst_varidesc          TYPE varid.
    
    *Variables:
    DATA:
      gv_subrc              TYPE sy-subrc.
    
    *Constants:
    CONSTANTS:
      gc_write              TYPE c                      VALUE \'W\',
      gc_selmat             TYPE rsparams-selname       VALUE \'S_MATNR\',
      gc_kind               TYPE rsparams-kind          VALUE \'S\',
      gc_sign               TYPE rsparams-sign          VALUE \'I\',
      gc_option             TYPE rsparams-low           VALUE \'EQ\'.
    
    ************************************************************************
    * SELECTION SCREEN                                                     *
    ************************************************************************
    SELECT-OPTIONS:
    *  Material Number.
       s_matnr FOR vbap-matnr OBLIGATORY.
    
    ************************************************************************
    * CODE LOGIC                                                           *
    ************************************************************************
    *Check if the variant used to run the program exists.
    CALL FUNCTION \'RS_VARIANT_EXISTS\'
      EXPORTING
        report              = sy-repid  \"Program Name
        variant             = sy-slset  \"Variant Name
      IMPORTING
        r_c                 = gv_subrc
      EXCEPTIONS
        not_authorized      = 01
        no_report           = 02
        report_not_existent = 03
        report_not_supplied = 04.
    
    IF gv_subrc EQ 0.
    
    * Select sales order data from table VBAP.
      SELECT * UP TO 10 ROWS
        FROM vbap
        INTO TABLE gt_vbap.
    
      IF sy-subrc EQ 0.
    
    *   Retrieve the contents of the variant to the internal table \'GT_VARI\'.
        CALL FUNCTION \'RS_VARIANT_CONTENTS\'
          EXPORTING
            report               = sy-repid  \"Program Name
            variant              = sy-slset  \"Variant Name
            move_or_write        = gc_write
          TABLES
            valutab              = gt_vari   \"Variant Contents
          EXCEPTIONS
            variant_non_existent = 1
            variant_obsolete     = 2
            OTHERS               = 3.
    
        IF sy-subrc EQ 0.
    
    *     Delete the current content of select option \'S_MATNR\' from the variant. This is
    *     done by deleting all \'S_MATNR\' selname entries from material select option(GT_VARI).
    *     We will then populate it with fresh new data later on.
          DELETE gt_vari WHERE selname EQ gc_selmat.
    
    *     Building up and assigning new material numbers to
    *     the table variant(GT_VARI).
          LOOP AT gt_vbap ASSIGNING <fs_vbap>.
    
    *       Assigning the necessary values and parameters
    *       to the select options via the table LT_VARI.
            gst_vari-selname = gc_selmat.
            gst_vari-kind    = gc_kind.
            gst_vari-option  = gc_option.
            gst_vari-sign    = gc_sign.
            gst_vari-low     = <fs_vbap>-matnr.
    
    *       Appending the material number to the select option.
            APPEND gst_vari TO gt_vari.
    
          ENDLOOP.
    
    *     Re-populating the variant description.
          gst_varidesc-mandt    = sy-mandt.
          gst_varidesc-aename   = sy-uname.
          gst_varidesc-aedat    = sy-datum.
          gst_varidesc-aetime   = sy-uzeit.
    
    *     Creating a new variant with the new values.
          CALL FUNCTION \'RS_CHANGE_CREATED_VARIANT\'
            EXPORTING
              curr_report               = sy-repid      \"Program Name
              curr_variant              = sy-slset      \"Variant Name
              vari_desc                 = gst_varidesc  \"Variant Description
            TABLES
              vari_contents             = gt_vari       \"Variant Contents
            EXCEPTIONS
              illegal_report_or_variant = 1
              illegal_variantname       = 2
              not_authorized            = 3
              not_executed              = 4
              report_not_existent       = 5
              report_not_supplied       = 6
              variant_doesnt_exist      = 7
              variant_locked            = 8
              selections_no_match       = 9
              OTHERS                    = 10.
    
          IF sy-subrc EQ 0.
    
    *       Variant has been populated with new data successfully.
    
          ENDIF.
    
        ENDIF.
    
      ENDIF.
    
    ENDIF.

    Please note that to test the above program, create a variant with dummy data.

    Then, just select the variant, the program will automatically change the content of the select option \’S_MATNR\’ based on the data retrieved from VBAP-MATNR.

     

     

  • Function Module To Change or Cancel Confirmed Quantity

    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.
    *&--------------------------------------------------------------------&*
    *& Program Description:                                               &*
    *& -----------------------                                            &*
    *& This demo program will update the confirmed quantity in a          &*
    *& sales order.                                                       &*
    *&                                                                    &*
    *& The program demonstrate the use of the FM \'SD_BACKORDER_UPDATE\'.   &*
    *&                                                                    &*
    *& Author:  ABAPCOOKBOOK                                              &*
    *& Website: www.abapcookbook.com                                      &*
    ************************************************************************
    
    ************************************************************************
    * 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.

    Note that, to cancel confirmed quantity just set the value of the field \’GST_KORTAB-VMENG\’ to \’0\’.

  • BAPI To Update Sales Order Order Quantity

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

    REPORT ztestbapiso2.
    *&--------------------------------------------------------------------&*
    *& Program Description:                                               &*
    *& -----------------------                                            &*
    *& This demo program will update the reason for rejection and the     &*
    *& order quantity 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_vbep               TYPE STANDARD TABLE OF vbep,
      gt_item_in            TYPE STANDARD TABLE OF bapisditm,
      gt_item_inx           TYPE STANDARD TABLE OF bapisditmx,
      gt_schedule_lines     TYPE STANDARD TABLE OF bapischdl,
      gt_schedule_linesx    TYPE STANDARD TABLE OF bapischdlx,
      gt_return             TYPE STANDARD TABLE OF bapiret2.
    
    *Field Symbols:
    FIELD-SYMBOLS:
      <fs_vbap>             TYPE vbap,
      <fs_vbep>             TYPE vbep.
    
    *Structures:
    DATA:
      gst_item_hedx         TYPE bapisdh1x,
      gst_item_in           TYPE bapisditm,
      gst_item_inx          TYPE bapisditmx,
      gst_schedule_lines    TYPE bapischdl,
      gst_schedule_linesx   TYPE bapischdlx.
    
    *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,
    
    * Order Quantity.
      p_wmeng TYPE vbep-wmeng 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>.
    
    *     ........................
    *     BAPI Data for updating the reason for rejection.
    *     ........................
    *     (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.
    
    *     ........................
    *     BAPI Data for updating the order 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.
    
    *           (Schedule Line Level)
    *           Setting of the item number(POSNR) at schedule line level.
                gst_schedule_lines-itm_number  = <fs_vbep>-posnr.
                gst_schedule_linesx-itm_number = <fs_vbep>-posnr.
    
    *           Setting of the schedule line number(ETENR) at schedule line level.
                gst_schedule_lines-sched_line  = <fs_vbep>-etenr.
                gst_schedule_linesx-sched_line = <fs_vbep>-etenr.
    
    *           Setting the update flag at schedule line level to update mode.
                gst_schedule_linesx-updateflag = \'U\'.
    
    *           Setting the new order quantity(WMENG).
                gst_schedule_lines-req_qty  = p_wmeng.
                gst_schedule_linesx-req_qty = \'X\'.
    
    *           BAPI schedule lines level tables:
                APPEND:
                  gst_schedule_lines  TO gt_schedule_lines,
                  gst_schedule_linesx TO gt_schedule_linesx.
    
              ELSE.
    
    *           Clear index
                CLEAR gv_tabix.
    
    *           Move out of the loop.
                EXIT.
    
              ENDIF.
    
    *         Clearing of work areas.
              CLEAR:
                gst_schedule_lines,
                gst_schedule_linesx.
    
            ENDLOOP.
    
          ENDIF.
    
    *     Calling BAPI to update reason for rejection and the
    *     schedule line order quantity 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
              schedule_lines   = gt_schedule_lines
              schedule_linesx  = gt_schedule_linesx.
    
    *     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,
            gv_tabix,
    *       Structures:
            gst_item_hedx,
            gst_item_in,
            gst_item_inx.
    
    *     Refreshing internal tables:
          REFRESH:
            gt_item_in,
            gt_item_inx,
            gt_schedule_lines,
            gt_schedule_linesx,
            gt_return.
    
        ENDLOOP.
    
      ENDIF.
    
    ENDIF.

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

    If it happens that you got the error \’Field \’WMENG\’ cannot be changed, VBEPKOM 000010 0001 ready for input\’ from the return table then it may be because of the reason for rejection, clear the reason for rejection and try again. If this is not the case then try to check in VA02 if the order quantity field or the reason for rejection field in editable manually, if not it may be an issue with your test case.

    If you just want to update the reason for rejection, please check this post.

    Hope it helps. 🙂