ABAP - Execute BDC immediately by replacing the include BDCRECX1

When you generate the BDC (Batch Data Communication options define the processing mode for a batch input session) program with SHDB, you can remove a lot of unwanted fields by copying this customize abap include program.  It allows you to execute the BDC program immediately without filling up those SAP generate fields.  To run background, just run it as a background job.

* Execute BDC immediately by replacing the include BDCRECX1
*
* Written by : SAP Basis, ABAP Programming and Other IMG Stuff
*                 https://www.erpgreat.com
*
***INCLUDE ZBDCRECX1.
*  When you generate the program using SHDB, you can replace it
*  with this if you want to execute it immediately without having
*  to process it using SM35.
*
*  During testing you can used the original include.
*
*  For example,
*  include zbdcrecx1.   "After test
*  include bdcrecx.     "Before test
*  -------------------------------------
*  Declare your internal table as RECORD
*  -------------------------------------
*  for programs doing a data transfer by creating a batch-input session
*  and
*  for programs doing a data transfer by CALL TRANSACTION USING

*SELECTION-SCREEN BEGIN OF LINE.
*  PARAMETERS SESSION RADIOBUTTON GROUP CTU.  "create session
*  SELECTION-SCREEN COMMENT 3(20) TEXT-S07 FOR FIELD SESSION.
*  selection-screen position 45.
*  PARAMETERS CTU RADIOBUTTON GROUP CTU DEFAULT 'X'. "call transaction
*  SELECTION-SCREEN COMMENT 48(20) TEXT-S08 FOR FIELD CTU.
*SELECTION-SCREEN END OF LINE.
PARAMETERS: SESSION NO-DISPLAY,
            CTU     NO-DISPLAY DEFAULT 'X'.

*SELECTION-SCREEN BEGIN OF LINE.
*  SELECTION-SCREEN COMMENT 3(20) TEXT-S01 FOR FIELD GROUP.
*  selection-screen position 25.
*  PARAMETERS GROUP(12).                      "group name of session
*  SELECTION-SCREEN COMMENT 48(20) TEXT-S05 FOR FIELD CTUMODE.
*  selection-screen position 70.
*  PARAMETERS CTUMODE LIKE CTU_PARAMS-DISMODE DEFAULT 'N'.
*                                      "A: show all dynpros
*                                      "E: show dynpro on error only
*                                      "N: do not display dynpro
*SELECTION-SCREEN END OF LINE.
PARAMETERS: GROUP(12) NO-DISPLAY,
            CTUMODE   NO-DISPLAY DEFAULT 'N'.

*SELECTION-SCREEN BEGIN OF LINE.
*  SELECTION-SCREEN COMMENT 3(20) TEXT-S02 FOR FIELD USER.
*  selection-screen position 25.
*  PARAMETERS: USER(12) DEFAULT SY-UNAME.    "user for session in batch
*  SELECTION-SCREEN COMMENT 48(20) TEXT-S06 FOR FIELD CUPDATE.
*  selection-screen position 70.
*  PARAMETERS CUPDATE LIKE CTU_PARAMS-UPDMODE DEFAULT 'L'.
*                                      "S: synchronously
*                                      "A: asynchronously
*                                      "L: local
*SELECTION-SCREEN END OF LINE.
PARAMETERS: USER(12) NO-DISPLAY DEFAULT SY-UNAME,
            CUPDATE LIKE CTU_PARAMS-UPDMODE DEFAULT 'L' NO-DISPLAY.

*SELECTION-SCREEN BEGIN OF LINE.
*  SELECTION-SCREEN COMMENT 3(20) TEXT-S03 FOR FIELD KEEP.
*  selection-screen position 25.
*  PARAMETERS: KEEP AS CHECKBOX.       "' ' = delete session if finished
*                                      "'X' = keep   session if finished
*  SELECTION-SCREEN COMMENT 48(20) TEXT-S09 FOR FIELD E_GROUP.
*  selection-screen position 70.
*  parameters E_GROUP(12).             "group name of error-session
*SELECTION-SCREEN END OF LINE.
PARAMETERS: KEEP        NO-DISPLAY,
            E_GROUP(12) NO-DISPLAY.

*SELECTION-SCREEN BEGIN OF LINE.
*  SELECTION-SCREEN COMMENT 3(20) TEXT-S04 FOR FIELD HOLDDATE.
*  selection-screen position 25.
*  PARAMETERS: HOLDDATE LIKE SY-DATUM.
*  SELECTION-SCREEN COMMENT 51(17) TEXT-S02 FOR FIELD E_USER.
*  selection-screen position 70.
*  PARAMETERS: E_USER(12) DEFAULT SY-UNAME.    "user for error-session
*SELECTION-SCREEN END OF LINE.
PARAMETERS: HOLDDATE LIKE SY-DATUM NO-DISPLAY,
           E_USER(12) DEFAULT SY-UNAME NO-DISPLAY.

*SELECTION-SCREEN BEGIN OF LINE.
*  SELECTION-SCREEN COMMENT 51(17) TEXT-S03 FOR FIELD E_KEEP.
*  selection-screen position 70.
*  PARAMETERS: E_KEEP AS CHECKBOX.     "' ' = delete session if finished
*                                      "'X' = keep   session if finished
*SELECTION-SCREEN END OF LINE.
PARAMETERS: E_KEEP NO-DISPLAY.

*SELECTION-SCREEN BEGIN OF LINE.
*  SELECTION-SCREEN COMMENT 51(17) TEXT-S04 FOR FIELD E_HDATE.
*  selection-screen position 70.
*  PARAMETERS: E_HDATE LIKE SY-DATUM.
*SELECTION-SCREEN END OF LINE.
*
*SELECTION-SCREEN SKIP.
PARAMETERS: E_HDATE LIKE SY-DATUM NO-DISPLAY.

*SELECTION-SCREEN BEGIN OF LINE.
*  SELECTION-SCREEN COMMENT 1(33) TEXT-S10 FOR FIELD NODATA.
*  PARAMETERS: NODATA DEFAULT '/' LOWER CASE.          "nodata
*SELECTION-SCREEN END OF LINE.
PARAMETERS: NODATA DEFAULT '/' LOWER CASE NO-DISPLAY.

*SELECTION-SCREEN BEGIN OF LINE.
*  SELECTION-SCREEN COMMENT 1(33) FOR FIELD SMALLLOG.
*  PARAMETERS: SMALLLOG as checkbox.  "' ' = log all transactions
*                                     "'X' = no transaction logging
*SELECTION-SCREEN END OF LINE.
PARAMETERS: SMALLLOG NO-DISPLAY.

*----------------------------------------------------------------------*
*   data definition
*----------------------------------------------------------------------*
*       Batchinputdata of single transaction
DATA:   BDCDATA LIKE BDCDATA    OCCURS 0 WITH HEADER LINE.
*       messages of call transaction
DATA:   MESSTAB LIKE BDCMSGCOLL OCCURS 0 WITH HEADER LINE.
*       error session opened (' ' or 'X')
DATA:   E_GROUP_OPENED.
*       message texts
TABLES: T100.
 

*----------------------------------------------------------------------*
*   at selection screen                                                *
*----------------------------------------------------------------------*
AT SELECTION-SCREEN.
* group and user must be filled for create session
  IF SESSION = 'X' AND
     GROUP = SPACE OR USER = SPACE.
    MESSAGE E613(MS).
  ENDIF.

*----------------------------------------------------------------------*
*   open dataset                                                       *
*----------------------------------------------------------------------*
FORM OPEN_DATASET USING P_DATASET.
  OPEN DATASET P_DATASET IN TEXT MODE.
  IF SY-SUBRC <> 0.
    WRITE: / TEXT-E00, SY-SUBRC.
    STOP.
  ENDIF.
ENDFORM.

*----------------------------------------------------------------------*
*   close dataset                                                      *
*----------------------------------------------------------------------*
FORM CLOSE_DATASET USING P_DATASET.
  CLOSE DATASET P_DATASET.
ENDFORM.

*----------------------------------------------------------------------*
*   create batchinput session                                          *
*   (not for call transaction using...)                                *
*----------------------------------------------------------------------*
FORM OPEN_GROUP.
  IF SESSION = 'X'.
    SKIP.
    WRITE: /(20) 'Create group'(I01), GROUP.
    SKIP.
*   open batchinput group
    CALL FUNCTION 'BDC_OPEN_GROUP'
         EXPORTING  CLIENT   = SY-MANDT
                    GROUP    = GROUP
                    USER     = USER
                    KEEP     = KEEP
                    HOLDDATE = HOLDDATE.
    WRITE: /(30) 'BDC_OPEN_GROUP'(I02),
            (12) 'returncode:'(I05),
                 SY-SUBRC.
  ENDIF.
ENDFORM.

*----------------------------------------------------------------------*
*   end batchinput session                                             *
*   (call transaction using...: error session)                         *
*----------------------------------------------------------------------*
FORM CLOSE_GROUP.
  IF SESSION = 'X'.
*   close batchinput group
    CALL FUNCTION 'BDC_CLOSE_GROUP'.
    WRITE: /(30) 'BDC_CLOSE_GROUP'(I04),
            (12) 'returncode:'(I05),
                 SY-SUBRC.
  ELSE.
    IF E_GROUP_OPENED = 'X'.
      CALL FUNCTION 'BDC_CLOSE_GROUP'.
      WRITE: /.
      WRITE: /(30) 'Fehlermappe wurde erzeugt'(I06).
    ENDIF.
  ENDIF.
ENDFORM.

*----------------------------------------------------------------------*
*        Start new transaction according to parameters                 *
*----------------------------------------------------------------------*
FORM BDC_TRANSACTION USING TCODE.
  DATA: L_MSTRING(480).
  DATA: L_SUBRC LIKE SY-SUBRC.
* batch input session
  IF SESSION = 'X'.
    CALL FUNCTION 'BDC_INSERT'
         EXPORTING TCODE     = TCODE
         TABLES    DYNPROTAB = BDCDATA.
    IF SMALLLOG <> 'X'.
      WRITE: / 'BDC_INSERT'(I03),
               TCODE,
               'returncode:'(I05),
               SY-SUBRC,
               'RECORD:',
               SY-INDEX.
    ENDIF.
* call transaction using
  ELSE.
    REFRESH MESSTAB.
    CALL TRANSACTION TCODE USING BDCDATA
                     MODE   CTUMODE
                     UPDATE CUPDATE
                     MESSAGES INTO MESSTAB.
    L_SUBRC = SY-SUBRC.
    IF SMALLLOG <> 'X'.
*      WRITE: / 'CALL_TRANSACTION',
*               TCODE,
*               'returncode:'(I05),
*               L_SUBRC,
*               'RECORD:',
*               SY-INDEX.
      IF SY-SUBRC = 0.
         FORMAT COLOR OFF.
         WRITE:/ 'Successfully Process ', MESSTAB, RECORD.
      ELSE.
         FORMAT COLOR COL_NEGATIVE.
         WRITE:/ 'Failed Process ', MESSTAB, RECORD.
      ENDIF.
      LOOP AT MESSTAB.
        SELECT SINGLE * FROM T100 WHERE SPRSL = MESSTAB-MSGSPRA
                                  AND   ARBGB = MESSTAB-MSGID
                                  AND   MSGNR = MESSTAB-MSGNR.
        IF SY-SUBRC = 0.
          L_MSTRING = T100-TEXT.
          IF L_MSTRING CS '&1'.
            REPLACE '&1' WITH MESSTAB-MSGV1 INTO L_MSTRING.
            REPLACE '&2' WITH MESSTAB-MSGV2 INTO L_MSTRING.
            REPLACE '&3' WITH MESSTAB-MSGV3 INTO L_MSTRING.
            REPLACE '&4' WITH MESSTAB-MSGV4 INTO L_MSTRING.
          ELSE.
            REPLACE '&' WITH MESSTAB-MSGV1 INTO L_MSTRING.
            REPLACE '&' WITH MESSTAB-MSGV2 INTO L_MSTRING.
            REPLACE '&' WITH MESSTAB-MSGV3 INTO L_MSTRING.
            REPLACE '&' WITH MESSTAB-MSGV4 INTO L_MSTRING.
          ENDIF.
          CONDENSE L_MSTRING.
          WRITE: / MESSTAB-MSGTYP, L_MSTRING(250).
        ELSE.
          WRITE: / MESSTAB.
        ENDIF.
      ENDLOOP.
      SKIP.
    ENDIF.
** Erzeugen fehlermappe ************************************************
    IF L_SUBRC <> 0 AND E_GROUP <> SPACE.
      IF E_GROUP_OPENED = ' '.
        CALL FUNCTION 'BDC_OPEN_GROUP'
             EXPORTING  CLIENT   = SY-MANDT
                        GROUP    = E_GROUP
                        USER     = E_USER
                        KEEP     = E_KEEP
                        HOLDDATE = E_HDATE.
         E_GROUP_OPENED = 'X'.
      ENDIF.
      CALL FUNCTION 'BDC_INSERT'
           EXPORTING TCODE     = TCODE
           TABLES    DYNPROTAB = BDCDATA.
    ENDIF.
  ENDIF.
  REFRESH BDCDATA.
ENDFORM.

*----------------------------------------------------------------------*
*        Start new screen                                              *
*----------------------------------------------------------------------*
FORM BDC_DYNPRO USING PROGRAM DYNPRO.
  CLEAR BDCDATA.
  BDCDATA-PROGRAM  = PROGRAM.
  BDCDATA-DYNPRO   = DYNPRO.
  BDCDATA-DYNBEGIN = 'X'.
  APPEND BDCDATA.
ENDFORM.

*----------------------------------------------------------------------*
*        Insert field                                                  *
*----------------------------------------------------------------------*
FORM BDC_FIELD USING FNAM FVAL.
  IF FVAL <> NODATA.
    CLEAR BDCDATA.
    BDCDATA-FNAM = FNAM.
    BDCDATA-FVAL = FVAL.
    APPEND BDCDATA.
  ENDIF.
ENDFORM. 

--- End of ABAP Program

Fast Links:
BDC program examples:
Auto Disallowed Back Posting to Previous Period

Get help for your ABAP problems
Do you have a ABAP Question?

SAP Books
SAP Certification, Functional, Basis Administration and ABAP Programming Reference Books

ABAP Programming Tips
ABAP and Samples Program Codes for Abapers

Best regards,
SAP Basis, ABAP Programming and Other IMG Stuff
http://www.erpgreat.com

All the site contents are Copyright © www.erpgreat.com and the content authors. All rights reserved.
All product names are trademarks of their respective companies.  The site www.erpgreat.com is in no way affiliated with SAP AG. 
Every effort is made to ensure the content integrity.  Information used on this site is at your own risk. 
 The content on this site may not be reproduced or redistributed without the express written permission of 
www.erpgreat.com or the content authors.